Advertisement
FoxyCorndog

LWJGL Testing

Mar 12th, 2012
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.54 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.ByteBuffer;
  5. import java.nio.FloatBuffer;
  6. import java.nio.IntBuffer;
  7.  
  8. import java.awt.image.DataBufferByte;
  9.  
  10. import javax.imageio.ImageIO;
  11.  
  12. import net.foxycorndog.presto3d.graphics.PrestoGL;
  13.  
  14. import org.lwjgl.LWJGLException;
  15. import org.lwjgl.input.Keyboard;
  16. import org.lwjgl.input.Mouse;
  17. import org.lwjgl.opengl.Display;
  18. import org.lwjgl.opengl.DisplayMode;
  19. import org.lwjgl.util.glu.Sphere;
  20. import org.lwjgl.util.vector.Vector3f;
  21.  
  22. import org.lwjgl.BufferUtils;
  23.  
  24. import static org.lwjgl.util.glu.GLU.gluPerspective;
  25. import static org.lwjgl.opengl.GL11.*;
  26. import static org.lwjgl.opengl.GL15.*;
  27.  
  28. public class Main
  29. {
  30.     Vector3f rotation = new Vector3f(0, 0, 0);
  31.     Vector3f offset   = new Vector3f(0, 0, 0);
  32.     Vector3f position = new Vector3f(0, 0, 0);
  33.    
  34.     Vector3f cubes[] = new Vector3f[10000];
  35.    
  36.     FloatBuffer matSpecular;
  37.     FloatBuffer lightPosition;
  38.     FloatBuffer whiteLight;
  39.     FloatBuffer lModelAmbient;
  40.    
  41.     private int fps;
  42.    
  43.     private long oldTime, newTime;
  44.    
  45.     float walkingSpeed = 10;
  46.    
  47.     public static void main(String[] argv)
  48.     {
  49.         Main displayExample = new Main();
  50.         displayExample.start();
  51.     }
  52.    
  53.     public void start()
  54.     {
  55.         try
  56.         {
  57.             Display.setDisplayMode(new DisplayMode(640,512));
  58.             Display.setVSyncEnabled(true);
  59.             Display.create();
  60.         }
  61.         catch (LWJGLException e)
  62.         {
  63.             e.printStackTrace();
  64.             System.exit(0);
  65.         }
  66.        
  67. //      glMatrixMode(GL_PROJECTION);
  68. //      glLoadIdentity();
  69. //      //glOrtho(0, 640, 512, 0, -1, 40);
  70. //      gluPerspective((float) 30, 640f / 512f, 0.001f, -1);
  71. //      glMatrixMode(GL_MODELVIEW);
  72.        
  73.        
  74.        
  75.        
  76.         glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
  77.         glShadeModel(GL_SMOOTH); // Enable Smooth Shading
  78.         glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
  79.         glClearDepth(1.0); // Depth Buffer Setup
  80.         glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  81.         glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
  82.  
  83.         glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
  84.         glLoadIdentity(); // Reset The Projection Matrix
  85.  
  86.         // Calculate The Aspect Ratio Of The Window
  87.         gluPerspective(45.0f, (float)Display.getWidth() / (float)Display.getHeight(), 0.01f, -1.0f);
  88. //      glOrtho(1, 1, 1, 1, 1, -1);
  89.         glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
  90.  
  91.         // Really Nice Perspective Calculations
  92.         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  93.         glPointSize(8.0f);
  94.         glLineWidth(2.0f);
  95.        
  96.         glEnable(GL_CULL_FACE);
  97.        
  98.        
  99.        
  100.         //----------- Variables & method calls added for Lighting Test -----------//
  101.         initLightArrays();
  102.         glShadeModel(GL_SMOOTH);
  103.         glMaterial(GL_FRONT, GL_SPECULAR, matSpecular);             // sets specular material color
  104.         glMaterialf(GL_FRONT, GL_SHININESS, 50.0f);                 // sets shininess
  105.        
  106.         glLight(GL_LIGHT0, GL_POSITION, lightPosition);             // sets light position
  107.         glLight(GL_LIGHT0, GL_SPECULAR, whiteLight);                // sets specular light to white
  108.         glLight(GL_LIGHT0, GL_DIFFUSE, whiteLight);                 // sets diffuse light to white
  109.         glLightModel(GL_LIGHT_MODEL_AMBIENT, lModelAmbient);        // global ambient light
  110.        
  111.         glEnable(GL_LIGHTING);                                      // enables lighting
  112.         glEnable(GL_LIGHT0);                                        // enables light0
  113.        
  114.         glEnable(GL_COLOR_MATERIAL);                                // enables opengl to use glColor3f to define material color
  115.         glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);          // tell opengl glColor3f effects the ambient and diffuse properties of material
  116.         //----------- END: Variables & method calls added for Lighting Test -----------//
  117.  
  118.        
  119.         for (int i = 0; i < cubes.length; i ++)
  120.         {
  121.             cubes[i] = new Vector3f((int)((Math.random() * 200) - 100) * 10, (int)((Math.random() * 10) - 5) * 10, (int)((Math.random() * 200) - 100) * 10);
  122.         }
  123.        
  124. //      int cube = glGenLists(1);
  125. //      glNewList(cube, GL_COMPILE);
  126. //     
  127. //      PrestoGL.drawCube(10, 10, -100, 10);
  128. //     
  129. //      glEndList();
  130.        
  131.         final int amountOfVertices = 6 * 4;
  132.         final int vertexSize = 3;
  133. //      final int colorSize = 3;
  134.        
  135.         FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
  136.         vertexData.put(PrestoGL.getCubeArray(0, 0, -50, 10));
  137.         vertexData.flip();
  138.        
  139. //      FloatBuffer colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize);
  140. //      colorData.put(new float[]{1, 0, 0, 0, 1, 0, 0, 0, 1});
  141. //      colorData.flip();
  142.        
  143.         int vboVertexHandle = glGenBuffers();
  144.         glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
  145.         glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW);
  146.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  147.        
  148. //      int vboColorHandle = glGenBuffers();
  149. //      glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
  150. //      glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
  151. //      glBindBuffer(GL_ARRAY_BUFFER, 0);
  152.        
  153.        
  154.         newTime = System.currentTimeMillis();
  155.         oldTime = System.currentTimeMillis();
  156.        
  157.         float speed = 0;
  158.          
  159.         // init OpenGL here
  160.         while (!Display.isCloseRequested())
  161.         {
  162.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  163.            
  164.             updateFps();
  165.            
  166. //          glBegin(GL_QUADS);
  167. //         
  168. //              glColor3f(1, 0, 0);
  169. //             
  170. //              glVertex3i(0, 0, -90);
  171. //             
  172. //              glColor3f(0, 0, 0);
  173. //             
  174. //              glVertex3i(10, 0, -90);
  175. //             
  176. //              glColor3f(0, 1, 0);
  177. //             
  178. //              glVertex3i(10, 10, -50);
  179. //             
  180. //              glColor3f(0, 0, 8);
  181. //             
  182. //              glVertex3i(0, 10, -50);
  183. //         
  184. //          glEnd();
  185.            
  186. //          glLoadIdentity();
  187.  
  188.            
  189.            
  190.             glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
  191.             glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
  192.            
  193. //            glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
  194. //            glColorPointer(colorSize, GL_FLOAT, 0, 0L);
  195.            
  196.             glEnableClientState(GL_VERTEX_ARRAY);
  197. //            glEnableClientState(GL_COLOR_ARRAY);
  198.             glDrawArrays(GL_QUADS, 0, amountOfVertices);
  199. //            glDisableClientState(GL_COLOR_ARRAY);
  200.             glDisableClientState(GL_VERTEX_ARRAY);
  201.            
  202.            
  203.            
  204.             glRotatef(rotation.x, 1f, 0f, 0f);
  205.             glRotatef(rotation.y, 0f, 1f, 0f);
  206.             glRotatef(rotation.z, 0f, 0f, 1f);
  207.            
  208.             float testx = (float)Math.cos(rotation.x) + offset.x;
  209. //          float testy = (float)Math.cos(rotation.y) + offset.y;
  210.             float testz = (float)Math.cos(rotation.z) + offset.z;
  211.            
  212. //          float testx = offset.x;
  213. //          float testy = offset.y;
  214. //          float testz = offset.z;
  215.            
  216.             move(position.x, position.y, position.z);
  217.            
  218.             glTranslatef(offset.x, offset.y, offset.z);
  219.            
  220. //          int cube = glGenLists(1);
  221. //          glNewList(cube, GL_COMPILE);
  222. //         
  223. //          PrestoGL.drawCube(10, 10, -100, 10);
  224. //         
  225. //          glEndList();
  226.            
  227. //          for (Vector3f vec : cubes)
  228. //          {
  229. //              cube = glGenLists(1);
  230. //              glNewList(cube, GL_COMPILE);
  231. //             
  232. //              PrestoGL.drawCube(vec.x, vec.y, vec.z, 10);
  233. //             
  234. //              glEndList();
  235. //          }
  236.            
  237.            
  238. //          int maxLookDown = 80;
  239. //          int maxLookUp = -80;
  240.            
  241.             float mouseDX = Mouse.getDX() * 1 * 0.0016f;
  242.             float mouseDY = Mouse.getDY() * 1 * 0.0016f;
  243.            
  244. //////////////////////////////////////////////////////////////////////////
  245.            
  246.             if (rotation.y + mouseDX >= 360)
  247.             {
  248.                 rotation.y = rotation.y + mouseDX - 360;
  249.             }
  250.             else if (rotation.y + mouseDX < 0)
  251.             {
  252.                 rotation.y = 360 - rotation.y + mouseDX;
  253.             }
  254.             else
  255.             {
  256.                 rotation.y += mouseDX;
  257.             }
  258.            
  259. //////////////////////////////////////////////////////////////////////////
  260.            
  261.             if (rotation.x + -mouseDY >= 360)
  262.             {
  263.                 rotation.x = rotation.x + -mouseDY - 360;
  264.             }
  265.             else if (rotation.x + -mouseDY < 0)
  266.             {
  267.                 rotation.x = 360 - rotation.x + -mouseDY;
  268.             }
  269.             else
  270.             {
  271.                 rotation.x += -mouseDY;
  272.             }
  273.          
  274. //////////////////////////////////////////////////////////////////////////
  275.            
  276. //          if (rotation.x - mouseDY >= maxLookDown && rotation.x - mouseDY <= maxLookUp)
  277. //          {
  278. //              rotation.x += -mouseDY;
  279. //          }
  280. //          else if (rotation.x - mouseDY < maxLookDown)
  281. //          {
  282. //              rotation.x = maxLookDown;
  283. //          }
  284. //          else if (rotation.x - mouseDY > maxLookUp)
  285. //          {
  286. //              rotation.x = maxLookUp;
  287. //          }
  288.            
  289.            
  290.            
  291.            
  292.             boolean keyUp = Keyboard.isKeyDown(Keyboard.KEY_UP) || Keyboard.isKeyDown(Keyboard.KEY_W);
  293.             boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_DOWN) || Keyboard.isKeyDown(Keyboard.KEY_S);
  294.             boolean keyLeft = Keyboard.isKeyDown(Keyboard.KEY_LEFT) || Keyboard.isKeyDown(Keyboard.KEY_A);
  295.             boolean keyRight = Keyboard.isKeyDown(Keyboard.KEY_RIGHT) || Keyboard.isKeyDown(Keyboard.KEY_D);
  296.             boolean flyUp = Keyboard.isKeyDown(Keyboard.KEY_SPACE);
  297.             boolean flyDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
  298.             boolean moveFaster = Keyboard.isKeyDown(Keyboard.KEY_LCONTROL);
  299.             boolean moveSlower = Keyboard.isKeyDown(Keyboard.KEY_TAB);
  300.  
  301. //          if (moveFaster && !moveSlower) {
  302. //              walkingSpeed *= 4f;
  303. //          }
  304. //          if (moveSlower && !moveFaster) {
  305. //              walkingSpeed /= 10f;
  306. //          }
  307.  
  308.             if (keyUp && keyRight && !keyLeft && !keyDown) {
  309.                 float angle = rotation.y + 45;
  310.                 Vector3f newPosition = new Vector3f(offset);
  311.                 float schuine = (walkingSpeed * 0.0002f) * 1;//delta;
  312.                 float aanliggende = schuine * (float) Math.cos(Math.toRadians(angle));
  313.                 float overstaande = (float) (Math.sin(Math.toRadians(angle)) * schuine);
  314.                 newPosition.z += aanliggende;
  315.                 newPosition.x -= overstaande;
  316.                 offset.z = newPosition.z;
  317.                 offset.x = newPosition.x;
  318.             }
  319.             if (keyUp && keyLeft && !keyRight && !keyDown) {
  320.                 float angle = rotation.y - 45;
  321.                 Vector3f newPosition = new Vector3f(offset);
  322.                 float schuine = (walkingSpeed * 0.0002f) * 1;//delta;
  323.                 float aanliggende = schuine * (float) Math.cos(Math.toRadians(angle));
  324.                 float overstaande = (float) (Math.sin(Math.toRadians(angle)) * schuine);
  325.                 newPosition.z += aanliggende;
  326.                 newPosition.x -= overstaande;
  327.                 offset.z = newPosition.z;
  328.                 offset.x = newPosition.x;
  329.             }
  330.             if (keyUp && !keyLeft && !keyRight && !keyDown) {
  331.                 float angle = rotation.y;
  332.                 Vector3f newPosition = new Vector3f(offset);
  333.                 float schuine = (walkingSpeed * 0.0002f) * 1;//delta;
  334.                 float aanliggende = schuine * (float) Math.cos(Math.toRadians(angle));
  335.                 float overstaande = (float) (Math.sin(Math.toRadians(angle)) * schuine);
  336.                 newPosition.z += aanliggende;
  337.                 newPosition.x -= overstaande;
  338.                 offset.z = newPosition.z;
  339.                 offset.x = newPosition.x;
  340.             }
  341.             if (keyDown && keyLeft && !keyRight && !keyUp) {
  342.                 float angle = rotation.y - 135;
  343.                 Vector3f newPosition = new Vector3f(offset);
  344.                 float schuine = (walkingSpeed * 0.0002f) * 1;//delta;
  345.                 float aanliggende = schuine * (float) Math.cos(Math.toRadians(angle));
  346.                 float overstaande = (float) (Math.sin(Math.toRadians(angle)) * schuine);
  347.                 newPosition.z += aanliggende;
  348.                 newPosition.x -= overstaande;
  349.                 offset.z = newPosition.z;
  350.                 offset.x = newPosition.x;
  351.             }
  352.             if (keyDown && keyRight && !keyLeft && !keyUp) {
  353.                 float angle = rotation.y + 135;
  354.                 Vector3f newPosition = new Vector3f(offset);
  355.                 float schuine = (walkingSpeed * 0.0002f) * 1;//delta;
  356.                 float aanliggende = schuine * (float) Math.cos(Math.toRadians(angle));
  357.                 float overstaande = (float) (Math.sin(Math.toRadians(angle)) * schuine);
  358.                 newPosition.z += aanliggende;
  359.                 newPosition.x -= overstaande;
  360.                 offset.z = newPosition.z;
  361.                 offset.x = newPosition.x;
  362.             }
  363.             if (keyDown && !keyUp && !keyLeft && !keyRight) {
  364.                 float angle = rotation.y;
  365.                 Vector3f newPosition = new Vector3f(offset);
  366.                 float schuine = -(walkingSpeed * 0.0002f) * 1;//delta;
  367.                 float aanliggende = schuine * (float) Math.cos(Math.toRadians(angle));
  368.                 float overstaande = (float) (Math.sin(Math.toRadians(angle)) * schuine);
  369.                 newPosition.z += aanliggende;
  370.                 newPosition.x -= overstaande;
  371.                 offset.z = newPosition.z;
  372.                 offset.x = newPosition.x;
  373.             }
  374.             if (keyLeft && !keyRight && !keyUp && !keyDown) {
  375.                 float angle = rotation.y - 90;
  376.                 Vector3f newPosition = new Vector3f(offset);
  377.                 float schuine = (walkingSpeed * 0.0002f) * 1;//delta;
  378.                 float aanliggende = schuine * (float) Math.cos(Math.toRadians(angle));
  379.                 float overstaande = (float) (Math.sin(Math.toRadians(angle)) * schuine);
  380.                 newPosition.z += aanliggende;
  381.                 newPosition.x -= overstaande;
  382.                 offset.z = newPosition.z;
  383.                 offset.x = newPosition.x;
  384.             }
  385.             if (keyRight && !keyLeft && !keyUp && !keyDown) {
  386.                 float angle = rotation.y + 90;
  387.                 Vector3f newPosition = new Vector3f(offset);
  388.                 float schuine = (walkingSpeed * 0.0002f) * 1;//delta;
  389.                 float aanliggende = schuine * (float) Math.cos(Math.toRadians(angle));
  390.                 float overstaande = (float) (Math.sin(Math.toRadians(angle)) * schuine);
  391.                 newPosition.z += aanliggende;
  392.                 newPosition.x -= overstaande;
  393.                 offset.z = newPosition.z;
  394.                 offset.x = newPosition.x;
  395.             }
  396.             if (flyUp && !flyDown) {
  397.                 double newPositionY = (walkingSpeed * 0.0002) * 1;//delta;
  398.                 offset.y -= newPositionY;
  399.             }
  400.             if (flyDown && !flyUp) {
  401.                 double newPositionY = (walkingSpeed * 0.0002) * 1;//delta;
  402.                 offset.y += newPositionY;
  403.             }
  404.             if (moveFaster && !moveSlower) {
  405.                 walkingSpeed /= 4f;
  406.             }
  407.             if (moveSlower && !moveFaster) {
  408.                 walkingSpeed *= 10f;
  409.             }
  410.            
  411.             while (Mouse.next()) {
  412.                 if (Mouse.isButtonDown(0)) {
  413.                     Mouse.setGrabbed(true);
  414.                 }
  415.                 if (Mouse.isButtonDown(1)) {
  416.                     Mouse.setGrabbed(false);
  417.                 }
  418.  
  419.             }
  420.            
  421.            
  422. //          glBegin(GL_POINTS);
  423. //         
  424. //          glVertex3f(-21f, 0f, -81f);
  425. //         
  426. //          glEnd();
  427.            
  428.             if (Keyboard.isKeyDown(Keyboard.KEY_UP))
  429.             {
  430.                 speed += 0.01f;
  431.             }
  432.             else if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
  433.             {
  434.                 speed -= 0.01f;
  435.             }
  436.            
  437.             while (Keyboard.next())
  438.             {
  439.                
  440.             }
  441.            
  442.             if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
  443.             {
  444.                 Display.destroy();
  445.                 System.exit(0);
  446.             }
  447.            
  448.             // render OpenGL here
  449.             Display.update();
  450.             Display.sync(60);
  451.         }
  452.        
  453.         Display.destroy();
  454.         System.exit(0);
  455.     }
  456.    
  457.     int loadTexture(String pathname)
  458.     {
  459.         BufferedImage img = null;
  460.         try
  461.         {
  462.             img = ImageIO.read(new File(pathname));
  463.         }
  464.         catch(IOException e)
  465.         {
  466.             throw new RuntimeException(e);
  467.         }
  468.        
  469.         byte[] src = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
  470.         byte temp;
  471.        
  472.         for(int i = 0x00000000; i < src.length; i += 0x00000003) {
  473.             temp = src[i];
  474.             src[i] = src[i+0x00000002];
  475.             src[i+0x00000002] = temp;
  476.         }
  477.        
  478.         ByteBuffer pixels = (ByteBuffer)BufferUtils.createByteBuffer(src.length).put(src, 0x00000000, src.length).flip();
  479.         IntBuffer textures = BufferUtils.createIntBuffer(0x00000001);
  480.        
  481.         glGenTextures(textures);
  482.         glBindTexture(GL_TEXTURE_2D, textures.get(0x00000000));
  483.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  484.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  485.         glTexImage2D(GL_TEXTURE_2D, 0x00000000, GL_RGB, img.getWidth(), img.getHeight(), 0x00000000, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  486.        
  487.         return textures.get(0x00000000);
  488.     }
  489.    
  490.     public void move(float x, float y, float z)
  491.     {
  492.         position.x += x;
  493.         position.y += y;
  494.         position.z += z;
  495.     }
  496.    
  497.  
  498.     //------- Added for Lighting Test----------//
  499.     private void initLightArrays()
  500.     {
  501.         matSpecular = BufferUtils.createFloatBuffer(4);
  502.         matSpecular.put(1.0f).put(1.0f).put(1.0f).put(1.0f).flip();
  503.        
  504.         lightPosition = BufferUtils.createFloatBuffer(4);
  505.         lightPosition.put(1.0f).put(1.0f).put(1.0f).put(0.0f).flip();
  506.        
  507.         whiteLight = BufferUtils.createFloatBuffer(4);
  508.         whiteLight.put(1.0f).put(1.0f).put(1.0f).put(1.0f).flip();
  509.        
  510.         lModelAmbient = BufferUtils.createFloatBuffer(4);
  511.         lModelAmbient.put(0.5f).put(0.5f).put(0.5f).put(1.0f).flip();
  512.     }
  513.    
  514.     private void updateFps()
  515.     {
  516.         fps ++;
  517.        
  518.         newTime = System.currentTimeMillis();
  519.        
  520.         if (newTime >= oldTime + 1000)
  521.         {
  522.             Display.setTitle("FPS: " + fps);
  523.            
  524.             oldTime = newTime;
  525.            
  526.             fps = 0;
  527.         }
  528.     }
  529. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement