Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.srinjoy.engine;
- import org.lwjgl.BufferUtils;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import static org.lwjgl.opengl.GL20.*;
- import static org.lwjgl.opengl.GL30.glBindVertexArray;
- import static org.lwjgl.opengl.GL30.glGenVertexArrays;
- public class LevelEditorScene extends Scene {
- private final String vertexShaderSrc =
- " #version 330\n" +
- " layout (location=0) in vec3 aPos;\n" +
- " layout (location=1) in vec4 aColour;\n" +
- "\n" +
- " out vec4 fColour;\n" +
- "\n" +
- " void main(){\n" +
- " fColour = aColour;\n" +
- " gl_Position = vec4(aPos, 1.0);\n" +
- " }";
- private final String fragmentShaderSrc =
- " #version 330\n" +
- " in vec4 fColour;\n" +
- " out vec4 colour;\n" +
- " void main(){\n" +
- " colour = fColour;\n" +
- " }";
- private int vertexID, fragmentID, shadersProgram;
- private float[] vertexArray = {
- /*
- 1 2
- 3 0
- */
- // position // colour
- 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Bottom right 0
- -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // Top left 1
- 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top right 2
- -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, // Bottom left 3
- };
- // Must be in counter-clockwise order
- private int[] elementArray = {
- 2, 1, 0, // Top right triangle
- 0, 1, 3, // Bottom left triangle
- };
- private int vaoID, vboID, eboID;
- @Override
- public void init() {
- //compile and link shaders
- //load and compile vertex shader
- vertexID = glCreateShader(GL_VERTEX_SHADER);
- //pass shader source to gpu
- glShaderSource(vertexID, vertexShaderSrc);
- glCompileShader(vertexID);
- int success = glGetShaderi(vertexID, GL_COMPILE_STATUS);
- if (success == GL_FALSE){
- int length = glGetShaderi(vertexID, GL_INFO_LOG_LENGTH);
- System.out.println("ERROR: 'defaultShader.glsl'\n\tVertexShader compilation failed.");
- System.out.println(glGetShaderInfoLog(vertexID, length));
- assert false : "";
- }
- //load and compile fragment shader
- fragmentID = glCreateShader(GL_FRAGMENT_SHADER);
- //pass shader source to gpu
- glShaderSource(fragmentID, fragmentShaderSrc);
- glCompileShader(fragmentID);
- success = glGetShaderi(fragmentID, GL_COMPILE_STATUS);
- if (success == GL_FALSE){
- int length = glGetShaderi(fragmentID, GL_INFO_LOG_LENGTH);
- System.out.println("ERROR: 'defaultShader.glsl'\n\tFragmentShader compilation failed.");
- System.out.println(glGetShaderInfoLog(fragmentID, length));
- assert false : "";
- }
- //link shaders and check for errors
- shadersProgram = glCreateProgram();
- glAttachShader(shadersProgram, vertexID);
- glAttachShader(shadersProgram, fragmentID);
- glLinkProgram(shadersProgram);
- //check for linking error
- success = glGetProgrami(shadersProgram, GL_LINK_STATUS);
- if (success == GL_FALSE){
- int length = glGetProgrami(shadersProgram, GL_INFO_LOG_LENGTH);
- System.out.println("ERROR: 'defaultShader.glsl'\n\tLinking of shaders failed.");
- System.out.println(glGetProgramInfoLog(fragmentID, length));
- assert false : "";
- }
- // Generate VAO, VBO, EBO buffer objects and send to GPU
- vaoID = glGenVertexArrays();
- glBindVertexArray(vaoID);
- // create float buffer of vertices
- FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(vertexArray.length);
- vertexBuffer.put(vertexArray).flip();
- // create VBO upload vertex buffer
- vboID = glGenBuffers();
- glBindBuffer(GL_ARRAY_BUFFER, vboID);
- glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
- // create indices and upload
- IntBuffer elementBuffer = BufferUtils.createIntBuffer(elementArray.length);
- elementBuffer.put(elementArray).flip();
- eboID = glGenBuffers();
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboID);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW);
- // add vertex attribute pointers
- int positionSize = 3;
- int colourSize = 4;
- int floatSizeBytes = 4;
- int vertexSizeBytes = (positionSize + colourSize) * floatSizeBytes;
- glVertexAttribPointer(0, positionSize, GL_FLOAT, false, vertexSizeBytes, 0);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(1, colourSize, GL_FLOAT, false, vertexSizeBytes, positionSize * vertexSizeBytes);
- glEnableVertexAttribArray(1);
- }
- public LevelEditorScene() {
- }
- @Override
- public void update(float dt) {
- // Bind shader program
- glUseProgram(shadersProgram);
- // Bind VAO
- glBindVertexArray(vaoID);
- // enable vertex attribute pointers
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glDrawElements(GL_TRIANGLES, elementArray.length, GL_UNSIGNED_INT, 0);
- // unbind
- glDisableVertexAttribArray(0);
- glDisableVertexAttribArray(1);
- glBindVertexArray(0);
- glUseProgram(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement