Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL15.*;
- import static org.lwjgl.opengl.GL20.*;
- import static org.lwjgl.opengl.GL30.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.nio.FloatBuffer;
- import java.util.Scanner;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.input.Keyboard;
- import org.lwjgl.opengl.ContextAttribs;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.PixelFormat;
- public class Main {
- public static void main(String[] args) {
- System.out.println("Starting...");
- // Set attributes for the current OpenGL context - OpenGL 3.3 Core and higher.
- ContextAttribs ca = new ContextAttribs(3, 3).withForwardCompatible(true).withProfileCore(true);
- // Create display.
- try {
- Display.setDisplayMode(new DisplayMode(800, 600));
- Display.create(new PixelFormat(), ca);
- Display.setTitle("OpenGL");
- } catch (LWJGLException e) {
- e.printStackTrace();
- }
- // Set the OpenGL viewport to the full size of the display.
- glViewport(0, 0, 800, 600);
- //------------------------------------------------------------------------------
- // Vertices co-ordinates to use for drawing a triangle (ranging -1.0 to 1.0).
- // These co-ordinates are translated into positions based on the viewport.
- float[] vertices = {
- -0.5f, -0.5f, 0.0f,
- 0.5f, -0.5f, 0.0f,
- 0.0f, 0.5f, 0.0f
- };
- // VAO - Vertex Array Object
- // Create and bind a VAO, used to store a group of settings for easy access.
- int vaoID = glGenVertexArrays();
- glBindVertexArray(vaoID);
- // Store the vertices in a FloatBuffer because that's the optimal way of storing the data for OpenGL.
- FloatBuffer buffer = BufferUtils.createFloatBuffer(vertices.length);
- buffer.put(vertices);
- buffer.flip();
- // VBO - Vertex Buffer Object
- // Create a VBO and bind it.
- int vboID = glGenBuffers();
- glBindBuffer(GL_ARRAY_BUFFER, vboID);
- // Move the vertices buffer into the VBO.
- glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
- // Help OpenGL understand how to use the vertices buffer data:
- // 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.
- glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
- // Enable array #0
- glEnableVertexAttribArray(0);
- // Generate a shader and store the ID pointing to it.
- int vtxShaderID = glCreateShader(GL_VERTEX_SHADER);
- int fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);
- // Used for storing the shader source code.
- String vertexSource = null;
- String fragmentSource = null;
- try {
- //Read vertex shader
- Scanner vtxScanner = new Scanner(new File("shaders/vertex_shader.glsl"));
- StringBuilder vtxSb = new StringBuilder();
- while(vtxScanner.hasNextLine()) vtxSb.append(vtxScanner.nextLine() + System.lineSeparator());
- vtxScanner.close();
- vertexSource = vtxSb.toString();
- //Read fragment shader
- Scanner fragScanner = new Scanner(new File("shaders/fragment_shader.glsl"));
- StringBuilder fragSb = new StringBuilder();
- while(fragScanner.hasNextLine()) fragSb.append(fragScanner.nextLine() + System.lineSeparator());
- fragScanner.close();
- fragmentSource = fragSb.toString();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- // Feed the source code to the shaders.
- glShaderSource(vtxShaderID, vertexSource);
- glShaderSource(fragShaderID, fragmentSource);
- // Compile the source code.
- glCompileShader(vtxShaderID);
- glCompileShader(fragShaderID);
- // Check for compilation errors.
- int vtxCompileStatus = glGetShaderi(vtxShaderID, GL_COMPILE_STATUS);
- if(vtxCompileStatus == GL_FALSE) {
- String log = glGetShaderInfoLog(vtxShaderID, 512);
- System.err.println("FAILED VERTEX SHADER COMPILE:\n"+log);
- }
- int fragCompileStatus = glGetShaderi(fragShaderID, GL_COMPILE_STATUS);
- if(fragCompileStatus == GL_FALSE) {
- String log = glGetShaderInfoLog(fragShaderID, 512);
- System.err.println("FAILED FRAGMENT SHADER COMPILE:\n"+log);
- }
- // Create a shader program and store the ID pointing to it.
- int shaderProgramID = glCreateProgram();
- // Attach the vertex and fragment shaders (in order) to this shader program.
- glAttachShader(shaderProgramID, vtxShaderID);
- glAttachShader(shaderProgramID, fragShaderID);
- // Link the shader program's current attachments, which makes the output from one become input to the next.
- glLinkProgram(shaderProgramID);
- // Check for linking errors.
- int linkStatus = glGetProgrami(shaderProgramID, GL_LINK_STATUS);
- if(linkStatus == GL_FALSE) {
- String log = glGetProgramInfoLog(shaderProgramID, 512);
- System.err.println("FAILED SHADER LINKING:\n"+log);
- }
- // Delete the shader sources because they are now linked in the shader program.
- glDeleteShader(vtxShaderID);
- glDeleteShader(fragShaderID);
- // Set the color that will be used to clear the screen with.
- glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- // Activate the shader program to use with render calls.
- glUseProgram(shaderProgramID);
- // Activate the specified VAO
- glBindVertexArray(vaoID);
- /* Main loop */
- while(!Display.isCloseRequested())
- {
- if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) break;
- // Clear the screen every frame with the color specified in glClearColor
- glClear(GL_COLOR_BUFFER_BIT);
- // Draw using the currently set VAO and shader.
- glDrawArrays(GL_TRIANGLES, 0, 3);
- // Update the display to a framerate.
- Display.sync(60);
- Display.update();
- }
- // Cleanly close the display.
- Display.destroy();
- System.out.println("Stopping.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement