From: Luke H. <lh...@us...> - 2002-11-27 05:54:30
|
Update of /cvsroot/java-game-lib/LWJGL/examples In directory sc8-pr-cvs1:/tmp/cvs-serv29807/examples Added Files: Quadrics.java Log Message: GLU: Added full quadrics support (except gluQuadricCallback) Quadrics.java: port of NeHe lesson18: Quadrics --- NEW FILE: Quadrics.java --- CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/Quadrics.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: Quadrics.java,v 1.1 2002/11/27 05:54:27 lholden Exp $ * * Quadrics * * Credit goes to GB Schmick (TipTup) and Jeff Molofee (NeHe) whos tutorial this is based on * * @author Luke Holden * @version $Revision: 1.1 $ */ public class Quadrics extends FiltersLightingAndControl { /* Lighting ON / OFF */ private boolean light; /* Start of Disc */ private int part1; /* End of Disc */ private int part2; /* Increase */ private int p1 = 10; private int p2 = 10; private float xrot; private float yrot; private float xspeed; private float yspeed; private float z=-5.0f; /* Storage for our quadric objects */ private int quadric; /* 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; /* Which object to draw */ private int object = 0; /* 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 space = false; } private KeyToggle keys = new KeyToggle(); /** Creates a new instance of Quadrics */ public Quadrics() { } /* 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/wall.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(); /* Create a pointer to the quadric object */ quadric = glu.newQuadric(); glu.quadricNormals(quadric, GLU.SMOOTH); glu.quadricTexture(quadric, true); } catch (Exception e) { throw new Exception("Problem initialising GL", e); } } public void drawCube() { gl.begin(GL.QUADS); /* Front Face */ gl.normal3f( 0.0f, 0.0f, 1.0f); gl.texCoord2f(0.0f, 0.0f); gl.vertex3f(-1.0f, -1.0f, 1.0f); gl.texCoord2f(1.0f, 0.0f); gl.vertex3f( 1.0f, -1.0f, 1.0f); gl.texCoord2f(1.0f, 1.0f); gl.vertex3f( 1.0f, 1.0f, 1.0f); gl.texCoord2f(0.0f, 1.0f); gl.vertex3f(-1.0f, 1.0f, 1.0f); /* Back Face */ gl.normal3f( 0.0f, 0.0f,-1.0f); gl.texCoord2f(1.0f, 0.0f); gl.vertex3f(-1.0f, -1.0f, -1.0f); gl.texCoord2f(1.0f, 1.0f); gl.vertex3f(-1.0f, 1.0f, -1.0f); gl.texCoord2f(0.0f, 1.0f); gl.vertex3f( 1.0f, 1.0f, -1.0f); gl.texCoord2f(0.0f, 0.0f); gl.vertex3f( 1.0f, -1.0f, -1.0f); /* Top Face */ gl.normal3f( 0.0f, 1.0f, 0.0f); gl.texCoord2f(0.0f, 1.0f); gl.vertex3f(-1.0f, 1.0f, -1.0f); gl.texCoord2f(0.0f, 0.0f); gl.vertex3f(-1.0f, 1.0f, 1.0f); gl.texCoord2f(1.0f, 0.0f); gl.vertex3f( 1.0f, 1.0f, 1.0f); gl.texCoord2f(1.0f, 1.0f); gl.vertex3f( 1.0f, 1.0f, -1.0f); /* Bottom Face */ gl.normal3f( 0.0f,-1.0f, 0.0f); gl.texCoord2f(1.0f, 1.0f); gl.vertex3f(-1.0f, -1.0f, -1.0f); gl.texCoord2f(0.0f, 1.0f); gl.vertex3f( 1.0f, -1.0f, -1.0f); gl.texCoord2f(0.0f, 0.0f); gl.vertex3f( 1.0f, -1.0f, 1.0f); gl.texCoord2f(1.0f, 0.0f); gl.vertex3f(-1.0f, -1.0f, 1.0f); /* Right face */ gl.normal3f( 1.0f, 0.0f, 0.0f); gl.texCoord2f(1.0f, 0.0f); gl.vertex3f( 1.0f, -1.0f, -1.0f); gl.texCoord2f(1.0f, 1.0f); gl.vertex3f( 1.0f, 1.0f, -1.0f); gl.texCoord2f(0.0f, 1.0f); gl.vertex3f( 1.0f, 1.0f, 1.0f); gl.texCoord2f(0.0f, 0.0f); gl.vertex3f( 1.0f, -1.0f, 1.0f); /* Left Face */ gl.normal3f(-1.0f, 0.0f, 0.0f); gl.texCoord2f(0.0f, 0.0f); gl.vertex3f(-1.0f, -1.0f, -1.0f); gl.texCoord2f(1.0f, 0.0f); gl.vertex3f(-1.0f, -1.0f, 1.0f); gl.texCoord2f(1.0f, 1.0f); gl.vertex3f(-1.0f, 1.0f, 1.0f); gl.texCoord2f(0.0f, 1.0f); gl.vertex3f(-1.0f, 1.0f, -1.0f); gl.end(); } protected boolean drawGLScene(float frameTime) { gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT); gl.loadIdentity(); gl.translatef(0.0f, 0.0f, z); gl.rotatef(xrot,1.0f,0.0f,0.0f); gl.rotatef(yrot,0.0f,1.0f,0.0f); gl.bindTexture(GL.TEXTURE_2D, textureBuf.get(filter)); switch(object) { case 0: drawCube(); break; case 1: gl.translatef(0.0f, 0.0f, -1.5f); glu.cylinder(quadric, 1.0f, 1.0f, 3.0f, 32, 32); break; case 2: glu.disk(quadric, 0.5f, 1.5f, 32, 32); break; case 3: glu.sphere(quadric, 1.3f, 32, 32); break; case 4: gl.translatef(0.0f, 0.0f, -1.5f); glu.cylinder(quadric, 1.0f, 0.0f, 3.0f, 32, 32); break; case 5: part1+=(frameTime * p1); part2+=(frameTime * p2); if(part1>359) { p1=0; part1=0; p2=1; part2=0; } if(part2>359) { p1=1; p2=0; } glu.partialDisk(quadric, 0.5f, 1.5f, 32, 32, part1, part2-part1); break; }; /* X Axis Rotation */ xrot+=(frameTime * xspeed); /* Y Axis Rotation */ yrot+=(frameTime * yspeed); return true; } protected void killGLWindow() { glu.deleteQuadric(quadric); super.killGLWindow(); } 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_SPACE)) { if (!keys.space) { keys.space = true; object++; if (object > 5) { object = 0; } } } else { keys.space = 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; Quadrics lesson = new Quadrics(); try { lesson.start(640, 480, 16, false); } catch (Exception e) { err = 1; e.printStackTrace(); } System.exit(err); } } |