Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This Code Was Created By Jeff Molofee 2000
- * A HUGE Thanks To Fredric Echols For Cleaning Up
- * And Optimizing The Base 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 5 to LWJGL
- * Title: 3D Shapes
- * 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-22: Updated to version 0.92alpha of LWJGL.
- */
- public class Lesson05 {
- private boolean done = false;
- private boolean fullscreen = false;
- private final String windowTitle = "NeHe's OpenGL Lesson 5 for LWJGL (3D Shapes)";
- private boolean f1 = false;
- private float rtri; // Angle For The Triangle ( NEW )
- private float rquad; // Angle For The Quad ( NEW )
- private DisplayMode displayMode;
- public static void main(String args[]) {
- boolean fullscreen = false;
- if(args.length>0) {
- if(args[0].equalsIgnoreCase("fullscreen")) {
- fullscreen = true;
- }
- }
- Lesson05 l5 = new Lesson05();
- l5.run(fullscreen);
- }
- 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);
- }
- }
- 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();
- }
- }
- 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
- GL11.glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
- GL11.glRotatef(rtri,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW )
- GL11.glBegin(GL11.GL_TRIANGLES); // Drawing Using Triangles
- GL11.glColor3f(1.0f,0.0f,0.0f); // Red
- GL11.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
- GL11.glColor3f(0.0f,1.0f,0.0f); // Green
- GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
- GL11.glColor3f(0.0f,0.0f,1.0f); // Blue
- GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)
- GL11.glColor3f(1.0f,0.0f,0.0f); // Red
- GL11.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
- GL11.glColor3f(0.0f,0.0f,1.0f); // Blue
- GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)
- GL11.glColor3f(0.0f,1.0f,0.0f); // Green
- GL11.glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right)
- GL11.glColor3f(1.0f,0.0f,0.0f); // Red
- GL11.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
- GL11.glColor3f(0.0f,1.0f,0.0f); // Green
- GL11.glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back)
- GL11.glColor3f(0.0f,0.0f,1.0f); // Blue
- GL11.glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back)
- GL11.glColor3f(1.0f,0.0f,0.0f); // Red
- GL11.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
- GL11.glColor3f(0.0f,0.0f,1.0f); // Blue
- GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)
- GL11.glColor3f(0.0f,1.0f,0.0f); // Green
- GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)
- GL11.glEnd(); // Finished Drawing The Triangle
- GL11.glLoadIdentity(); // Reset The Current Modelview Matrix
- GL11.glTranslatef(1.5f,0.0f,-7.0f); // Move Right 1.5 Units And Into The Screen 6.0
- GL11.glRotatef(rquad,1.0f,1.0f,1.0f); // Rotate The Quad On The X axis ( NEW )
- GL11.glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only
- GL11.glBegin(GL11.GL_QUADS); // Draw A Quad
- GL11.glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
- GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
- GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
- GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
- GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
- GL11.glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
- GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
- GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
- GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
- GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
- GL11.glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
- GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
- GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
- GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
- GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
- GL11.glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
- GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back)
- GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back)
- GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back)
- GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back)
- GL11.glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
- GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
- GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
- GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
- GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
- GL11.glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
- GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
- GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
- GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
- GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
- GL11.glEnd(); // Done Drawing The Quad
- rtri+=0.2f; // Increase The Rotation Variable For The Triangle ( NEW )
- rquad-=0.15f; // Decrease The Rotation Variable For The Quad ( NEW )
- return true;
- }
- 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();
- }
- private void init() throws Exception {
- createWindow();
- initGL();
- }
- 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);
- }
- private static void cleanup() {
- Display.destroy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement