From: Luke H. <lh...@us...> - 2002-11-25 06:53:14
|
Update of /cvsroot/java-game-lib/LWJGL/examples/nehe/lesson12 In directory sc8-pr-cvs1:/tmp/cvs-serv18314/lesson12 Added Files: Lesson12.java Texture.java Log Message: up we go =) --- NEW FILE: Lesson12.java --- CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/nehe/lesson12/Lesson12.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: Lesson12.java,v 1.1 2002/11/25 06:53:11 lholden Exp $ * * Display Lists * * Credit goes to Jeff Molofee (NeHe) whos tutorial this is based on * * @author Luke Holden * @version $Revision: 1.1 $ */ public class Lesson12 { private GL gl; private GLU glu; private boolean done = false; private boolean fullscreen = true; private IntBuffer textureBuf = createIntBuffer(1); /* Storage For The Display List */ private int box; /* Storage For The Second Display List */ private int top; /* Loop For X Axis */ private int xloop; /* Loop For Y Axis */ private int yloop; private float xrot; private float yrot; /* Array For Box Colors */ public static FloatBuffer[] boxcol = { /* Bright: Red, Orange, Yellow, Green, Blue */ createFloatBuffer(3).put(1.0f).put(0.0f).put(0.0f), createFloatBuffer(3).put(1.0f).put(0.5f).put(0.0f), createFloatBuffer(3).put(1.0f).put(1.0f).put(0.0f), createFloatBuffer(3).put(0.0f).put(1.0f).put(0.0f), createFloatBuffer(3).put(0.0f).put(1.0f).put(1.0f) }; /* Array For Top Colors */ public static FloatBuffer[] topcol = { /* Dark: Red, Orange, Yellow, Green, Blue */ createFloatBuffer(3).put(0.5f).put(0.0f).put(0.0f), createFloatBuffer(3).put(0.5f).put(0.25f).put(0.0f), createFloatBuffer(3).put(0.5f).put(0.5f).put(0.0f), createFloatBuffer(3).put(0.0f).put(0.5f).put(0.0f), createFloatBuffer(3).put(0.0f).put(0.5f).put(0.5f) }; /** Creates a new instance of Lesson */ public Lesson12() { } /* Build Box Display List */ private void buildLists() { /* Building Two Lists */ box = gl.genLists(2); /* New Compiled box Display List */ gl.newList(box, GL.COMPILE); /* Start Drawing Quads */ gl.begin(GL.QUADS); /* Bottom Face */ /* NOTE: glNormal3f(...) was missing from the tutorial, but exists * in the lesson source code. * * Without this line, the lighting changes as you rotate the boxes */ gl.normal3f( 0.0f,-1.0f, 0.0f); /* Top Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 1.0f); gl.vertex3f(-1.0f, -1.0f, -1.0f); /* Top Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 1.0f); gl.vertex3f( 1.0f, -1.0f, -1.0f); /* Bottom Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 0.0f); gl.vertex3f( 1.0f, -1.0f, 1.0f); /* Bottom Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 0.0f); gl.vertex3f(-1.0f, -1.0f, 1.0f); /* Front Face */ /* NOTE: glNormal3f(...) was missing from the tutorial, but exists * in the lesson source code. */ gl.normal3f( 0.0f, 0.0f, 1.0f); /* Bottom Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 0.0f); gl.vertex3f(-1.0f, -1.0f, 1.0f); /* Bottom Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 0.0f); gl.vertex3f( 1.0f, -1.0f, 1.0f); /* Top Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 1.0f); gl.vertex3f( 1.0f, 1.0f, 1.0f); /* Top Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 1.0f); gl.vertex3f(-1.0f, 1.0f, 1.0f); /* Back Face */ /* NOTE: glNormal3f(...) was missing from the tutorial, but exists * in the lesson source code. */ gl.normal3f( 0.0f, 0.0f,-1.0f); /* Bottom Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 0.0f); gl.vertex3f(-1.0f, -1.0f, -1.0f); /* Top Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 1.0f); gl.vertex3f(-1.0f, 1.0f, -1.0f); /* Top Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 1.0f); gl.vertex3f( 1.0f, 1.0f, -1.0f); /* Bottom Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 0.0f); gl.vertex3f( 1.0f, -1.0f, -1.0f); /* Right face*/ /* NOTE: glNormal3f(...) was missing from the tutorial, but exists * in the lesson source code. */ gl.normal3f( 1.0f, 0.0f, 0.0f); /* Bottom Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 0.0f); gl.vertex3f( 1.0f, -1.0f, -1.0f); /* Top Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 1.0f); gl.vertex3f( 1.0f, 1.0f, -1.0f); /* Top Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 1.0f); gl.vertex3f( 1.0f, 1.0f, 1.0f); /* Bottom Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 0.0f); gl.vertex3f( 1.0f, -1.0f, 1.0f); /* Left Face */ /* NOTE: glNormal3f(...) was missing from the tutorial, but exists * in the lesson source code. */ gl.normal3f(-1.0f, 0.0f, 0.0f); /* Bottom Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 0.0f); gl.vertex3f(-1.0f, -1.0f, -1.0f); /* Bottom Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 0.0f); gl.vertex3f(-1.0f, -1.0f, 1.0f); /* Top Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 1.0f); gl.vertex3f(-1.0f, 1.0f, 1.0f); /* Top Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 1.0f); gl.vertex3f(-1.0f, 1.0f, -1.0f); gl.end(); gl.endList(); /* top List Value Is box List Value +1 */ top = box+1; /* New Compiled top Display List */ gl.newList(top, GL.COMPILE); /* Start Drawing Quad */ gl.begin(GL.QUADS); /* Top Face */ /* NOTE: glNormal3f(...) was missing from the tutorial, but exists * in the lesson source code. */ gl.normal3f( 0.0f, 1.0f, 0.0f); /* Top Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 1.0f); gl.vertex3f(-1.0f, 1.0f, -1.0f); /* Bottom Left Of The Texture and Quad */ gl.texCoord2f(0.0f, 0.0f); gl.vertex3f(-1.0f, 1.0f, 1.0f); /* Bottom Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 0.0f); gl.vertex3f( 1.0f, 1.0f, 1.0f); /* Top Right Of The Texture and Quad */ gl.texCoord2f(1.0f, 1.0f); gl.vertex3f( 1.0f, 1.0f, -1.0f); gl.end(); gl.endList(); } /* 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[1]; /* Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit */ try { textureImage[0] = loadImage("data/cube.png"); if (textureImage[0] == null) { throw new Exception("Error: got null from loadBmp()!"); } /* Create The Texture */ gl.genTextures(1, Sys.getDirectBufferAddress(textureBuf)); /* Typical Texture Generation Using Data From The Bitmap */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(0)); /* Generate The Texture */ gl.texImage2D(GL.TEXTURE_2D, 0, 3, textureImage[0].getWidth(), textureImage[0].getHeight(), 0, GL.RGB, GL.UNSIGNED_BYTE, textureImage[0].getPtr()); /* Linear Filtering */ gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR); } catch (Exception e) { throw new Exception("Problem loading textures", e); } } 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(); buildLists(); /* Enable Texture Mapping */ gl.enable(GL.TEXTURE_2D); /* Enables Smooth Shading */ gl.shadeModel(GL.SMOOTH); /* Black Background */ gl.clearColor(0.0f, 0.0f, 0.0f, 0.5f); /* Depth Buffer Setup */ gl.clearDepth(1.0f); /* Enables Depth Testing */ gl.enable(GL.DEPTH_TEST); /* The Type Of Depth Test To Do */ gl.depthFunc(GL.LEQUAL); /* Quick And Dirty Lighting (Assumes Light0 Is Set Up) */ gl.enable(GL.LIGHT0); /* Enable Lighting */ gl.enable(GL.LIGHTING); /* Enable Material Coloring */ gl.enable(GL.COLOR_MATERIAL); /* Really Nice Perspective Calculations */ gl.hint(GL.PERSPECTIVE_CORRECTION_HINT, GL.NICEST); } 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); /* Select our Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(0)); /* Loop Through The Y Plane */ for (yloop = 1; yloop < 6; yloop++) { /* Loop Through The X Plane */ for (xloop = 0; xloop < yloop; xloop++) { /* Reset The Current Modelview Matrix */ gl.loadIdentity(); /* Position The Cubes On The Screen */ gl.translatef(1.4f + (((float) xloop) * 2.8f) - (((float) yloop) * 1.4f), ((6.0f - ((float) yloop)) * 2.4f) -7.0f, -20.0f); /* Tilt The Cubes Up And Down */ gl.rotatef(45.0f - (2.0f * yloop) + xrot, 1.0f, 0.0f, 0.0f); /* Spin Cubes Left And Right */ gl.rotatef(45.0f + yrot, 0.0f, 1.0f, 0.0f); /* Select A Box Color * FIXME: gl.color3fv is not currently implemented. */ // gl.color3fv(Sys.getDirectBufferAddress(boxcol[yloop-1])); gl.color3f(boxcol[yloop-1].get(0), boxcol[yloop-1].get(1), boxcol[yloop-1].get(2)); /* Draw The Box */ gl.callList(box); /* Select The Top Color */ // gl.color3fv(Sys.getDirectBufferAddress(topcol[yloop-1])); gl.color3f(topcol[yloop-1].get(0), topcol[yloop-1].get(1), topcol[yloop-1].get(2)); /* Draw The Box */ gl.callList(top); } } return true; } public void killGLWindow() { 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 constant effect */ Keyboard.poll(); if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { yrot -= 0.2f; } if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { yrot += 0.2f; } if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { xrot -= 0.2f; } if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { xrot += 0.2f; } /* 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 static IntBuffer createIntBuffer(int size) { ByteBuffer temp = ByteBuffer.allocateDirect(4 * size); temp.order(ByteOrder.nativeOrder()); return temp.asIntBuffer(); } private static FloatBuffer createFloatBuffer(int size) { ByteBuffer temp = ByteBuffer.allocateDirect(4 * size); temp.order(ByteOrder.nativeOrder()); return temp.asFloatBuffer(); } public static void main(String[] arguments) { int err = 0; Lesson12 lesson = new Lesson12(); 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/lesson12/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:11 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; } } |