Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.37 KB | None | 0 0
  1.  
  2.  
  3. import java.nio.*;
  4.  
  5. import org.lwjgl.BufferUtils;
  6. import org.lwjgl.Sys;
  7. import org.lwjgl.input.Keyboard;
  8. import org.lwjgl.input.Mouse;
  9. import org.lwjgl.opengl.Display;
  10. import org.lwjgl.opengl.DisplayMode;
  11. import org.lwjgl.opengl.GL11;
  12. import org.lwjgl.util.glu.*;
  13. import org.lwjgl.util.vector.Quaternion;
  14. import org.lwjgl.util.vector.Vector3f;
  15.  
  16. /**
  17.  * Asteroid game
  18.  *
  19.  * @author Akuryo <cogney.maxime@gmail.com>
  20.  * @author Sporbie <sporbie@gmail.com>
  21.  * @version 0.0
  22.  */
  23.  
  24. public class OpenClustar {
  25.  
  26.     public static final String GAME_TITLE = "Asteroid";
  27.     private static final int FRAMERATE = 60;
  28.     private static boolean finished;
  29.     private static Vector3f pos = new Vector3f(0f,0f,0f);
  30.     private static Vector3f rot = new Vector3f(1f,0f,0f);
  31.     private static Quaternion q = new Quaternion();
  32.  
  33.     public static void main(String[] args) {
  34.        
  35.         try {
  36.             init(false);
  37.             run();
  38.         } catch (Exception e) {
  39.             e.printStackTrace(System.err);
  40.             Sys.alert(GAME_TITLE, "An error occured and the game will exit.");
  41.         } finally {
  42.             cleanup();
  43.         }
  44.         System.exit(0);
  45.     }
  46.  
  47.     private static void render() {
  48.        
  49.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clearing up the screen and depth buffer
  50.         GL11.glLoadIdentity(); // Resetting the matrix (so X is left-right, Y up-down and Z front-behind)
  51.        
  52.  
  53.         float[] rr=GetMatrix(q);
  54.        
  55.         FloatBuffer br=BufferUtils.createFloatBuffer(16*4);
  56.        
  57.         br.put(rr);
  58.        
  59.        
  60.        
  61.         /*GL11.glRotatef(rot.y, 0f, 1.0f, 0f);
  62.         GL11.glRotatef(rot.x, 1.0f, 0f, 0f);
  63.         GL11.glRotatef(rot.z, 0f, 0f, 1.0f);*/
  64.         GL11.glLoadMatrix(br);
  65.         GL11.glTranslatef(-pos.x,-pos.y,-pos.z);
  66.        
  67.         GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(1f, 0f, 0f);
  68.             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
  69.             GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
  70.             GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
  71.             GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
  72.         GL11.glEnd();
  73.         GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(0f, 1f, 0f);
  74.             GL11.glVertex3f(-1.0f, 1.0f,-1.0f);
  75.             GL11.glVertex3f( 1.0f, 1.0f,-1.0f);
  76.             GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
  77.             GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
  78.         GL11.glEnd();
  79.         GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(0f, 0f, 1f);
  80.             GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
  81.             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
  82.             GL11.glVertex3f(-1.0f, 1.0f,-1.0f);
  83.             GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
  84.         GL11.glEnd();
  85.         GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(1f, 1f, 0f);
  86.             GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
  87.             GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
  88.             GL11.glVertex3f( 1.0f, 1.0f,-1.0f);
  89.             GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
  90.         GL11.glEnd();
  91.        
  92.  
  93.        
  94.     }
  95.  
  96.     /**
  97.      * Initialise the game
  98.      * @throws Exception if init fails
  99.      */
  100.     private static void init(boolean fullscreen) throws Exception {
  101.         // Create a fullscreen window with 1:1 orthographic 2D projection (default)
  102.         Display.setTitle(GAME_TITLE);
  103.         Display.setDisplayMode(new DisplayMode(800, 600));
  104.         Display.setFullscreen(fullscreen);
  105.         Display.setVSyncEnabled(true);
  106.         Display.create();
  107.  
  108.         GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
  109.         GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
  110.         GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
  111.         GL11.glClearDepth(1.0); // Depth Buffer Setup
  112.         GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
  113.         GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do
  114.  
  115.         GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
  116.         GL11.glLoadIdentity(); // Reset The Projection Matrix
  117.  
  118.         // Calculate The Aspect Ratio Of The Window
  119.         GLU.gluPerspective(
  120.                 45.0f,
  121.                 Display.getDisplayMode().getWidth() / Display.getDisplayMode().getHeight(),
  122.                 0.1f,
  123.                 100.0f);
  124.         GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
  125.  
  126.         // Really Nice Perspective Calculations
  127.         GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
  128.  
  129.         Mouse.create();
  130.         Mouse.setGrabbed(true);
  131.        
  132.         GL11.glPushMatrix();
  133.     }
  134.  
  135.     private static void run() {
  136.  
  137.         while (!finished) {
  138.             // Always call Window.update(), all the time - it does some behind the
  139.             // scenes work, and also displays the rendered output
  140.             Display.update();
  141.  
  142.             // Check for close requests
  143.             if (Display.isCloseRequested()) {
  144.                 finished = true;
  145.             }
  146.  
  147.             // The window is in the foreground, so we should play the game
  148.             else if (Display.isActive()) {
  149.                 logic();
  150.                 render();
  151.                 Display.sync(FRAMERATE);
  152.             }
  153.  
  154.             // The window is not in the foreground, so we can allow other stuff to run and
  155.             // infrequently update
  156.             else {
  157.                 try {
  158.                     Thread.sleep(100);
  159.                 } catch (InterruptedException e) {
  160.                 }
  161.                 logic();
  162.  
  163.                 // Only bother rendering if the window is visible or dirty
  164.                 if (Display.isVisible() || Display.isDirty()) {
  165.                     render();
  166.                 }
  167.             }
  168.         }
  169.     }
  170.  
  171.    
  172.     private static Quaternion FromAxis(float x, float y, float z, float deg)
  173.     {
  174.         Quaternion qat = new Quaternion();
  175.         float angle = (float)((deg / 180.0f) * 3.14159265f);
  176.  
  177.         // Here we calculate the sin( theta / 2) once for optimization
  178.         float result = (float)Math.sin( angle / 2.0f );
  179.  
  180.         // Calcualte the w value by cos( theta / 2 )
  181.         qat.w = (float)Math.cos( angle / 2.0f );
  182.  
  183.         // Calculate the x, y and z of the quaternion
  184.         qat.x = (float)(x * result);
  185.         qat.y = (float)(y * result);
  186.         qat.z = (float)(z * result);
  187.        
  188.         return qat;
  189.     }
  190.    
  191.     private static float[] GetMatrix (Quaternion q)
  192.     {
  193.         float pMatrix[]=new float[16];
  194.         pMatrix[ 0] = 1.0f - 2.0f * ( q.y * q.y + q.z * q.z );
  195.         pMatrix[ 1] = 2.0f * (q.x * q.y + q.z * q.w);
  196.         pMatrix[ 2] = 2.0f * (q.x * q.z - q.y * q.w);
  197.         pMatrix[ 3] = 0.0f;
  198.        
  199.         // Second row
  200.         pMatrix[ 4] = 2.0f * ( q.x * q.y - q.z * q.w );
  201.         pMatrix[ 5] = 1.0f - 2.0f * ( q.x * q.x + q.z * q.z );
  202.         pMatrix[ 6] = 2.0f * (q.z * q.y + q.x * q.w );
  203.         pMatrix[ 7] = 0.0f;
  204.  
  205.         // Third row
  206.         pMatrix[ 8] = 2.0f * ( q.x * q.z + q.y * q.w );
  207.         pMatrix[ 9] = 2.0f * ( q.y * q.z - q.x * q.w );
  208.         pMatrix[10] = 1.0f - 2.0f * ( q.x * q.x + q.y * q.y );
  209.         pMatrix[11] = 0.0f;
  210.  
  211.         // Fourth row
  212.         pMatrix[12] = 0;
  213.         pMatrix[13] = 0;
  214.         pMatrix[14] = 0;
  215.         pMatrix[15] = 1.0f;
  216.  
  217.         return pMatrix;
  218.        
  219.     }
  220.     /**
  221.      * Do any game-specific cleanup
  222.      */
  223.     private static void cleanup() {
  224.         // Close the window
  225.         Display.destroy();
  226.     }
  227.  
  228.     /**
  229.      * Do all calculations, handle input, etc.
  230.      */
  231.     private static void logic() {
  232.        
  233.         // Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
  234.         if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
  235.             finished = true;
  236.         }
  237.        
  238.         float speed = 0.2f;
  239.        
  240.         /*if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
  241.             pos.z+=speed*Math.sin((rot.x*3.14)/180)*Math.cos((rot.z*3.14)/180);
  242.             pos.x+=speed*Math.cos((rot.x*3.14)/180)*Math.cos((rot.z*3.14)/180);
  243.             pos.y+=speed*Math.sin(-(rot.z*3.14)/180);
  244.         }
  245.         if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
  246.             pos.z-=speed*Math.sin((rot.x*3.14)/180)*Math.cos(-(rot.z*3.14)/180);
  247.             pos.x-=speed*Math.cos((rot.x*3.14)/180)*Math.cos(-(rot.z*3.14)/180);
  248.             pos.y-=speed*Math.sin(-(rot.z*3.14)/180);
  249.         }
  250.         if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
  251.             pos.z+=speed*Math.cos(-(rot.x*3.14)/180)*Math.cos((rot.y*3.14)/180);
  252.             pos.x+=speed*Math.sin(-(rot.x*3.14)/180)*Math.cos((rot.y*3.14)/180);
  253.             pos.y+=speed*Math.sin(-(rot.y*3.14)/180);
  254.         }
  255.         if (Keyboard.isKeyDown(Keyboard.KEY_Z)) {
  256.             pos.z-=speed*Math.cos(-(rot.x*3.14)/180)*Math.cos(-(rot.y*3.14)/180);
  257.             pos.x-=speed*Math.sin(-(rot.x*3.14)/180)*Math.cos(-(rot.y*3.14)/180);
  258.             pos.y-=speed*Math.sin(-(rot.y*3.14)/180);
  259.         }*/
  260.         if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
  261.             pos.x+=speed;
  262.         }
  263.         if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
  264.             pos.x-=speed;
  265.         }
  266.         if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
  267.             pos.z+=speed;
  268.         }
  269.         if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
  270.             pos.z-=speed;
  271.         }
  272.         if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
  273.             rot.z-=2;
  274.         }
  275.         if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
  276.             rot.z+=2;
  277.         }
  278.         float DY = Mouse.getDX()/2.0f;
  279.         float DX = Mouse.getDY()/2.0f;
  280.  
  281.         rot.x-=speed*DX;
  282.         rot.y-=-speed*DY;
  283.         Quaternion q1=FromAxis(1.f,0.f,0.f,rot.x);
  284.         Quaternion q2=FromAxis(0.f,1.f,0.f,rot.y);
  285.         Quaternion q3=FromAxis(0.f,0.f,1.f,rot.z);
  286.         q1.normalise();
  287.         q2.normalise();
  288.         q3.normalise();
  289.         Quaternion qq=new Quaternion();
  290.         Quaternion.mul(q2, q3, qq);
  291.         Quaternion.mul(q1,qq,q);
  292.         q.normalise();
  293.         Mouse.setCursorPosition(Display.getDisplayMode().getWidth()/2, Display.getDisplayMode().getHeight()/2);
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement