Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.69 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.             GL20.glEnableVertexAttribArray(0);
  138.             GL20.glEnableVertexAttribArray(1);
  139.            
  140.             GL11.glDrawArrays( GL11.GL_TRIANGLES, 0, 3 );
  141.            
  142.             GL20.glDisableVertexAttribArray(1);
  143.             GL20.glDisableVertexAttribArray(0);
  144.             GL30.glBindVertexArray( 0 );
  145.             GL20.glUseProgram(0);
  146.         }
  147.        
  148.         private void createBuffer() {
  149.             vao = GL30.glGenVertexArrays();
  150.             GL30.glBindVertexArray(vao);
  151.            
  152.                 vbo = GL15.glGenBuffers();
  153.                 GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, vbo);
  154.                 GL15.glBufferData( GL15.GL_ARRAY_BUFFER, 1024 * Vertex.ByteSize, null, GL15.GL_STREAM_DRAW );
  155.                    
  156.                 GL20.glEnableVertexAttribArray(0);
  157.                 GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
  158.        
  159.                 GL20.glEnableVertexAttribArray(1);
  160.                 GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 3);
  161.                
  162.                 GL20.glDisableVertexAttribArray(0);
  163.                 GL20.glDisableVertexAttribArray(1);
  164.                
  165.                 GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, 0);
  166.                
  167.             GL30.glBindVertexArray(0);  
  168.         }
  169.        
  170.         private void fillBuffer() {
  171.             Vertex[] vertices = new Vertex[] {
  172.                     new Vertex(new Vector3f(-1.0f, -1.0f, 0.0f), new Color4f(0.5f, 0.5f, 0.5f, 0.5f), Vector2f.Zero),
  173.                     new Vertex(new Vector3f( 1.0f, -1.0f, 0.0f), new Color4f(0.5f, 0.5f, 0.5f, 0.5f), Vector2f.Zero),
  174.                     new Vertex(new Vector3f( 0.0f,  1.0f, 0.0f), new Color4f(0.5f, 0.5f, 0.5f, 0.5f), Vector2f.Zero),
  175.             };
  176.            
  177.             // 1# Create buffer
  178.             FloatBuffer buffer = ByteBuffer.allocateDirect(3 * 7 * Float.BYTES).asFloatBuffer();
  179.             buffer.position(0);
  180.             for(Vertex vertex : vertices) {
  181.                 buffer.put(vertex.position.x);
  182.                 buffer.put(vertex.position.y);
  183.                 buffer.put(vertex.position.z);
  184.                 buffer.put(vertex.color.r);
  185.                 buffer.put(vertex.color.g);
  186.                 buffer.put(vertex.color.b);
  187.                 buffer.put(vertex.color.a);
  188.             }
  189.            
  190.             // 2# Write data
  191.             GL30.glBindVertexArray(vao);
  192.            
  193.                 GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, vbo);
  194.                 GL15.glBufferSubData( GL15.GL_ARRAY_BUFFER, 3 * 7, buffer);
  195.                 GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, 0);
  196.                
  197.             GL30.glBindVertexArray(0);
  198.         }
  199.        
  200.         private void createShader() {
  201.             String dummyVertexShaderSrc =
  202.                     "#version 330 core"
  203.                     + "\n" + "layout(location = 0) in vec3 vs_position;"
  204.                     + "\n" + "layout(location = 1) in vec4 vs_color;"
  205.                     + "\n" + ""
  206.                     + "\n" + "out vec4 fs_color;"
  207.                     + "\n" + ""
  208.                     + "\n" + "void main() {"
  209.                     + "\n" + "    gl_Position = vec4(vs_position, 1.0);"
  210.                     + "\n" + "    fs_color = vs_color;"
  211.                     + "\n" + "}"
  212.                     ;
  213.            
  214.             String dummyFragmentShaderSrc = "#version 330 core"
  215.                     + "\n" + "in vec4 fs_color;"
  216.                     + "\n" + "void main() {"
  217.                     + "\n" + "    gl_FragColor = fs_color;"
  218.                     + "\n" + "}";
  219.            
  220.             System.out.println("Vertex-Shader: \n" + dummyVertexShaderSrc + "\n");
  221.             System.out.println("Fragment-Shader: \n" + dummyFragmentShaderSrc + "\n");
  222.            
  223.             // 1# Read/Compile VertexShader
  224.             int idVertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
  225.             GL20.glShaderSource(idVertexShader, dummyVertexShaderSrc);
  226.             GL20.glCompileShader(idVertexShader);
  227.    
  228.             if (GL20.glGetShaderi(idVertexShader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
  229.                 System.err.println("Could not compile vertex shader: " + GL20.glGetShaderInfoLog(idVertexShader));
  230.                 System.exit(-1);
  231.             }
  232.            
  233.             // 2# Read/Compile FragmentShader
  234.             int idFragmentShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
  235.             GL20.glShaderSource(idFragmentShader, dummyFragmentShaderSrc);
  236.             GL20.glCompileShader(idFragmentShader);
  237.    
  238.             if (GL20.glGetShaderi(idFragmentShader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
  239.                 System.err.println("Could not compile fragment shader: " + GL20.glGetShaderInfoLog(idFragmentShader));
  240.                 System.exit(-1);
  241.             }
  242.            
  243.             // 3# Create Shader-Program
  244.             shader = GL20.glCreateProgram();
  245.             GL20.glAttachShader(shader, idVertexShader);
  246.             GL20.glAttachShader(shader, idFragmentShader);
  247.            
  248.             GL20.glBindAttribLocation(shader, 0, "vs_position");
  249.             GL20.glBindAttribLocation(shader, 1, "vs_color");
  250.    
  251.             GL20.glLinkProgram(shader);
  252.             if (GL20.glGetProgrami(shader, GL20.GL_LINK_STATUS) == GL11.GL_FALSE) {
  253.                 System.out.println("Shader linking failed: " + GL20.glGetProgramInfoLog(shader));
  254.                 System.exit(-1);
  255.             }
  256.    
  257.             GL20.glValidateProgram(shader);
  258.             GL20.glDeleteShader(idVertexShader);
  259.             GL20.glDeleteShader(idFragmentShader);
  260.         }
  261.      
  262.         private int vao;
  263.         private int vbo;
  264.         private int shader;
  265.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement