Advertisement
Guest User

WorldTest

a guest
Apr 12th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.47 KB | None | 0 0
  1. package main;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.nio.FloatBuffer;
  8. import java.nio.IntBuffer;
  9. import java.util.ArrayList;
  10.  
  11. import org.lwjgl.BufferUtils;
  12. import org.lwjgl.LWJGLException;
  13. import org.lwjgl.Sys;
  14. import org.lwjgl.input.Keyboard;
  15. import org.lwjgl.input.Mouse;
  16. import org.lwjgl.opengl.Display;
  17. import org.lwjgl.opengl.DisplayMode;
  18. import org.lwjgl.opengl.GL11;
  19. import org.lwjgl.opengl.GL15;
  20. import org.lwjgl.util.glu.GLU;
  21. import org.newdawn.slick.opengl.Texture;
  22. import org.newdawn.slick.opengl.TextureLoader;
  23.  
  24. public class WorldTest {
  25.    
  26.     static float[] GRASS = Block.texCoords(1, 0, 0, 1, 0, 0, 4);
  27.     static float[] SAND = Block.texCoords(1, 1, 1, 1, 1, 1, 4);
  28.     static float[] BRICK = Block.texCoords(2, 0, 2, 0, 2, 0, 4);
  29.     static float[] STONE = Block.texCoords(2, 1, 2, 1, 2, 1, 4);
  30.    
  31.     int width = 800;
  32.     int height = 600;
  33.    
  34.     long lastFrame;
  35.    
  36.     int fps;
  37.     long lastFPS;
  38.     private Texture texture;
  39.     private Vector position = new Vector(0, -1.8f, 0);
  40.     private float yaw = 0;
  41.     private float pitch = 0;
  42.  
  43.     private float[] cubeVerts;
  44.  
  45.     private int vHandle;
  46.  
  47.     private int tHandle;
  48.    
  49.     public void start()
  50.     {
  51.        
  52.         try {
  53.             Display.setDisplayMode(new DisplayMode(width, height));
  54.             Display.create();
  55.         } catch (LWJGLException e) {
  56.             e.printStackTrace();
  57.             System.exit(0);
  58.         }
  59.        
  60.         Mouse.setGrabbed(true);
  61.        
  62.         try {
  63.             texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("texture.png")));
  64.         } catch (FileNotFoundException e) {
  65.             e.printStackTrace();
  66.         } catch (IOException e) {
  67.             e.printStackTrace();
  68.         }
  69.  
  70.         initGL();
  71.         setupBasicOpenGL();
  72.         getDelta();
  73.         lastFPS = getTime();
  74.        
  75.         texture.bind();
  76.        
  77.         IntBuffer ib = BufferUtils.createIntBuffer(2);
  78.         GL15.glGenBuffers(ib);
  79.         vHandle = ib.get(0);
  80.         tHandle = ib.get(1);
  81.        
  82.         init();
  83.  
  84.         while (!Display.isCloseRequested()) {
  85.             int delta = getDelta();
  86.            
  87.             GL11.glLoadIdentity();
  88.             lookThrough();
  89.            
  90.             update(delta);
  91.  
  92.             renderGL();
  93.            
  94.  
  95.             Display.update();
  96.             Display.sync(60); // cap fps to 60fps
  97.         }
  98.        
  99.  
  100.         GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
  101.         GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
  102.        
  103.         ib.put(0, vHandle);
  104.         ib.put(1, tHandle);
  105.         GL15.glDeleteBuffers(ib);
  106.  
  107.         Display.destroy();
  108.     }
  109.    
  110.     private void init()
  111.     {
  112.         ArrayList<Block> blocks = new ArrayList<Block>();
  113.         int n = 10;  //1/2 width and height of world
  114.         int s = 1;
  115.         int y = 0;
  116.        
  117.         for (int x = -n; x < n + 1; x += s)
  118.         {
  119.             for (int z = -n; z < n + 1; z += s)
  120.             {
  121.                 blocks.add(new Block(new Vector(x, y - 2, z), GRASS));
  122.             }
  123.         }
  124.        
  125.         updateVBO(blocks);
  126.     }
  127.    
  128.     public void updateVBO(ArrayList<Block> blocks)
  129.     {
  130.         ArrayList<Float> cubeVertList = new ArrayList<Float>();
  131.         ArrayList<Float> texCoordList = new ArrayList<Float>();
  132.        
  133.         for (Block block : blocks)
  134.         {
  135.             for (float f : block.getCubeVerts())
  136.                 cubeVertList.add(f);
  137.             for (float f : block.getTexCoords())
  138.                 texCoordList.add(f);
  139.             System.out.println(block.getPosition());
  140.         }
  141.        
  142.         cubeVerts = new float[cubeVertList.size()];
  143.         for (int i = 0; i < cubeVertList.size(); i++)
  144.             cubeVerts[i] = cubeVertList.get(i);
  145.        
  146.         FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(cubeVerts.length);
  147.         vertBuffer.put(cubeVerts);
  148.         vertBuffer.flip();
  149.        
  150.         float[] textures = new float[texCoordList.size()];
  151.         for (int i = 0; i < texCoordList.size(); i++)
  152.             textures[i] = texCoordList.get(i);
  153.        
  154.        
  155.         FloatBuffer texBuffer = BufferUtils.createFloatBuffer(textures.length);
  156.         texBuffer.put(textures);
  157.         texBuffer.flip();
  158.        
  159.        
  160.        
  161.        
  162.         GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
  163.         GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
  164.        
  165.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vHandle);
  166.         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
  167.         GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
  168.        
  169.        
  170.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tHandle);
  171.         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texBuffer, GL15.GL_STATIC_DRAW);
  172.         GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0L);
  173.        
  174.     }
  175.    
  176.     public void update(int delta)
  177.     {
  178.         float mouseSensitivity = .15f;
  179.         float movementSpeed = 2 * (float)delta / 1000.0f;
  180.        
  181.         float dx = Mouse.getDX();
  182.         //distance in mouse movement from the last getDY() call.
  183.         float dy = Mouse.getDY();
  184.        
  185.         yaw += dx * mouseSensitivity;
  186.         pitch -= dy * mouseSensitivity;
  187.        
  188.         if (Keyboard.isKeyDown(Keyboard.KEY_W))
  189.         {
  190.             position.x -= movementSpeed * (float)Math.sin(Math.toRadians(yaw));
  191.             position.z += movementSpeed * (float)Math.cos(Math.toRadians(yaw));
  192.         }
  193.  
  194.         //moves the camera backward relitive to its current rotation (yaw)
  195.         if (Keyboard.isKeyDown(Keyboard.KEY_S))
  196.         {      
  197.             position.x += movementSpeed * (float)Math.sin(Math.toRadians(yaw));
  198.             position.z -= movementSpeed * (float)Math.cos(Math.toRadians(yaw));
  199.         }
  200.  
  201.         //strafes the camera left relitive to its current rotation (yaw)
  202.         if (Keyboard.isKeyDown(Keyboard.KEY_A))
  203.         {
  204.             position.x -= movementSpeed * (float)Math.sin(Math.toRadians(yaw-90));
  205.             position.z += movementSpeed * (float)Math.cos(Math.toRadians(yaw-90));
  206.         }
  207.  
  208.         //strafes the camera right relitive to its current rotation (yaw)
  209.         if (Keyboard.isKeyDown(Keyboard.KEY_D))
  210.         {
  211.             position.x -= movementSpeed * (float)Math.sin(Math.toRadians(yaw+90));
  212.             position.z += movementSpeed * (float)Math.cos(Math.toRadians(yaw+90));
  213.         }
  214.        
  215.         if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
  216.             position.y -= movementSpeed * 2;
  217.         if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
  218.             position.y += movementSpeed * 2;
  219.        
  220.         updateFPS(); // update FPS Counter
  221.     }
  222.    
  223.  
  224.     public int getDelta()
  225.     {
  226.         long time = getTime();
  227.         int delta = (int) (time - lastFrame);
  228.         lastFrame = time;
  229.      
  230.         return delta;
  231.     }
  232.    
  233.     public static long getTime()
  234.     {
  235.         return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  236.     }
  237.    
  238.     public void updateFPS()
  239.     {
  240.         if (getTime() - lastFPS > 1000)
  241.         {
  242.             Display.setTitle("FPS: " + fps);
  243.             fps = 0;
  244.             lastFPS += 1000;
  245.         }
  246.         fps++;
  247.     }
  248.    
  249.     public void initGL()
  250.     {
  251.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  252.         GL11.glLoadIdentity();
  253.         GLU.gluPerspective(45.0f,((float)width)/((float)height),0.1f,100.0f);
  254.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  255.         GL11.glLoadIdentity();
  256.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  257.         GL11.glShadeModel(GL11.GL_SMOOTH);
  258.         GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  259.         GL11.glClearDepth(1.0f);
  260.         GL11.glEnable(GL11.GL_DEPTH_TEST);
  261.         GL11.glDepthFunc(GL11.GL_LEQUAL);
  262.         GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
  263.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
  264.     }
  265.    
  266.     public void lookThrough()
  267.     {
  268.         GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
  269.         GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
  270.         GL11.glTranslatef(position .x, position.y, position.z);
  271.     }
  272.    
  273.     public void renderGL()
  274.     {
  275.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
  276.         GL11.glTranslatef(0.0f,0f,-5.0f);
  277.        
  278.        
  279.         GL11.glDrawArrays(GL11.GL_QUADS, 0, cubeVerts.length);
  280.        
  281.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  282.        
  283.     }
  284.    
  285.    
  286.    
  287.    
  288.    
  289.    
  290.    
  291.     public void setupBasicOpenGL()
  292.     {
  293.         GL11.glClearColor(0.5f, 0.69f, 1.0f, 1f);
  294.         GL11.glEnable(GL11.GL_CULL_FACE);
  295.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
  296.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
  297.        
  298.        
  299.         FloatBuffer fogColor = BufferUtils.createFloatBuffer(4);
  300.         fogColor.put(.5f).put(.69f).put(1f).put(1f).flip();
  301.        
  302.         GL11.glEnable(GL11.GL_DEPTH_TEST);
  303.         GL11.glEnable(GL11.GL_FOG);
  304.         GL11.glFog(GL11.GL_FOG_COLOR, fogColor);
  305.         GL11.glHint(GL11.GL_FOG_HINT, GL11.GL_DONT_CARE);
  306.         GL11.glFogf(GL11.GL_FOG_DENSITY, 0.035f);
  307.         GL11.glFogf(GL11.GL_FOG_START, 20.0f);
  308.         GL11.glFogf(GL11.GL_FOG_END, 60.0f);
  309.     }
  310.    
  311.     public static void main(String[] argv)
  312.     {
  313.         WorldTest worldTest = new WorldTest();
  314.         worldTest.start();
  315.     }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement