/* * This Code Was Created By Jeff Molofee 2000 * A HUGE Thanks To Fredric Echols For Cleaning Up * And Optimizing This Code, Making It More Flexible! * If You've Found This Code Useful, Please Let Me Know. * Visit My Site At nehe.gamedev.net */ import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.glu.GLU; import org.lwjgl.input.Keyboard; /** * @author Mark Bernard * date: 16-Nov-2003 * * Port of NeHe's Lesson 1 to LWJGL * Title: Setting Up An OpenGL Window * Uses version 0.8alpha of LWJGL http://www.lwjgl.org/ * * Be sure that the LWJGL libraries are in your classpath * * Ported directly from the C++ version * * 2004-05-08: Updated to version 0.9alpha of LWJGL. * Changed from all static to all instance objects. * 2004-09-21: Updated to version 0.92alpha of LWJGL. */ public class Lesson01 { private boolean done = false; private boolean fullscreen = false; private final String windowTitle = "NeHe's OpenGL Lesson 1 for LWJGL (Setting Up An OpenGL Window)"; private boolean f1 = false; private DisplayMode displayMode; /** * Everything starts and ends here. Takes 1 optional command line argument. * If fullscreen is specified on the command line then fullscreen is used, * otherwise windowed mode will be used. * @param args command line arguments */ public static void main(String args[]) { boolean fullscreen = false; if(args.length>0) { if(args[0].equalsIgnoreCase("fullscreen")) { fullscreen = true; } } Lesson01 l1 = new Lesson01(); l1.run(fullscreen); } /** * Launch point * @param fullscreen boolean value, set to true to run in fullscreen mode */ public void run(boolean fullscreen) { this.fullscreen = fullscreen; try { init(); while (!done) { mainloop(); render(); Display.update(); } cleanup(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } /** * All updating is done here. Key and mouse polling as well as window closing and * custom updates, such as AI. */ private void mainloop() { if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Exit if Escape is pressed done = true; } if(Display.isCloseRequested()) { // Exit if window is closed done = true; } if(Keyboard.isKeyDown(Keyboard.KEY_F1) && !f1) { // Is F1 Being Pressed? f1 = true; // Tell Program F1 Is Being Held switchMode(); // Toggle Fullscreen / Windowed Mode } if(!Keyboard.isKeyDown(Keyboard.KEY_F1)) { // Is F1 Being Pressed? f1 = false; } } private void switchMode() { fullscreen = !fullscreen; try { Display.setFullscreen(fullscreen); } catch(Exception e) { e.printStackTrace(); } } /** * For rendering all objects to the screen * @return boolean for success or not */ private boolean render() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer GL11.glLoadIdentity(); // Reset The Current Modelview Matrix return true; } /** * Create a window depending on whether fullscreen is selected * @throws Exception Throws the Window.create() exception up the stack. */ private void createWindow() throws Exception { Display.setFullscreen(fullscreen); DisplayMode d[] = Display.getAvailableDisplayModes(); for (int i = 0; i < d.length; i++) { if (d[i].getWidth() == 640 && d[i].getHeight() == 480 && d[i].getBitsPerPixel() == 32) { displayMode = d[i]; break; } } Display.setDisplayMode(displayMode); Display.setTitle(windowTitle); Display.create(); } /** * Do all initilization code here. Including Keyboard and OpenGL * @throws Exception Passes any exceptions up to the main loop to be handled */ private void init() throws Exception { createWindow(); initGL(); } /** * Initialize OpenGL * */ private void initGL() { GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background GL11.glClearDepth(1.0); // Depth Buffer Setup GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix GL11.glLoadIdentity(); // Reset The Projection Matrix // Calculate The Aspect Ratio Of The Window GLU.gluPerspective( 45.0f, (float) displayMode.getWidth() / (float) displayMode.getHeight(), 0.1f, 100.0f); GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix // Really Nice Perspective Calculations GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); } /** * Cleanup all the resources. * */ private void cleanup() { Display.destroy(); } }