Update of /cvsroot/java-game-lib/LWJGL/examples In directory sc8-pr-cvs1:/tmp/cvs-serv936 Added Files: BaseWindow.java BitmapFonts.java Blending.java DisplayLists.java FiltersLightingAndControl.java FirstPersonWorld.java FlagEffect.java Fog.java MovingStarBitmaps.java Polygon.java PolygonColored.java PolygonColoredRotating.java PolygonColoredRotating3D.java Texture.java TextureMapping.java Log Message: Lots of changes. Everything is now real time so ALL animations should run at the same speed reguardless of frame rate. Keyboard input now uses only the Keyboard.pull() method. Also changed the class names for everything. FirstPersonWorld now limits how much you can rotate the camera up or down. Plus some other small changes I cant think of ATM =) --- NEW FILE: BaseWindow.java --- CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/BaseWindow.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 org.lwjgl.*; import org.lwjgl.opengl.*; import org.lwjgl.input.*; /** * $Id: BaseWindow.java,v 1.1 2002/11/27 00:16:34 lholden Exp $ * * Setting Up An OpenGL Window * * Credit goes to Jeff Molofee (NeHe) whos tutorial this is based on * * @author Luke Holden * @version $Revision: 1.1 $ */ public class BaseWindow { protected GL gl; protected GLU glu; protected boolean fullscreen = true; protected boolean done = false; protected long timerRes; /** Creates a new instance of Lesson */ public BaseWindow() { } protected 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(); } protected void initGL() throws Exception { /* Enables Smooth Shading */ gl.shadeModel(GL.SMOOTH); /* Black Background */ gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f); /* 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); /* Really Nice Perspective Calculations */ gl.hint(GL.PERSPECTIVE_CORRECTION_HINT, GL.NICEST); } protected boolean drawGLScene(float frameTime) { /* Clear The Screen And The Depth Buffer */ gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT); /* Reset The Current Modelview Matrix */ gl.loadIdentity(); return true; } protected void killGLWindow() { Keyboard.destroy(); gl.destroy(); Display.destroy(); } protected 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); } } protected void start(int width, int height, int bpp, boolean fullscreen) throws Exception { long frameTime = 0; timerRes = Sys.getTimerResolution(); if (timerRes == 0) { throw new Exception("There are no timers availible!"); } try { createGLWindow(width, height, bpp, fullscreen); do { /* The frameTime is how much time it takes to draw a single frame. * We use this so we can animate things in real time. * * Say you want to move an object 100 pixels every second and it * takes 0.10 seconds to draw a frame. Pixels per second * multiplied by the frame time equals the amount of pixels to move * per frame. or 100 * 0.10 = 10; */ frameTime = Sys.getTime(); /* Reset the counter, so we can find out how long it takes to draw a * frame */ Sys.setTime(0); } while (!loop((float)frameTime / (float)timerRes)); killGLWindow(); } catch (Exception e) { throw new Exception("Problem starting loop", e); } } protected boolean loop(float frameTime) { /* if drawGLScene(...) fails, stop the loop */ if (!drawGLScene(frameTime)) { return true; } gl.swapBuffers(); input(frameTime); return done; } protected void input(float frameTime) { Keyboard.poll(); if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { done = true; } } public static void main(String[] arguments) { int err = 0; BaseWindow lesson = new BaseWindow(); try { lesson.start(640, 480, 16, false); } catch (Exception e) { err = 1; e.printStackTrace(); } System.exit(err); } } --- NEW FILE: BitmapFonts.java --- CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/BitmapFonts.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: BitmapFonts.java,v 1.1 2002/11/27 00:16:34 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 BitmapFonts extends TextureMapping { /* 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; /* buffer for holding the text to print */ public static int BUFLEN = 255; private ByteBuffer stringBuf = createByteBuffer(BUFLEN); /** Creates a new instance of Lesson */ public BitmapFonts() { } /* Load Bitmaps And Convert To Textures */ protected 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 */ private void killFont() { /* Delete All 256 Display Lists */ gl.deleteLists(base, 256); } /* Where The Printing Happens */ private void print(int x, int y, String text, int set) { if (set > 1) { set = 1; } /* prevent a buffer overflow */ if (text.length() > BUFLEN -1) { text = text.substring(0, 254); } /* Load our string into a CharBuffer */ byte[] charAry = text.getBytes(); stringBuf.clear(); 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); } protected 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); } } protected boolean drawGLScene(float frameTime) { /* 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 += (frameTime * 1.0f); cnt2 += (frameTime * 0.81f); return true; } protected void killGLWindow() { killFont(); super.killGLWindow(); } 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; BitmapFonts lesson = new BitmapFonts(); try { lesson.start(640, 480, 16, false); } catch (Exception e) { err = 1; e.printStackTrace(); } System.exit(err); } } --- NEW FILE: Blending.java --- CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/Blending.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.swing.JOptionPane; import javax.imageio.*; import org.lwjgl.*; import org.lwjgl.opengl.*; import org.lwjgl.input.*; /** * $Id: Blending.java,v 1.1 2002/11/27 00:16:34 lholden Exp $ * * Blending * * Credit goes to Tom Stanis and Jeff Molofee (NeHe) whos tutorial this is based on * * @author Luke Holden * @version $Revision: 1.1 $ */ public class Blending extends FiltersLightingAndControl { /* Lighting ON / OFF */ private boolean light; /* Blending OFF/ON? ( NEW ) */ private boolean blend; private float xrot; private float yrot; private float xspeed; private float yspeed; private float z=-5.0f; /* Ambient Light Values */ private FloatBuffer lightAmbient = createFloatBuffer(4).put(0.5f).put(0.5f).put(0.5f).put(1.0f); /* Diffuse Light Values */ private FloatBuffer lightDiffuse = createFloatBuffer(4).put(1.0f).put(1.0f).put(1.0f).put(1.0f); /* Light Position */ private FloatBuffer lightPosition = createFloatBuffer(4).put(0.0f).put(0.0f).put(2.0f).put(1.0f); /* Which Filter To Use */ private int filter; /* Storage for 3 textures */ private IntBuffer textureBuf = createIntBuffer(3); /* our key states */ private class KeyToggle { public boolean l = false; public boolean f = false; public boolean b = false; } private KeyToggle keys = new KeyToggle(); /** Creates a new instance of Lesson */ public Blending() { } /* Load Bitmaps And Convert To Textures */ protected 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/glass.png"); if (textureImage[0] == null) { throw new Exception("Error: got null from loadBmp()!"); } /* Create Three Textures */ gl.genTextures(3, Sys.getDirectBufferAddress(textureBuf)); /* Create Nearest Filtered Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(0)); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.NEAREST); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.NEAREST); gl.texImage2D(GL.TEXTURE_2D, 0, 3, textureImage[0].getWidth(), textureImage[0].getHeight(), 0, GL.RGB, GL.UNSIGNED_BYTE, textureImage[0].getPtr()); /* Create Linear Filtered Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(1)); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR); gl.texImage2D(GL.TEXTURE_2D, 0, 3, textureImage[0].getWidth(), textureImage[0].getHeight(), 0, GL.RGB, GL.UNSIGNED_BYTE, textureImage[0].getPtr()); /* Create Linear Filtered Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(2)); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_NEAREST); glu.build2DMipmaps(GL.TEXTURE_2D, 3, textureImage[0].getWidth(), textureImage[0].getHeight(), GL.RGB, GL.UNSIGNED_BYTE, textureImage[0].getPtr()); } catch (Exception e) { throw new Exception("Problem loading textures", e); } } protected void initGL() throws Exception{ /* Jump To Texture Loading Routine */ try { super.initGL(); /* Full Brightness, 50% Alpha ( NEW ) */ gl.color4f(1.0f,1.0f,1.0f,0.5f); /* Blending Function For Translucency Based On Source Alpha Value ( NEW ) */ gl.blendFunc(GL.SRC_ALPHA, GL.ONE); } catch (Exception e) { throw new Exception("Problem initialising GL", e); } } protected boolean drawGLScene(float frameTime) { /* Clear The Screen And The Depth Buffer */ gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT); /* Reset The Current Modelview Matrix */ gl.loadIdentity(); /* Move Into The Screen 5 Units */ gl.translatef(0.0f, 0.0f, z); /* Rotate On The X Axis */ gl.rotatef(xrot, 1.0f, 0.0f, 0.0f); /* Rotate On The Y Axis */ gl.rotatef(yrot, 0.0f, 1.0f, 0.0f); /* Select our Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(filter)); /* build our textured cube */ gl.begin(GL.QUADS); /* Front Face */ /* Normal Pointing Towards Viewer */ 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 */ /* Normal Pointing Away From Viewer */ 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); /* Top Face */ /* Normal Pointing Up */ 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); /* Bottom Face */ /* Normal Pointing Down */ 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); /* Right face */ /* Normal Pointing Right */ 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 */ /* Normal Pointing Left */ 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(); /* X Axis Rotation */ xrot+=frameTime * xspeed; /* Y Axis Rotation */ yrot+=frameTime * yspeed; return true; } protected void input(float frameTime) { Keyboard.poll(); /* Keys that have a toggle effect */ if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { done = true; } if (Keyboard.isKeyDown(Keyboard.KEY_L)) { if (!keys.l) { keys.l = true; light=!light; if (!light) { gl.disable(GL.LIGHTING); } else { gl.enable(GL.LIGHTING); } } } else { keys.l = false; } if (Keyboard.isKeyDown(Keyboard.KEY_F)) { if (!keys.f) { keys.f = true; filter+=1; if (filter>2) { filter=0; } } } else { keys.f = false; } if (Keyboard.isKeyDown(Keyboard.KEY_B)) { if (!keys.b) { keys.b = true; blend=!blend; if(blend) { /* Turn Blending On */ gl.enable(GL.BLEND); /* Turn Depth Testing Off */ gl.disable(GL.DEPTH_TEST); } else { /* Turn Blending Off */ gl.disable(GL.BLEND); /* Turn Depth Testing On */ gl.enable(GL.DEPTH_TEST); } } } else { keys.b = false; } /* Keys that have a constant effect */ if (Keyboard.isKeyDown(Keyboard.KEY_PRIOR)) { z-=frameTime * 10f; } if (Keyboard.isKeyDown(Keyboard.KEY_NEXT)) { z+=frameTime * 10f; } if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { xspeed-=frameTime * 20f; } if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { xspeed+=frameTime * 20f; } if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { yspeed+=frameTime * 20f; } if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { yspeed-=frameTime * 20f; } } public static void main(String[] arguments) { int err = 0; Blending lesson = new Blending(); try { lesson.start(640, 480, 16, false); } catch (Exception e) { err = 1; e.printStackTrace(); } System.exit(err); } } --- NEW FILE: DisplayLists.java --- CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/DisplayLists.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: DisplayLists.java,v 1.1 2002/11/27 00:16:34 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 DisplayLists extends FiltersLightingAndControl { 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 DisplayLists() { } /* 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(); } /* Load Bitmaps And Convert To Textures */ protected 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); } } protected 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); } } protected boolean drawGLScene(float frameTime) { /* 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])); /* Draw The Box */ gl.callList(box); /* Select The Top Color */ gl.color3fv(Sys.getDirectBufferAddress(topcol[yloop-1])); /* Draw The Box */ gl.callList(top); } } return true; } protected void input(float frameTime) { Keyboard.poll(); /* Keys that have a toggle effect */ if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { done = true; } /* Keys that have a constant effect */ if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { yrot -= (frameTime * 25.0f); } if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { yrot += (frameTime * 25.0f); } if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { xrot -= (frameTime * 25.0f); } if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { xrot += (frameTime * 25.0f); } } public static void main(String[] arguments) { int err = 0; DisplayLists lesson = new DisplayLists(); try { lesson.start(640, 480, 16, false); } catch (Exception e) { err = 1; e.printStackTrace(); } System.exit(err); } } --- NEW FILE: FiltersLightingAndControl.java --- CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/FiltersLightingAndControl.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.swing.JOptionPane; import javax.imageio.*; import org.lwjgl.*; import org.lwjgl.opengl.*; import org.lwjgl.input.*; /** * $Id: FiltersLightingAndControl.java,v 1.1 2002/11/27 00:16:34 lholden Exp $ * * Texture Filters, Lighting & Keyboard Control * * Credit goes to Jeff Molofee (NeHe) whos tutorial this is based on * * @author Luke Holden * @version $Revision: 1.1 $ */ public class FiltersLightingAndControl extends TextureMapping { protected boolean fullscreen = false; /* Lighting ON / OFF */ private boolean light; private float xrot; private float yrot; private float xspeed; private float yspeed; private float z=-5.0f; /* Ambient Light Values ( NEW ) */ private FloatBuffer lightAmbient = createFloatBuffer(4).put(0.5f).put(0.5f).put(0.5f).put(1.0f); /* Diffuse Light Values ( NEW ) */ private FloatBuffer lightDiffuse = createFloatBuffer(4).put(1.0f).put(1.0f).put(1.0f).put(1.0f); /* Light Position ( NEW ) */ private FloatBuffer lightPosition = createFloatBuffer(4).put(0.0f).put(0.0f).put(2.0f).put(1.0f); /* Which Filter To Use */ private int filter; /* Storage for 3 textures */ private IntBuffer textureBuf = createIntBuffer(3); /* our key states */ private class KeyToggle { public boolean f1 = false; public boolean l = false; public boolean f = false; } private KeyToggle keys = new KeyToggle(); /** Creates a new instance of Lesson */ public FiltersLightingAndControl() { } /* Load Bitmaps And Convert To Textures */ protected 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/crate.png"); if (textureImage[0] == null) { throw new Exception("Error: got null from loadBmp()!"); } /* Create Three Textures */ gl.genTextures(3, Sys.getDirectBufferAddress(textureBuf)); /* Create Nearest Filtered Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(0)); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.NEAREST); // ( NEW ) gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.NEAREST); // ( NEW ) gl.texImage2D(GL.TEXTURE_2D, 0, 3, textureImage[0].getWidth(), textureImage[0].getHeight(), 0, GL.RGB, GL.UNSIGNED_BYTE, textureImage[0].getPtr()); /* Create Linear Filtered Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(1)); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR); gl.texImage2D(GL.TEXTURE_2D, 0, 3, textureImage[0].getWidth(), textureImage[0].getHeight(), 0, GL.RGB, GL.UNSIGNED_BYTE, textureImage[0].getPtr()); /* Create Linear Filtered Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(2)); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR); gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_NEAREST); // ( NEW ) glu.build2DMipmaps(GL.TEXTURE_2D, 3, textureImage[0].getWidth(), textureImage[0].getHeight(), GL.RGB, GL.UNSIGNED_BYTE, textureImage[0].getPtr()); // ( NEW ) } catch (Exception e) { throw new Exception("Problem loading textures", e); } } protected void initGL() throws Exception{ /* Jump To Texture Loading Routine */ try { super.initGL(); /* Setup The Ambient Light */ gl.lightfv(GL.LIGHT1, GL.AMBIENT, Sys.getDirectBufferAddress(lightAmbient)); /* Setup The Diffuse Light */ gl.lightfv(GL.LIGHT1, GL.DIFFUSE, Sys.getDirectBufferAddress(lightDiffuse)); /* Position The Light */ gl.lightfv(GL.LIGHT1, GL.POSITION, Sys.getDirectBufferAddress(lightPosition)); gl.enable(GL.LIGHT1); } catch (Exception e) { throw new Exception("Problem initialising GL", e); } } protected boolean drawGLScene(float frameTime) { /* Clear The Screen And The Depth Buffer */ gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT); /* Reset The Current Modelview Matrix */ gl.loadIdentity(); /* Move Into The Screen 5 Units */ gl.translatef(0.0f, 0.0f, z); /* Rotate On The X Axis */ gl.rotatef(xrot, 1.0f, 0.0f, 0.0f); /* Rotate On The Y Axis */ gl.rotatef(yrot, 0.0f, 1.0f, 0.0f); /* Select our Texture */ gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(filter)); /* build our textured cube */ gl.begin(GL.QUADS); /* Front Face */ /* Normal Pointing Towards Viewer */ 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 */ /* Normal Pointing Away From Viewer */ 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); /* Top Face */ /* Normal Pointing Up */ 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); /* Bottom Face */ /* Normal Pointing Down */ 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); /* Right face */ /* Normal Pointing Right */ 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 */ /* Normal Pointing Left */ 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(); /* X Axis Rotation */ xrot+=frameTime * xspeed; /* Y Axis Rotation */ yrot+=frameTime * yspeed; return true; } protected void input(float frameTime) { Keyboard.poll(); /* Keys that have a toggle effect */ if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { done = true; } /* F1 switch between fullscreen/windowed is buggy... * So for now, we are leaving this ability disabled */ if (Keyboard.isKeyDown(Keyboard.KEY_F1)) { if (!keys.f1) { keys.f1 = true; // try { // killGLWindow(); // fullscreen=!fullscreen; // createGLWindow(640, 480, 16, fullscreen); // } // catch (Exception e) { // /* We dont want to put exceptions into our loop... so */ // e.printStackTrace(); // System.exit(1); // } } } else { keys.f1 = false; } if (Keyboard.isKeyDown(Keyboard.KEY_L)) { if (!keys.l) { keys.l = true; light=!light; if (!light) { gl.disable(GL.LIGHTING); } else { gl.enable(GL.LIGHTING); } } } else { keys.l = false; } if (Keyboard.isKeyDown(Keyboard.KEY_F)) { if (!keys.f) { keys.f = true; ... [truncated message content] |