Advertisement
Guest User

LWJGL 3 cube render

a guest
Feb 4th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.94 KB | None | 0 0
  1. package xan_code;
  2.  
  3. import org.lwjgl.*;
  4. import org.lwjgl.glfw.*;
  5. import org.lwjgl.opengl.*;
  6. import org.lwjgl.system.*;
  7.  
  8. import com.jogamp.opengl.glu.GLU;
  9.  
  10. import xan_code.entity.Player;
  11. import xan_code.input.Keyboard;
  12. import xan_code.input.Mouse;
  13. import xan_code.util.Vector3;
  14.  
  15. import java.nio.*;
  16.  
  17. import static org.lwjgl.glfw.Callbacks.*;
  18. import static org.lwjgl.glfw.GLFW.*;
  19. import static org.lwjgl.opengl.GL11.*;
  20. import static org.lwjgl.system.MemoryStack.*;
  21. import static org.lwjgl.system.MemoryUtil.*;
  22.  
  23. public class Main {
  24.  
  25.     // The window handle
  26.     private long window;
  27.     private Player player = new Player(); //This is a custom class. All it stores is a Position value, which is my custom Vector3 class (Which just stores an X, Y and Z float value)
  28.  
  29.     public void run() {
  30.         init();
  31.         loop();
  32.  
  33.         // Free the window callbacks and destroy the window
  34.         glfwFreeCallbacks(window);
  35.         glfwDestroyWindow(window);
  36.  
  37.         // Terminate GLFW and free the error callback
  38.         glfwTerminate();
  39.         glfwSetErrorCallback(null).free();
  40.     }
  41.  
  42.     private void init() {
  43.         // Setup an error callback. The default implementation
  44.         // will print the error message in System.err.
  45.         GLFWErrorCallback.createPrint(System.err).set();
  46.  
  47.         // Initialize GLFW. Most GLFW functions will not work before doing this.
  48.         if ( !glfwInit() )
  49.             throw new IllegalStateException("Unable to initialize GLFW");
  50.  
  51.         // Configure GLFW
  52.         glfwDefaultWindowHints(); // optional, the current window hints are already the default
  53.         glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
  54.         glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
  55.  
  56.         // Create the window
  57.         window = glfwCreateWindow(1024, 768, "Xan's game", NULL, NULL);
  58.         if ( window == NULL )
  59.             throw new RuntimeException("Failed to create the GLFW window");
  60.  
  61.         // Setup a key callback. It will be called every time a key is pressed, repeated or released.
  62.         glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
  63.             if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) {
  64.                 glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
  65.             }
  66.             Keyboard.SetKey(window, key, scancode, action, mods);
  67.         });
  68.        
  69.  
  70.         // Get the thread stack and push a new frame
  71.         try ( MemoryStack stack = stackPush() ) {
  72.             IntBuffer pWidth = stack.mallocInt(1); // int*
  73.             IntBuffer pHeight = stack.mallocInt(1); // int*
  74.  
  75.             // Get the window size passed to glfwCreateWindow
  76.             glfwGetWindowSize(window, pWidth, pHeight);
  77.  
  78.             // Get the resolution of the primary monitor
  79.             GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  80.  
  81.             // Center the window
  82.             glfwSetWindowPos(
  83.                 window,
  84.                 (vidmode.width() - pWidth.get(0)) / 2,
  85.                 (vidmode.height() - pHeight.get(0)) / 2
  86.             );
  87.         } // the stack frame is popped automatically
  88.  
  89.         // Make the OpenGL context current
  90.         glfwMakeContextCurrent(window);
  91.         // Enable v-sync
  92.         glfwSwapInterval(1);
  93.  
  94.         // Make the window visible
  95.         glfwShowWindow(window);
  96.     }
  97.    
  98.     private void Render() {
  99.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  100.        
  101.         glMatrixMode(GL_MODELVIEW);
  102.         glLoadIdentity();
  103.         GLU.gluPerspective(80, 4/3, 0.1, 10000); //Faulty line right here.
  104.        
  105.         glTranslatef(player.Position.X, player.Position.Y, player.Position.Z); //See line 27
  106.         glRotatef(45f, 0, 1, 1);
  107.        
  108.         glBegin(GL11.GL_QUADS);    
  109.         glColor3f(10f,10f,0.0f);          
  110.         glVertex3f( 10f, 10f,-10f);        
  111.         glVertex3f(-10f, 10f,-10f);        
  112.         glVertex3f(-10f, 10f, 10f);
  113.         glVertex3f( 10f, 10f, 10f);  
  114.         glColor3f(10f,10f,0.0f);            
  115.         glVertex3f( 10f,-10f, 10f);
  116.         glVertex3f(-10f,-10f, 10f);
  117.         glVertex3f(-10f,-10f,-10f);
  118.         glVertex3f( 10f,-10f,-10f);
  119.         glColor3f(10f,0.0f,0.0f);
  120.         glVertex3f( 10f, 10f, 10f);
  121.         glVertex3f(-10f, 10f, 10f);
  122.         glVertex3f(-10f,-10f, 10f);
  123.         glVertex3f( 10f,-10f, 10f);
  124.         glColor3f(10f,10f,0.0f);
  125.         glVertex3f( 10f,-10f,-10f);
  126.         glVertex3f(-10f,-10f,-10f);
  127.         glVertex3f(-10f, 10f,-10f);
  128.         glVertex3f( 10f, 10f,-10f);
  129.         glColor3f(0.0f,0.0f,10f);
  130.         glVertex3f(-10f, 10f, 10f);
  131.         glVertex3f(-10f, 10f,-10f);
  132.         glVertex3f(-10f,-10f,-10f);
  133.         glVertex3f(-10f,-10f, 10f);
  134.         glColor3f(10f,0.0f,10f);
  135.         glVertex3f( 10f, 10f,-10f);
  136.         glVertex3f( 10f, 10f, 10f);
  137.         glVertex3f( 10f,-10f, 10f);
  138.         glVertex3f( 10f,-10f,-10f);
  139.         glEnd();    
  140.     }
  141.  
  142.     private void loop() {
  143.         // This line is critical for LWJGL's interoperation with GLFW's
  144.         // OpenGL context, or any context that is managed externally.
  145.         // LWJGL detects the context that is current in the current thread,
  146.         // creates the GLCapabilities instance and makes the OpenGL
  147.         // bindings available for use.
  148.         GL.createCapabilities();
  149.  
  150.         // Set the clear color
  151.         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  152.        
  153.         // Run the rendering loop until the user has attempted to close
  154.         // the window or has pressed the ESCAPE key.
  155.         while ( !glfwWindowShouldClose(window) ) {
  156.             Mouse.Update(window);
  157.            
  158.             if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_W)) {
  159.                 player.Position = player.Position.add(new Vector3(0, 0, 0.1f));
  160.             }          
  161.             if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_S)) {
  162.                 player.Position = player.Position.subtract(new Vector3(0, 0, 0.1f));
  163.             }          
  164.             if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_A)) {
  165.                 player.Position = player.Position.add(new Vector3(0.1f, 0, 0));
  166.             }          
  167.             if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_D)) {
  168.                 player.Position = player.Position.subtract(new Vector3(0.1f, 0, 0));
  169.             }
  170.             if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_Q)) {
  171.                 player.Position = player.Position.subtract(new Vector3(0, 0.1f, 0));
  172.             }
  173.             if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_E)) {
  174.                 player.Position = player.Position.add(new Vector3(0, 0.1f, 0));
  175.             }
  176.            
  177.             Render();
  178.             glfwSwapBuffers(window); // swap the color buffers
  179.  
  180.             // Poll for window events. The key callback above will only be
  181.             // invoked during this call.
  182.             glfwPollEvents();
  183.         }
  184.     }
  185.  
  186.     public static void main(String[] args) {
  187.         new Main().run();
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement