From: Luke H. <lh...@us...> - 2002-11-25 06:53:15
|
Update of /cvsroot/java-game-lib/LWJGL/examples/nehe/lesson17 In directory sc8-pr-cvs1:/tmp/cvs-serv18314/lesson17 Added Files: Lesson17.java Texture.java Log Message: up we go =) --- NEW FILE: Lesson17.java --- CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/nehe/lesson17/Lesson17.java /* * Copyright (c) 2002 Light Weight Java Game Library Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of 'Light Weight Java Game Library' nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.nio.*; import java.io.*; import java.awt.image.BufferedImage; import javax.imageio.*; import org.lwjgl.*; import org.lwjgl.opengl.*; import org.lwjgl.input.*; /** * $Id: Lesson17.java,v 1.1 2002/11/25 06:53:12 lholden Exp $ * * 2D Texture Font * * Credit goes to Giuseppe D'Agata and Jeff Molofee (NeHe) whos tutorial this is based on * * @author Luke Holden * @version $Revision: 1.1 $ */ public class Lesson17 { private GL gl; private GLU glu; private boolean done = false; private boolean fullscreen = true; /* Base Display List For The Font */ private int base; private IntBuffer textureBuf = createIntBuffer(2); /* Generic Loop Variable */ private int i; /* 1st Counter Used To Move Text & For Coloring */ private float cnt1; /* 1nd Counter Used To Move Text & For Coloring */ private float cnt2; /** Creates a new instance of Lesson */ public Lesson17() { } /* A javafied version of AUX_RGBImageRec *LoadBMP(char*); */ private Texture loadImage(String filename) throws Exception { Texture texture = null; BufferedImage tmpImg = null; /* normally I would use StringUtils.isValid(String) from the jakarta commons lib, but thats beyond the scope of this lesson */ if ((filename != null) && (filename.trim() != "")) { try { InputStream is = getClass().getResourceAsStream(filename); tmpImg = (BufferedImage) ImageIO.read(is); if (tmpImg == null) { throw new Exception("Error: Got null from ImageIO.read()"); } texture = new Texture(tmpImg); } catch ( Exception e ) { throw new Exception("Problem loading bitmap", e); } } else { throw new Exception("Error: file name is not valid!"); } return texture; } /* Load Bitmaps And Convert To Textures */ private void loadGLTextures() throws Exception { /* Create Storage Space For The Texture */ Texture[] textureImage = new Texture[2]; /* Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit */ try { textureImage[0] = loadImage("data/font.png"); textureImage[1] = loadImage("data/bumps.png"); if ((textureImage[0] == null) || (textureImage[1] == null)) { throw new Exception("Error: got null from loadBmp()!"); } /* Create The Texture */ gl.genTextures(2, Sys.getDirectBufferAddress(textureBuf)); /* Loop Through All The Textures */ for (i = 0; i < 2; i++) { /* Build The Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(i)); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR); gl.texImage2D(GL.TEXTURE_2D, 0, 3, textureImage[i].getWidth(), textureImage[i].getHeight(), 0, GL.RGB, GL.UNSIGNED_BYTE, textureImage[i].getPtr()); } } catch (Exception e) { throw new Exception("Problem loading textures", e); } } /* Build Our Font Display List */ private void buildFont() { /* Our X Character Coord */ float cx; /* Our Y Character Coord */ float cy; /* Creating 256 Display Lists */ base = gl.genLists(256); /* Select Our Font Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(0)); /* Loop Through All 256 Lists */ for (i = 0; i < 256; i++) { /* X Position Of Current Character */ cx = (i % 16) / 16.0f; /* Y Position Of Current Character */ cy = (i / 16) / 16.0f; /* Start Building A List */ gl.newList(base + i, GL.COMPILE); /* Use A Quad For Each Character */ gl.begin(GL.QUADS); gl.texCoord2f(cx,1-cy-0.0625f); gl.vertex2i(0,0); gl.texCoord2f(cx+0.0625f,1-cy-0.0625f); gl.vertex2i(16,0); gl.texCoord2f(cx+0.0625f,1-cy); gl.vertex2i(16,16); gl.texCoord2f(cx,1-cy); gl.vertex2i(0,16); gl.end(); /* Move To The Right Of The Character */ gl.translated(10,0,0); gl.endList(); } } /* Delete The Font From Memory */ void killFont() { /* Delete All 256 Display Lists */ gl.deleteLists(base, 256); } /* Where The Printing Happens */ void print(int x, int y, String text, int set) { if (set > 1) { set = 1; } /* Load our string into a CharBuffer */ byte[] charAry = text.getBytes(); ByteBuffer stringBuf = createByteBuffer(charAry.length); stringBuf.put(charAry); stringBuf.flip(); /* Select Our Font Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(0)); /* Disables Depth Testing */ gl.disable(GL.DEPTH_TEST); /* Select The Projection Matrix */ gl.matrixMode(GL.PROJECTION); /* Put Our Projection Matrix On The Stack */ gl.pushMatrix(); /* Reset The Projection Matrix */ gl.loadIdentity(); /* Set Up An Ortho Screen */ gl.ortho(0, 640, 0, 480, -1, 1); /* Select The Modelview Matrix */ gl.matrixMode(GL.MODELVIEW); /* Put Our ModelView Matrix On The Stack */ gl.pushMatrix(); /* Reset The Modelview Matrix */ gl.loadIdentity(); /* Position The Text (0,0 - Bottom Left) */ gl.translated(x, y, 0); /* Choose The Font Set (0 or 1) */ gl.listBase(base-32+(128*set)); /* Write The Text To The Screen */ gl.callLists(stringBuf.remaining(), GL.BYTE, Sys.getDirectBufferAddress(stringBuf)); /* Select The Projection Matrix */ gl.matrixMode(GL.PROJECTION); /* Pop Our Projection Matrix Off The Stack */ gl.popMatrix(); /* Select The Modelview Matrix */ gl.matrixMode(GL.MODELVIEW); /* Pop Our Modelview Matrix Off The Stack */ gl.popMatrix(); /* Enable Depth Testing */ gl.enable(GL.DEPTH_TEST); } private void resizeGLScene(int width, int height) { /* Reset The Current Viewport */ gl.viewport(0, 0, width, height); /* Select The Projection Matrix */ gl.matrixMode(GL.PROJECTION); /* Reset The Projection Matrix */ gl.loadIdentity(); /* Calculate The Aspect Ratio Of The Window */ glu.perspective(45.0f, ((float) Display.getWidth()) / ((float) Display.getHeight()), 0.1f, 100.0f); /* Select The Modelview Matrix */ gl.matrixMode(GL.MODELVIEW); /* Reset The Modelview Matrix */ gl.loadIdentity(); } private void initGL() throws Exception { /* Jump To Texture Loading Routine */ try { loadGLTextures(); buildFont(); /* Black Background */ gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f); /* Depth Buffer Setup */ gl.clearDepth(1.0f); /* The Type Of Depth Test To Do */ gl.depthFunc(GL.LEQUAL); /* Select The Type Of Blending */ gl.blendFunc(GL.SRC_ALPHA, GL.ONE); /* Enables Smooth Shading */ gl.shadeModel(GL.SMOOTH); /* Enable Texture Mapping */ gl.enable(GL.TEXTURE_2D); /* Enables Depth Testing */ gl.enable(GL.DEPTH_TEST); } catch (Exception e) { throw new Exception("Problem initialising GL", e); } } private boolean drawGLScene() { /* Clear The Screen And The Depth Buffer */ gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT); /* Reset The Current Modelview Matrix */ gl.loadIdentity(); /* Select our Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(1)); /* Move Into The Screen 5 Units */ gl.translatef(0.0f, 0.0f, -5.0f); /* Rotate On The Z Axis 45 Degrees (Clockwise) */ gl.rotatef(45.0f, 0.0f, 0.0f, 1.0f); /* Rotate On The X & Y Axis By cnt1 (Left To Right) */ gl.rotatef(cnt1 * 30.0f, 1.0f, 1.0f, 0.0f); /* Disable Blending Before We Draw In 3D */ gl.disable(GL.BLEND); /* Bright White */ gl.color3f(1.0f, 1.0f, 1.0f); gl.begin(GL.QUADS); gl.texCoord2d(0.0f, 0.0f); gl.vertex2f(-1.0f, 1.0f); gl.texCoord2d(1.0f, 0.0f); gl.vertex2f( 1.0f, 1.0f); gl.texCoord2d(1.0f, 1.0f); gl.vertex2f( 1.0f, -1.0f); gl.texCoord2d(0.0f, 1.0f); gl.vertex2f(-1.0f, -1.0f); gl.end(); /* Rotate On The X & Y Axis By 90 Degrees (Left To Right) */ gl.rotatef(90.0f, 1.0f, 1.0f, 0.0f); gl.begin(GL.QUADS); gl.texCoord2d(0.0f, 0.0f); gl.vertex2f(-1.0f, 1.0f); gl.texCoord2d(1.0f, 0.0f); gl.vertex2f( 1.0f, 1.0f); gl.texCoord2d(1.0f, 1.0f); gl.vertex2f( 1.0f, -1.0f); gl.texCoord2d(0.0f, 1.0f); gl.vertex2f(-1.0f, -1.0f); gl.end(); /* Enable Blinding */ gl.enable(GL.BLEND); /* Reset The View */ gl.loadIdentity(); // Pulsing Colors Based On Text Position gl.color3f(1.0f * ((float) java.lang.Math.cos(cnt1)), 1.0f * ((float) java.lang.Math.sin(cnt2)), 1.0f - (0.5f * ((float) java.lang.Math.cos(cnt1+cnt2)))); /* Print Text To The Screen */ print((int) (280 + (250 * java.lang.Math.cos(cnt1))), (int) (235 + (200 * java.lang.Math.sin(cnt2))), "NeHe", 0); gl.color3f(1.0f * ((float) java.lang.Math.sin(cnt2)), 1.0f - (0.5f * ((float) java.lang.Math.cos(cnt1 + cnt2))), 1.0f * ((float) java.lang.Math.cos(cnt1))); /* Print Text To The Screen */ print((int) (280 + (230 * java.lang.Math.cos(cnt2))), (int) (235 + (200 * java.lang.Math.sin(cnt1))), "OpenGL", 1); /* Set Color To Blue */ gl.color3f(0.0f, 0.0f, 1.0f); /* Print Text To The Screen */ print((int) (240 + (200 * java.lang.Math.cos((cnt2 + cnt1) / 5))), 2, "Giuseppe D'Agata", 0); /* Set Color To White */ gl.color3f(1.0f, 1.0f, 1.0f); /* Print Text To The Screen */ print((int) (242 + (200 * java.lang.Math.cos((cnt2 + cnt1) / 5))), 2, "Giuseppe D'Agata", 0); cnt1 += 0.01f; cnt2 += 0.0081f; return true; } public void killGLWindow() { killFont(); Keyboard.destroy(); gl.destroy(); Display.destroy(); } public void createGLWindow(int width, int height, int bits, boolean fullscreenflag) throws Exception { fullscreen = fullscreenflag; try { Display.create(new DisplayMode(width, height, bits, 60), fullscreenflag); gl = new GL(bits, 0, bits, 8); gl.create(); glu = new GLU(gl); Keyboard.create(); Keyboard.enableBuffer(); resizeGLScene(Display.getWidth(), Display.getHeight()); initGL(); } catch (Exception e) { throw new Exception("Problem initialising Lesson", e); } } public void start() throws Exception { try { createGLWindow(640, 480, 16, fullscreen); while (!done) { loop(); } killGLWindow(); } catch (Exception e) { throw new Exception("Problem starting loop", e); } } private void loop() { drawGLScene(); gl.swapBuffers(); /* Keys that have a toggle effect */ Keyboard.read(); for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) { Keyboard.next(); if (Keyboard.key == Keyboard.KEY_ESCAPE && Keyboard.state) { done = true; } } } private IntBuffer createIntBuffer(int size) { ByteBuffer temp = ByteBuffer.allocateDirect(4 * size); temp.order(ByteOrder.nativeOrder()); return temp.asIntBuffer(); } private ByteBuffer createByteBuffer(int size) { ByteBuffer temp = ByteBuffer.allocateDirect(4 * size); temp.order(ByteOrder.nativeOrder()); return temp; } public static void main(String[] arguments) { int err = 0; Lesson17 lesson = new Lesson17(); try { lesson.start(); } catch (Exception e) { err = 1; e.printStackTrace(); } System.exit(err); } } --- NEW FILE: Texture.java --- CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/nehe/lesson17/Texture.java /* * Copyright (c) 2002 Light Weight Java Game Library Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of 'Light Weight Java Game Library' nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.nio.*; import java.awt.image.*; import java.awt.geom.*; import org.lwjgl.*; import org.lwjgl.opengl.*; import org.lwjgl.input.*; /** * $Id: Texture.java,v 1.1 2002/11/25 06:53:12 lholden Exp $ * * Just a small container class for holding texture data * * @author Luke Holden * @version $Revision: 1.1 $ */ public class Texture { private ByteBuffer data; private int height; private int width; /** Creates a new instance of Texture */ public Texture(BufferedImage tmpImg) { width = tmpImg.getWidth(); height = tmpImg.getWidth(); /* flip the image, so it displays right * There might be a way to do this differently... */ AffineTransform tx = AffineTransform.getScaleInstance(1, -1); tx.translate(0, -tmpImg.getHeight(null)); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); tmpImg = op.filter(tmpImg, null); data = ByteBuffer.allocateDirect(4 * width * height); data.order(ByteOrder.nativeOrder()); data.clear(); byte[] byteData = (byte[])tmpImg.getRaster().getDataElements(0, 0, width, height, null); data.put(byteData); data.flip(); } public int getData() { return data.get(0); } public ByteBuffer getBuffer() { return data; } public int getPtr() { return Sys.getDirectBufferAddress(data); } public int getHeight() { return height; } public int getWidth() { return width; } } |