Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.47 KB | None | 0 0
  1. package de.orangestar;
  2.  
  3. import org.lwjgl.Sys;
  4. import org.lwjgl.glfw.*;
  5. import org.lwjgl.opengl.*;
  6.  
  7. import de.orangestar.engine.values.Color4f;
  8. import de.orangestar.engine.values.Vector2f;
  9. import de.orangestar.engine.values.Vector3f;
  10. import de.orangestar.engine.values.Vertex;
  11.  
  12. import java.nio.ByteBuffer;
  13.  
  14.  
  15.  
  16.  
  17. import java.nio.FloatBuffer;
  18. import java.util.Arrays;
  19.  
  20. import static org.lwjgl.glfw.Callbacks.*;
  21. import static org.lwjgl.glfw.GLFW.*;
  22. import static org.lwjgl.opengl.GL11.*;
  23. import static org.lwjgl.system.MemoryUtil.*;
  24.  
  25. public class HelloWorld {
  26.  
  27.     // We need to strongly reference callback instances.
  28.     private GLFWErrorCallback errorCallback;
  29.     private GLFWKeyCallback   keyCallback;
  30.  
  31.     // The window handle
  32.     private long window;
  33.  
  34.     public void run() {
  35.         System.out.println("Hello LWJGL " + Sys.getVersion() + "!");
  36.  
  37.         try {
  38.             init();
  39.             loop();
  40.  
  41.             // Release window and window callbacks
  42.             glfwDestroyWindow(window);
  43.             keyCallback.release();
  44.         } finally {
  45.             // Terminate GLFW and release the GLFWerrorfun
  46.             glfwTerminate();
  47.             errorCallback.release();
  48.         }
  49.     }
  50.  
  51.     private void init() {
  52.         // Setup an error callback. The default implementation
  53.         // will print the error message in System.err.
  54.         glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));
  55.  
  56.         // Initialize GLFW. Most GLFW functions will not work before doing this.
  57.         if ( glfwInit() != GL11.GL_TRUE )
  58.             throw new IllegalStateException("Unable to initialize GLFW");
  59.  
  60.         // Configure our window
  61.         glfwDefaultWindowHints(); // optional, the current window hints are already the default
  62.         glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
  63.         glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable
  64.  
  65.         int WIDTH = 300;
  66.         int HEIGHT = 300;
  67.  
  68.         // Create the window
  69.         window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL);
  70.         if ( window == NULL )
  71.             throw new RuntimeException("Failed to create the GLFW window");
  72.  
  73.         // Setup a key callback. It will be called every time a key is pressed, repeated or released.
  74.         glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
  75.             @Override
  76.             public void invoke(long window, int key, int scancode, int action, int mods) {
  77.                 if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
  78.                     glfwSetWindowShouldClose(window, GL_TRUE); // We will detect this in our rendering loop
  79.             }
  80.         });
  81.  
  82.         // Get the resolution of the primary monitor
  83.         ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  84.         // Center our window
  85.         glfwSetWindowPos(
  86.             window,
  87.             (GLFWvidmode.width(vidmode) - WIDTH) / 2,
  88.             (GLFWvidmode.height(vidmode) - HEIGHT) / 2
  89.         );
  90.  
  91.         // Make the OpenGL context current
  92.         glfwMakeContextCurrent(window);
  93.         // Enable v-sync
  94.         glfwSwapInterval(1);
  95.  
  96.         // Make the window visible
  97.         glfwShowWindow(window);
  98.     }
  99.  
  100.     private void loop() {
  101.         // This line is critical for LWJGL's interoperation with GLFW's
  102.         // OpenGL context, or any context that is managed externally.
  103.         // LWJGL detects the context that is current in the current thread,
  104.         // creates the ContextCapabilities instance and makes the OpenGL
  105.         // bindings available for use.
  106.         GLContext.createFromCurrent();
  107.  
  108.         // Set the clear color
  109.         glClearColor(0.0f, 0.2f, 0.0f, 0.0f);
  110.  
  111.         createShader();
  112.         createBuffer();
  113.         fillBuffer();
  114.  
  115.         // Run the rendering loop until the user has attempted to close
  116.         // the window or has pressed the ESCAPE key.
  117.         while ( glfwWindowShouldClose(window) == GL_FALSE ) {
  118.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
  119.  
  120.             display();
  121.            
  122.             glfwSwapBuffers(window); // swap the color buffers
  123.  
  124.             // Poll for window events. The key callback above will only be
  125.             // invoked during this call.
  126.             glfwPollEvents();
  127.         }
  128.     }
  129.  
  130.     public static void main(String[] args) {
  131.         new HelloWorld().run();
  132.     }
  133.    
  134.     private void display() {
  135.         GL20.glUseProgram(shader);
  136.         GL30.glBindVertexArray( vao );
  137.        
  138.         GL11.glDrawArrays( GL11.GL_TRIANGLES, 0, 3 );
  139.  
  140.         GL30.glBindVertexArray( 0 );
  141.         GL20.glUseProgram(0);
  142.     }
  143.    
  144.     private void createBuffer() {
  145.         vao = GL30.glGenVertexArrays();
  146.         GL30.glBindVertexArray(vao);
  147.        
  148.             vbo = GL15.glGenBuffers();
  149.             GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, vbo);
  150.             GL15.glBufferData( GL15.GL_ARRAY_BUFFER, 1024 * Vertex.ByteSize, null, GL15.GL_STREAM_DRAW );
  151.                
  152.             GL20.glEnableVertexAttribArray(0);
  153.             GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 7 * Float.BYTES, 0);
  154.    
  155.             GL20.glEnableVertexAttribArray(1);
  156.             GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 7 * Float.BYTES, 3 * Float.BYTES);
  157.                        
  158.             GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, 0);
  159.            
  160.         GL30.glBindVertexArray(0);  
  161.     }
  162.    
  163.     private void fillBuffer() {
  164.         Vertex[] vertices = new Vertex[] {
  165.                 new Vertex(new Vector3f(-1.0f, -1.0f, 0.0f), new Color4f(0.5f, 0.5f, 0.5f, 0.5f)),
  166.                 new Vertex(new Vector3f( 1.0f, -1.0f, 0.0f), new Color4f(0.5f, 0.5f, 0.5f, 0.5f)),
  167.                 new Vertex(new Vector3f( 0.0f,  1.0f, 0.0f), new Color4f(0.5f, 0.5f, 0.5f, 0.5f)),
  168.         };
  169.        
  170.         // 1# Create buffer
  171.         FloatBuffer buffer = ByteBuffer.allocateDirect(3 * 7 * Float.BYTES).asFloatBuffer();
  172.         buffer.position(0);
  173.         for(Vertex vertex : vertices) {
  174.             buffer.put(vertex.position.x);
  175.             buffer.put(vertex.position.y);
  176.             buffer.put(vertex.position.z);
  177.             buffer.put(vertex.color.r);
  178.             buffer.put(vertex.color.g);
  179.             buffer.put(vertex.color.b);
  180.             buffer.put(vertex.color.a);
  181.         }
  182.        
  183.         // 2# Write data
  184.         GL30.glBindVertexArray(vao);
  185.        
  186.             GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, vbo);
  187.             GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, buffer);
  188.             GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, 0);
  189.            
  190.         GL30.glBindVertexArray(0);
  191.     }
  192.    
  193.     private void createShader() {
  194.         String dummyVertexShaderSrc =
  195.                 "#version 330 core"
  196.                 + "\n" + "layout(location = 0) in vec3 vs_position;"
  197.                 + "\n" + "layout(location = 1) in vec4 vs_color;"
  198.                 + "\n" + ""
  199.                 + "\n" + "out vec4 fs_color;"
  200.                 + "\n" + ""
  201.                 + "\n" + "void main() {"
  202.                 + "\n" + "    gl_Position = vec4(vs_position, 1.0);"
  203.                 + "\n" + "    fs_color = vs_color;"
  204.                 + "\n" + "}"
  205.                 ;
  206.        
  207.         String dummyFragmentShaderSrc = "#version 330 core"
  208.                 + "\n" + "in vec4 fs_color;"
  209.                 + "\n" + ""
  210.                 + "\n" + "out vec4 out_color;"
  211.                 + "\n" + ""
  212.                 + "\n" + "void main() {"
  213.                 + "\n" + "    out_color = fs_color;"
  214.                 + "\n" + "}";
  215.        
  216.         System.out.println("Vertex-Shader: \n" + dummyVertexShaderSrc + "\n");
  217.         System.out.println("Fragment-Shader: \n" + dummyFragmentShaderSrc + "\n");
  218.        
  219.         // 1# Read/Compile VertexShader
  220.         int idVertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
  221.         GL20.glShaderSource(idVertexShader, dummyVertexShaderSrc);
  222.         GL20.glCompileShader(idVertexShader);
  223.  
  224.         if (GL20.glGetShaderi(idVertexShader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
  225.             System.err.println("Could not compile vertex shader: " + GL20.glGetShaderInfoLog(idVertexShader));
  226.             System.exit(-1);
  227.         }
  228.        
  229.         // 2# Read/Compile FragmentShader
  230.         int idFragmentShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
  231.         GL20.glShaderSource(idFragmentShader, dummyFragmentShaderSrc);
  232.         GL20.glCompileShader(idFragmentShader);
  233.  
  234.         if (GL20.glGetShaderi(idFragmentShader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
  235.             System.err.println("Could not compile fragment shader: " + GL20.glGetShaderInfoLog(idFragmentShader));
  236.             System.exit(-1);
  237.         }
  238.        
  239.         // 3# Create Shader-Program
  240.         shader = GL20.glCreateProgram();
  241.         GL20.glAttachShader(shader, idVertexShader);
  242.         GL20.glAttachShader(shader, idFragmentShader);
  243.        
  244.         GL20.glBindAttribLocation(shader, 0, "vs_position");
  245.         GL20.glBindAttribLocation(shader, 1, "vs_color");
  246.  
  247.         GL20.glLinkProgram(shader);
  248.         if (GL20.glGetProgrami(shader, GL20.GL_LINK_STATUS) == GL11.GL_FALSE) {
  249.             System.out.println("Shader linking failed: " + GL20.glGetProgramInfoLog(shader));
  250.             System.exit(-1);
  251.         }
  252.  
  253.         GL20.glValidateProgram(shader);
  254.         GL20.glDeleteShader(idVertexShader);
  255.         GL20.glDeleteShader(idFragmentShader);
  256.     }
  257.  
  258.     private int vao;
  259.     private int vbo;
  260.     private int shader;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement