Advertisement
Mudbill

Basic LWJGL (OpenGL) setup

Jul 15th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.79 KB | None | 0 0
  1. package main;
  2.  
  3. import static org.lwjgl.opengl.GL11.*;
  4. import static org.lwjgl.opengl.GL15.*;
  5. import static org.lwjgl.opengl.GL20.*;
  6. import static org.lwjgl.opengl.GL30.*;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.nio.FloatBuffer;
  11. import java.util.Scanner;
  12.  
  13. import org.lwjgl.BufferUtils;
  14. import org.lwjgl.LWJGLException;
  15. import org.lwjgl.input.Keyboard;
  16. import org.lwjgl.opengl.ContextAttribs;
  17. import org.lwjgl.opengl.Display;
  18. import org.lwjgl.opengl.DisplayMode;
  19. import org.lwjgl.opengl.PixelFormat;
  20.  
  21. public class Main {
  22.  
  23.     public static void main(String[] args) {
  24.        
  25.         System.out.println("Starting...");
  26.        
  27.         // Set attributes for the current OpenGL context - OpenGL 3.3 Core and higher.
  28.         ContextAttribs ca = new ContextAttribs(3, 3).withForwardCompatible(true).withProfileCore(true);
  29.        
  30.         // Create display.
  31.         try {
  32.             Display.setDisplayMode(new DisplayMode(800, 600));
  33.             Display.create(new PixelFormat(), ca);
  34.             Display.setTitle("OpenGL");
  35.         } catch (LWJGLException e) {
  36.             e.printStackTrace();
  37.         }
  38.        
  39.         // Set the OpenGL viewport to the full size of the display.
  40.         glViewport(0, 0, 800, 600);
  41.        
  42.         //------------------------------------------------------------------------------
  43.        
  44.         // Vertices co-ordinates to use for drawing a triangle (ranging -1.0 to 1.0).
  45.         // These co-ordinates are translated into positions based on the viewport.
  46.         float[] vertices = {
  47.                 -0.5f, -0.5f, 0.0f,
  48.                 0.5f, -0.5f, 0.0f,
  49.                 0.0f,  0.5f, 0.0f
  50.         };
  51.        
  52.         // VAO - Vertex Array Object
  53.         // Create and bind a VAO, used to store a group of settings for easy access.
  54.         int vaoID = glGenVertexArrays();
  55.         glBindVertexArray(vaoID);
  56.        
  57.         // Store the vertices in a FloatBuffer because that's the optimal way of storing the data for OpenGL.
  58.         FloatBuffer buffer = BufferUtils.createFloatBuffer(vertices.length);
  59.         buffer.put(vertices);
  60.         buffer.flip();
  61.        
  62.         // VBO - Vertex Buffer Object
  63.         // Create a VBO and bind it.
  64.         int vboID = glGenBuffers();
  65.         glBindBuffer(GL_ARRAY_BUFFER, vboID);
  66.        
  67.         // Move the vertices buffer into the VBO.
  68.         glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
  69.        
  70.         // Help OpenGL understand how to use the vertices buffer data:
  71.         // Stored in array #0, it has sets of 3 floats for each group, does not use normalising, a stride of 0 (auto), and an offset of 0.
  72.         glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
  73.        
  74.         // Enable array #0
  75.         glEnableVertexAttribArray(0);
  76.        
  77.         // Generate a shader and store the ID pointing to it.
  78.         int vtxShaderID = glCreateShader(GL_VERTEX_SHADER);
  79.         int fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  80.        
  81.         // Used for storing the shader source code.
  82.         String vertexSource = null;
  83.         String fragmentSource = null;
  84.        
  85.         try {
  86.             //Read vertex shader
  87.             Scanner vtxScanner = new Scanner(new File("shaders/vertex_shader.glsl"));
  88.             StringBuilder vtxSb = new StringBuilder();
  89.             while(vtxScanner.hasNextLine()) vtxSb.append(vtxScanner.nextLine() + System.lineSeparator());
  90.             vtxScanner.close();
  91.             vertexSource = vtxSb.toString();
  92.            
  93.             //Read fragment shader
  94.             Scanner fragScanner = new Scanner(new File("shaders/fragment_shader.glsl"));
  95.             StringBuilder fragSb = new StringBuilder();
  96.             while(fragScanner.hasNextLine()) fragSb.append(fragScanner.nextLine() + System.lineSeparator());
  97.             fragScanner.close();
  98.             fragmentSource = fragSb.toString();
  99.         } catch (FileNotFoundException e) {
  100.             e.printStackTrace();
  101.         }
  102.        
  103.         // Feed the source code to the shaders.
  104.         glShaderSource(vtxShaderID, vertexSource);
  105.         glShaderSource(fragShaderID, fragmentSource);
  106.        
  107.         // Compile the source code.
  108.         glCompileShader(vtxShaderID);
  109.         glCompileShader(fragShaderID);
  110.        
  111.         // Check for compilation errors.
  112.         int vtxCompileStatus = glGetShaderi(vtxShaderID, GL_COMPILE_STATUS);
  113.         if(vtxCompileStatus == GL_FALSE) {
  114.             String log = glGetShaderInfoLog(vtxShaderID, 512);
  115.             System.err.println("FAILED VERTEX SHADER COMPILE:\n"+log);
  116.         }
  117.        
  118.         int fragCompileStatus = glGetShaderi(fragShaderID, GL_COMPILE_STATUS);
  119.         if(fragCompileStatus == GL_FALSE) {
  120.             String log = glGetShaderInfoLog(fragShaderID, 512);
  121.             System.err.println("FAILED FRAGMENT SHADER COMPILE:\n"+log);
  122.         }
  123.        
  124.         // Create a shader program and store the ID pointing to it.
  125.         int shaderProgramID = glCreateProgram();
  126.        
  127.         // Attach the vertex and fragment shaders (in order) to this shader program.
  128.         glAttachShader(shaderProgramID, vtxShaderID);
  129.         glAttachShader(shaderProgramID, fragShaderID);
  130.        
  131.         // Link the shader program's current attachments, which makes the output from one become input to the next.
  132.         glLinkProgram(shaderProgramID);
  133.        
  134.         // Check for linking errors.
  135.         int linkStatus = glGetProgrami(shaderProgramID, GL_LINK_STATUS);
  136.         if(linkStatus == GL_FALSE) {
  137.             String log = glGetProgramInfoLog(shaderProgramID, 512);
  138.             System.err.println("FAILED SHADER LINKING:\n"+log);
  139.         }
  140.        
  141.         // Delete the shader sources because they are now linked in the shader program.
  142.         glDeleteShader(vtxShaderID);
  143.         glDeleteShader(fragShaderID);
  144.                        
  145.         // Set the color that will be used to clear the screen with.
  146.         glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  147.  
  148.         // Activate the shader program to use with render calls.
  149.         glUseProgram(shaderProgramID);
  150.        
  151.         // Activate the specified VAO
  152.         glBindVertexArray(vaoID);
  153.                
  154.         /* Main loop */
  155.         while(!Display.isCloseRequested())
  156.         {
  157.             if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) break;
  158.            
  159.             // Clear the screen every frame with the color specified in glClearColor
  160.             glClear(GL_COLOR_BUFFER_BIT);
  161.            
  162.             // Draw using the currently set VAO and shader.
  163.             glDrawArrays(GL_TRIANGLES, 0, 3);
  164.            
  165.             // Update the display to a framerate.
  166.             Display.sync(60);
  167.             Display.update();
  168.         }
  169.        
  170.         // Cleanly close the display.
  171.         Display.destroy();
  172.        
  173.         System.out.println("Stopping.");
  174.        
  175.     }
  176.    
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement