Hacked together rainbow square
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package org.hirw.game;
|
||||
|
||||
|
||||
public abstract class Scene {
|
||||
public Scene() {}
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package org.hirw.game;
|
||||
|
||||
import static org.lwjgl.opengl.ARBVertexArrayObject.glBindVertexArray;
|
||||
import static org.lwjgl.opengl.ARBVertexArrayObject.glGenVertexArrays;
|
||||
import static org.lwjgl.opengl.GL20.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
@@ -13,6 +17,7 @@ import java.util.Map;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.hirw.game.util.Log;
|
||||
import org.lwjgl.BufferUtils;
|
||||
|
||||
public class Shader {
|
||||
private enum ShaderType {
|
||||
@@ -35,6 +40,20 @@ public class Shader {
|
||||
@Getter @Setter private int fragmentID;
|
||||
@Getter @Setter private int shaderProgramID;
|
||||
|
||||
private float[] vertexArray = {
|
||||
0.5f, -0.5f, 0.0f, /* */ 1.0f, 0.0f, 0.0f, 1.0f, //
|
||||
-0.5f, 0.5f, 0.0f, /* */ 0.0f, 1.0f, 0.0f, 1.0f, //
|
||||
0.5f, 0.5f, 0.0f, /* */ 0.0f, 0.0f, 1.0f, 1.0f, //
|
||||
-0.5f, -0.5f, 0.0f, /* */ 1.0f, 1.0f, 0.0f, 1.0f, //
|
||||
};
|
||||
|
||||
private int[] elementArray = {
|
||||
2, 1, 0, //
|
||||
0, 1, 3
|
||||
};
|
||||
|
||||
@Getter private int vaoID, vboID, eboID;
|
||||
|
||||
public Shader(String fragPath, String vertPath) {
|
||||
this.fragmentSource = readFromFile(fragPath);
|
||||
this.vertexSource = readFromFile(vertPath);
|
||||
@@ -48,6 +67,7 @@ public class Shader {
|
||||
compileShader(ShaderType.FRAG);
|
||||
compileShader(ShaderType.VERT);
|
||||
createProgram();
|
||||
thingy();
|
||||
}
|
||||
|
||||
private void compileShader(ShaderType shaderType) {
|
||||
@@ -112,4 +132,46 @@ public class Shader {
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
private void thingy() {
|
||||
this.vaoID = glGenVertexArrays();
|
||||
glBindVertexArray(this.vaoID);
|
||||
|
||||
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(this.vertexArray.length);
|
||||
vertexBuffer.put(this.vertexArray).flip();
|
||||
|
||||
this.vboID = glGenBuffers();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this.vboID);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
|
||||
|
||||
IntBuffer elementBuffer = BufferUtils.createIntBuffer(this.elementArray.length);
|
||||
elementBuffer.put(this.elementArray).flip();
|
||||
|
||||
this.eboID = glGenBuffers();
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this.eboID);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW);
|
||||
|
||||
int positionsSize = 3;
|
||||
int colourSize = 4;
|
||||
int floatSizeBytes = 4;
|
||||
int vertexSizeBytes = (positionsSize + colourSize) * floatSizeBytes;
|
||||
glVertexAttribPointer(0, positionsSize, GL_FLOAT, false, vertexSizeBytes, 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(
|
||||
1, colourSize, GL_FLOAT, false, vertexSizeBytes, positionsSize * floatSizeBytes);
|
||||
glEnableVertexAttribArray(1);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
glUseProgram(this.shaderProgramID);
|
||||
glBindVertexArray(this.vaoID);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glDrawElements(GL_TRIANGLES, this.elementArray.length, GL_UNSIGNED_INT, 0);
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glBindVertexArray(0);
|
||||
glUseProgram(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ public class Window {
|
||||
|
||||
private static Window window = null;
|
||||
|
||||
private Shader shader;
|
||||
|
||||
private Window() {
|
||||
this.width = 1280;
|
||||
this.height = 720;
|
||||
@@ -81,6 +83,7 @@ public class Window {
|
||||
GL.createCapabilities();
|
||||
glClearColor(0.0f, 0.0f, 2.0f, 0.0f);
|
||||
Shader someShader = new Shader();
|
||||
this.shader = someShader;
|
||||
someShader.init();
|
||||
}
|
||||
|
||||
@@ -89,6 +92,7 @@ public class Window {
|
||||
Time.update();
|
||||
SceneManager.getScene().update();
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
this.shader.update();
|
||||
glfwSwapBuffers(glfwWindow);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user