Separate mesh logic out of Shader class into Mesh class
This commit is contained in:
106
src/main/java/org/hirw/game/Mesh.java
Normal file
106
src/main/java/org/hirw/game/Mesh.java
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
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.nio.FloatBuffer;
|
||||||
|
import java.nio.IntBuffer;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.lwjgl.BufferUtils;
|
||||||
|
|
||||||
|
public class Mesh {
|
||||||
|
@Getter @Setter private int vaoID, vboID, eboID;
|
||||||
|
@Getter private Shader shader;
|
||||||
|
@Getter float[] vertices;
|
||||||
|
@Getter int[] elements;
|
||||||
|
|
||||||
|
private static final float[] defaultVertexArray = {
|
||||||
|
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 static final int[] defaultElementArray = {
|
||||||
|
2, 1, 0,
|
||||||
|
0, 1, 3
|
||||||
|
};
|
||||||
|
|
||||||
|
public Mesh(Shader shader, float[] vertexArray, int[] elementArray) {
|
||||||
|
this.shader = shader;
|
||||||
|
this.vaoID = -1;
|
||||||
|
this.vboID = -1;
|
||||||
|
this.eboID = -1;
|
||||||
|
this.vertices = vertexArray;
|
||||||
|
this.elements = elementArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mesh(Shader shader) {
|
||||||
|
this(shader, defaultVertexArray, defaultElementArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
initialiseVertexArrayObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw() {
|
||||||
|
getShader().use();
|
||||||
|
glDraw();
|
||||||
|
getShader().detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void log() {
|
||||||
|
System.out.println(this.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return String.format(
|
||||||
|
"VAO: %d, VBO: %d, EBO: %d, ShaderProgram: %d",
|
||||||
|
getEboID(), getVboID(), getEboID(), getShader().getShaderProgramID());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void glDraw() {
|
||||||
|
glBindVertexArray(this.vaoID);
|
||||||
|
glDrawElements(GL_TRIANGLES, getElements().length, GL_UNSIGNED_INT, 0);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialiseVertexArrayObject() {
|
||||||
|
setVaoID(glGenVertexArrays());
|
||||||
|
glBindVertexArray(getVaoID());
|
||||||
|
|
||||||
|
initialiseVertexBufferObject();
|
||||||
|
initialiseElementBufferObject();
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialiseVertexBufferObject() {
|
||||||
|
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(getVertices().length);
|
||||||
|
vertexBuffer.put(getVertices()).flip();
|
||||||
|
|
||||||
|
setVboID(glGenBuffers());
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, getVboID());
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialiseElementBufferObject() {
|
||||||
|
IntBuffer elementBuffer = BufferUtils.createIntBuffer(getElements().length);
|
||||||
|
elementBuffer.put(getElements()).flip();
|
||||||
|
|
||||||
|
setEboID(glGenBuffers());
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, getEboID());
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,8 @@
|
|||||||
package org.hirw.game;
|
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 static org.lwjgl.opengl.GL20.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.FloatBuffer;
|
|
||||||
import java.nio.IntBuffer;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.InvalidPathException;
|
import java.nio.file.InvalidPathException;
|
||||||
import java.nio.file.NoSuchFileException;
|
import java.nio.file.NoSuchFileException;
|
||||||
@@ -17,7 +13,6 @@ import java.util.Map;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.hirw.game.util.Log;
|
import org.hirw.game.util.Log;
|
||||||
import org.lwjgl.BufferUtils;
|
|
||||||
|
|
||||||
public class Shader {
|
public class Shader {
|
||||||
private enum ShaderType {
|
private enum ShaderType {
|
||||||
@@ -39,19 +34,6 @@ public class Shader {
|
|||||||
@Getter @Setter private int vertexID;
|
@Getter @Setter private int vertexID;
|
||||||
@Getter @Setter private int fragmentID;
|
@Getter @Setter private int fragmentID;
|
||||||
@Getter @Setter private int shaderProgramID;
|
@Getter @Setter private int shaderProgramID;
|
||||||
@Getter @Setter private int vaoID, vboID, eboID;
|
|
||||||
|
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
public Shader(String fragPath, String vertPath) {
|
public Shader(String fragPath, String vertPath) {
|
||||||
this.fragmentSource = readFromFile(fragPath);
|
this.fragmentSource = readFromFile(fragPath);
|
||||||
@@ -66,7 +48,14 @@ public class Shader {
|
|||||||
compileShader(ShaderType.FRAG);
|
compileShader(ShaderType.FRAG);
|
||||||
compileShader(ShaderType.VERT);
|
compileShader(ShaderType.VERT);
|
||||||
createProgram();
|
createProgram();
|
||||||
thingy();
|
}
|
||||||
|
|
||||||
|
public void use() {
|
||||||
|
glUseProgram(getShaderProgramID());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void detach() {
|
||||||
|
glUseProgram(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void compileShader(ShaderType shaderType) {
|
private void compileShader(ShaderType shaderType) {
|
||||||
@@ -133,54 +122,4 @@ public class Shader {
|
|||||||
|
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void thingy() {
|
|
||||||
setVaoID(glGenVertexArrays());
|
|
||||||
glBindVertexArray(getVaoID());
|
|
||||||
|
|
||||||
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(this.vertexArray.length);
|
|
||||||
vertexBuffer.put(this.vertexArray).flip();
|
|
||||||
|
|
||||||
setVboID(glGenBuffers());
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, getVboID());
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
IntBuffer elementBuffer = BufferUtils.createIntBuffer(this.elementArray.length);
|
|
||||||
elementBuffer.put(this.elementArray).flip();
|
|
||||||
|
|
||||||
setEboID(glGenBuffers());
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, getEboID());
|
|
||||||
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 use() {
|
|
||||||
glUseProgram(getShaderProgramID());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void detach() {
|
|
||||||
glUseProgram(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update() {
|
|
||||||
glUseProgram(getShaderProgramID());
|
|
||||||
glBindVertexArray(getVaoID());
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
glDrawElements(GL_TRIANGLES, this.elementArray.length, GL_UNSIGNED_INT, 0);
|
|
||||||
glDisableVertexAttribArray(0);
|
|
||||||
glDisableVertexAttribArray(1);
|
|
||||||
glBindVertexArray(0);
|
|
||||||
glUseProgram(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import static org.lwjgl.opengl.GL11.*;
|
|||||||
import static org.lwjgl.system.MemoryUtil.*;
|
import static org.lwjgl.system.MemoryUtil.*;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import org.hirw.game.util.Time;
|
import org.hirw.game.util.Time;
|
||||||
import org.lwjgl.Version;
|
import org.lwjgl.Version;
|
||||||
import org.lwjgl.glfw.*;
|
import org.lwjgl.glfw.*;
|
||||||
@@ -18,7 +19,8 @@ public class Window {
|
|||||||
|
|
||||||
private static Window window = null;
|
private static Window window = null;
|
||||||
|
|
||||||
private Shader shader;
|
@Getter @Setter private Shader shader;
|
||||||
|
@Getter @Setter private Mesh mesh;
|
||||||
|
|
||||||
private Window() {
|
private Window() {
|
||||||
this.width = 1280;
|
this.width = 1280;
|
||||||
@@ -43,6 +45,7 @@ public class Window {
|
|||||||
logVersion();
|
logVersion();
|
||||||
createWindow();
|
createWindow();
|
||||||
createShader();
|
createShader();
|
||||||
|
createMesh();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logVersion() {
|
private void logVersion() {
|
||||||
@@ -74,9 +77,16 @@ public class Window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void createShader() {
|
private void createShader() {
|
||||||
Shader someShader = new Shader();
|
Shader shader = new Shader();
|
||||||
this.shader = someShader;
|
setShader(shader);
|
||||||
someShader.init();
|
shader.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createMesh() {
|
||||||
|
Mesh mesh = new Mesh(getShader());
|
||||||
|
setMesh(mesh);
|
||||||
|
mesh.init();
|
||||||
|
mesh.log();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loop() {
|
private void loop() {
|
||||||
@@ -84,7 +94,7 @@ public class Window {
|
|||||||
Time.update();
|
Time.update();
|
||||||
SceneManager.update();
|
SceneManager.update();
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
this.shader.update();
|
getMesh().draw();
|
||||||
glfwSwapBuffers(glfwWindow);
|
glfwSwapBuffers(glfwWindow);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user