Advertisement
Ineentho

Untitled

Aug 6th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.42 KB | None | 0 0
  1. /*
  2.  *      This Code Was Created By Jeff Molofee 2000
  3.  *      A HUGE Thanks To Fredric Echols For Cleaning Up
  4.  *      And Optimizing The Base Code, Making It More Flexible!
  5.  *      If You've Found This Code Useful, Please Let Me Know.
  6.  *      Visit My Site At nehe.gamedev.net
  7.  */
  8.  
  9. import org.lwjgl.opengl.Display;
  10. import org.lwjgl.opengl.DisplayMode;
  11. import org.lwjgl.opengl.GL11;
  12. import org.lwjgl.opengl.glu.GLU;
  13. import org.lwjgl.input.Keyboard;
  14.  
  15. /**
  16.  * @author Mark Bernard
  17.  * date:    16-Nov-2003
  18.  *
  19.  * Port of NeHe's Lesson 5 to LWJGL
  20.  * Title: 3D Shapes
  21.  * Uses version 0.8alpha of LWJGL http://www.lwjgl.org/
  22.  *
  23.  * Be sure that the LWJGL libraries are in your classpath
  24.  *
  25.  * Ported directly from the C++ version
  26.  *
  27.  * 2004-05-08: Updated to version 0.9alpha of LWJGL.
  28.  *             Changed from all static to all instance objects.
  29.  * 2004-09-22: Updated to version 0.92alpha of LWJGL.
  30.  */
  31. public class Lesson05 {
  32.     private boolean done = false;
  33.     private boolean fullscreen = false;
  34.     private final String windowTitle = "NeHe's OpenGL Lesson 5 for LWJGL (3D Shapes)";
  35.     private boolean f1 = false;
  36.  
  37.     private float rtri;                 // Angle For The Triangle ( NEW )
  38.     private float rquad;                // Angle For The Quad     ( NEW )
  39.     private DisplayMode displayMode;
  40.  
  41.     public static void main(String args[]) {
  42.         boolean fullscreen = false;
  43.         if(args.length>0) {
  44.             if(args[0].equalsIgnoreCase("fullscreen")) {
  45.                 fullscreen = true;
  46.             }
  47.         }
  48.  
  49.         Lesson05 l5 = new Lesson05();
  50.         l5.run(fullscreen);
  51.     }
  52.     public void run(boolean fullscreen) {
  53.         this.fullscreen = fullscreen;
  54.         try {
  55.             init();
  56.             while (!done) {
  57.                 mainloop();
  58.                 render();
  59.                 Display.update();
  60.             }
  61.             cleanup();
  62.         }
  63.         catch (Exception e) {
  64.             e.printStackTrace();
  65.             System.exit(0);
  66.         }
  67.     }
  68.     private void mainloop() {
  69.         if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {       // Exit if Escape is pressed
  70.             done = true;
  71.         }
  72.         if(Display.isCloseRequested()) {                     // Exit if window is closed
  73.             done = true;
  74.         }
  75.         if(Keyboard.isKeyDown(Keyboard.KEY_F1) && !f1) {    // Is F1 Being Pressed?
  76.             f1 = true;                                      // Tell Program F1 Is Being Held
  77.             switchMode();                                   // Toggle Fullscreen / Windowed Mode
  78.         }
  79.         if(!Keyboard.isKeyDown(Keyboard.KEY_F1)) {          // Is F1 Being Pressed?
  80.             f1 = false;
  81.         }
  82.     }
  83.  
  84.     private void switchMode() {
  85.         fullscreen = !fullscreen;
  86.         try {
  87.             Display.setFullscreen(fullscreen);
  88.         }
  89.         catch(Exception e) {
  90.             e.printStackTrace();
  91.         }
  92.     }
  93.  
  94.     private boolean render() {
  95.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);          // Clear The Screen And The Depth Buffer
  96.  
  97.         GL11.glLoadIdentity();                          // Reset The Current Modelview Matrix
  98.  
  99.         GL11.glTranslatef(-1.5f,0.0f,-6.0f);                // Move Left 1.5 Units And Into The Screen 6.0
  100.         GL11.glRotatef(rtri,0.0f,1.0f,0.0f);                // Rotate The Triangle On The Y axis ( NEW )
  101.         GL11.glBegin(GL11.GL_TRIANGLES);                    // Drawing Using Triangles
  102.             GL11.glColor3f(1.0f,0.0f,0.0f);             // Red
  103.             GL11.glVertex3f( 0.0f, 1.0f, 0.0f);         // Top Of Triangle (Front)
  104.             GL11.glColor3f(0.0f,1.0f,0.0f);             // Green
  105.             GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Left Of Triangle (Front)
  106.             GL11.glColor3f(0.0f,0.0f,1.0f);             // Blue
  107.             GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Right Of Triangle (Front)
  108.             GL11.glColor3f(1.0f,0.0f,0.0f);             // Red
  109.             GL11.glVertex3f( 0.0f, 1.0f, 0.0f);         // Top Of Triangle (Right)
  110.             GL11.glColor3f(0.0f,0.0f,1.0f);             // Blue
  111.             GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Left Of Triangle (Right)
  112.             GL11.glColor3f(0.0f,1.0f,0.0f);             // Green
  113.             GL11.glVertex3f( 1.0f,-1.0f, -1.0f);            // Right Of Triangle (Right)
  114.             GL11.glColor3f(1.0f,0.0f,0.0f);             // Red
  115.             GL11.glVertex3f( 0.0f, 1.0f, 0.0f);         // Top Of Triangle (Back)
  116.             GL11.glColor3f(0.0f,1.0f,0.0f);             // Green
  117.             GL11.glVertex3f( 1.0f,-1.0f, -1.0f);            // Left Of Triangle (Back)
  118.             GL11.glColor3f(0.0f,0.0f,1.0f);             // Blue
  119.             GL11.glVertex3f(-1.0f,-1.0f, -1.0f);            // Right Of Triangle (Back)
  120.             GL11.glColor3f(1.0f,0.0f,0.0f);             // Red
  121.             GL11.glVertex3f( 0.0f, 1.0f, 0.0f);         // Top Of Triangle (Left)
  122.             GL11.glColor3f(0.0f,0.0f,1.0f);             // Blue
  123.             GL11.glVertex3f(-1.0f,-1.0f,-1.0f);         // Left Of Triangle (Left)
  124.             GL11.glColor3f(0.0f,1.0f,0.0f);             // Green
  125.             GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Right Of Triangle (Left)
  126.         GL11.glEnd();                                       // Finished Drawing The Triangle
  127.  
  128.         GL11.glLoadIdentity();                          // Reset The Current Modelview Matrix
  129.         GL11.glTranslatef(1.5f,0.0f,-7.0f);             // Move Right 1.5 Units And Into The Screen 6.0
  130.         GL11.glRotatef(rquad,1.0f,1.0f,1.0f);               // Rotate The Quad On The X axis ( NEW )
  131.         GL11.glColor3f(0.5f,0.5f,1.0f);                 // Set The Color To Blue One Time Only
  132.         GL11.glBegin(GL11.GL_QUADS);                        // Draw A Quad
  133.             GL11.glColor3f(0.0f,1.0f,0.0f);             // Set The Color To Green
  134.             GL11.glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Top)
  135.             GL11.glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Top)
  136.             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);         // Bottom Left Of The Quad (Top)
  137.             GL11.glVertex3f( 1.0f, 1.0f, 1.0f);         // Bottom Right Of The Quad (Top)
  138.             GL11.glColor3f(1.0f,0.5f,0.0f);             // Set The Color To Orange
  139.             GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Top Right Of The Quad (Bottom)
  140.             GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Top Left Of The Quad (Bottom)
  141.             GL11.glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Bottom)
  142.             GL11.glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Bottom)
  143.             GL11.glColor3f(1.0f,0.0f,0.0f);             // Set The Color To Red
  144.             GL11.glVertex3f( 1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Front)
  145.             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Front)
  146.             GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Bottom Left Of The Quad (Front)
  147.             GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Bottom Right Of The Quad (Front)
  148.             GL11.glColor3f(1.0f,1.0f,0.0f);             // Set The Color To Yellow
  149.             GL11.glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Back)
  150.             GL11.glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Back)
  151.             GL11.glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Back)
  152.             GL11.glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Back)
  153.             GL11.glColor3f(0.0f,0.0f,1.0f);             // Set The Color To Blue
  154.             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Left)
  155.             GL11.glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Left)
  156.             GL11.glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Left)
  157.             GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Bottom Right Of The Quad (Left)
  158.             GL11.glColor3f(1.0f,0.0f,1.0f);             // Set The Color To Violet
  159.             GL11.glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Right)
  160.             GL11.glVertex3f( 1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Right)
  161.             GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Bottom Left Of The Quad (Right)
  162.             GL11.glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Right)
  163.         GL11.glEnd();                                       // Done Drawing The Quad
  164.  
  165.         rtri+=0.2f;                                     // Increase The Rotation Variable For The Triangle ( NEW )
  166.         rquad-=0.15f;                                   // Decrease The Rotation Variable For The Quad     ( NEW )
  167.         return true;
  168.     }
  169.     private void createWindow() throws Exception {
  170.         Display.setFullscreen(fullscreen);
  171.         DisplayMode d[] = Display.getAvailableDisplayModes();
  172.         for (int i = 0; i < d.length; i++) {
  173.             if (d[i].getWidth() == 640
  174.                 && d[i].getHeight() == 480
  175.                 && d[i].getBitsPerPixel() == 32) {
  176.                 displayMode = d[i];
  177.                 break;
  178.             }
  179.         }
  180.         Display.setDisplayMode(displayMode);
  181.         Display.setTitle(windowTitle);
  182.         Display.create();
  183.     }
  184.     private void init() throws Exception {
  185.         createWindow();
  186.  
  187.         initGL();
  188.     }
  189.  
  190.     private void initGL() {
  191.         GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
  192.         GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
  193.         GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
  194.         GL11.glClearDepth(1.0); // Depth Buffer Setup
  195.         GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
  196.         GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do
  197.  
  198.         GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
  199.         GL11.glLoadIdentity(); // Reset The Projection Matrix
  200.  
  201.         // Calculate The Aspect Ratio Of The Window
  202.         GLU.gluPerspective(
  203.           45.0f,
  204.           (float) displayMode.getWidth() / (float) displayMode.getHeight(),
  205.           0.1f,
  206.           100.0f);
  207.         GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
  208.  
  209.         // Really Nice Perspective Calculations
  210.         GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
  211.     }
  212.     private static void cleanup() {
  213.         Display.destroy();
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement