From: Luke H. <lh...@us...> - 2002-11-25 06:24:12
|
Update of /cvsroot/java-game-lib/LWJGL/examples/nehe/lesson03 In directory sc8-pr-cvs1:/tmp/cvs-serv2578/nehe/lesson03 Added Files: Lesson3.java Log Message: initial commit --- NEW FILE: Lesson3.java --- CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/examples/nehe/lesson03/Lesson3.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: Lesson3.java,v 1.1 2002/11/25 06:24:09 lholden Exp $ * * Adding Color * * Credit goes to Jeff Molofee (NeHe) whos tutorial this is based on * * @author Luke Holden * @version $Revision: 1.1 $ */ public class Lesson3 { private GL gl; private GLU glu; private boolean done = false; private boolean fullscreen = true; /** Creates a new instance of Lesson */ public Lesson3() { } 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 boolean initGL() { /* 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); return true; } 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(); /* Move Left 1.5 Units And Into The Screen 6.0 */ gl.translatef(-1.5f,0.0f,-6.0f); /* Draw A Colored Triangle */ gl.begin(GL.TRIANGLES); /* Top - red */ gl.color3f(1.0f,0.0f,0.0f); gl.vertex3f( 0.0f, 1.0f, 0.0f); /* Bottom Left - green */ gl.color3f(0.0f,1.0f,0.0f); gl.vertex3f(-1.0f,-1.0f, 0.0f); /* Bottom Right - blue */ gl.color3f(0.0f,0.0f,1.0f); gl.vertex3f( 1.0f,-1.0f, 0.0f); gl.end(); /* Move Right 3 Units */ gl.translatef(3.0f,0.0f,0.0f); /* Draw A Blue Quad*/ gl.color3f(0.5f,0.5f,1.0f); gl.begin(GL.QUADS); /* Top Left */ gl.vertex3f(-1.0f, 1.0f, 0.0f); /* Top Right */ gl.vertex3f( 1.0f, 1.0f, 0.0f); /* Bottom Right */ gl.vertex3f( 1.0f,-1.0f, 0.0f); /* Bottom Left */ gl.vertex3f(-1.0f,-1.0f, 0.0f); gl.end(); 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 toggle effect */ Keyboard.read(); for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) { Keyboard.next(); if (Keyboard.key == Keyboard.KEY_ESCAPE && Keyboard.state) { done = true; } } } public static void main(String[] arguments) { int err = 0; Lesson3 lesson = new Lesson3(); try { lesson.start(); } catch (Exception e) { err = 1; e.printStackTrace(); } System.exit(err); } } |