Improve Rendering Performance

This commit is contained in:
Sebastian 2023-08-04 08:10:38 +02:00
parent 1303d0d05e
commit 76e268e9ed
3 changed files with 29 additions and 6 deletions

View File

@ -106,11 +106,13 @@ public class Engine {
-0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
0.5f, 0.5f, 0f,
-0.5f, 0.5f, 0f
};
RawModel model = loader.loadToVAO(vertices);
int[] indices = {
0,1,3,
3,1,2
};
RawModel model = loader.loadToVAO(vertices, indices);
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while ( !glfwWindowShouldClose(window) ) {

View File

@ -8,6 +8,7 @@ import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;
@ -16,11 +17,13 @@ public class Loader {
private List<Integer> vaos = new ArrayList<>();
private List<Integer> vbos = new ArrayList<>();
public RawModel loadToVAO(float[] positions) {
public RawModel loadToVAO(float[] positions, int[] indices) {
int vaoID = createVAO();
bindIndicesBuffer(indices);
storeDataInAttributeList(0, positions);
unbindVAO();
return new RawModel(vaoID, positions.length/3);
return new RawModel(vaoID, indices.length);
}
private int createVAO() {
@ -44,6 +47,22 @@ public class Loader {
GL30.glBindVertexArray(0);
}
private void bindIndicesBuffer(int[] indices) {
int vboID = GL15.glGenBuffers();
vbos.add(vboID);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
IntBuffer buffer = storeInIntBuffer(indices);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW);
}
private IntBuffer storeInIntBuffer(int[] data) {
IntBuffer intBuffer = BufferUtils.createIntBuffer(data.length);
intBuffer.put(data);
//Buffer is until know expected to be written to. Flip it, so it can be read
intBuffer.flip();
return intBuffer;
}
private FloatBuffer storeInFloatBuffer(float[] data) {
FloatBuffer floatBuffer = BufferUtils.createFloatBuffer(data.length);
floatBuffer.put(data);
@ -52,6 +71,7 @@ public class Loader {
return floatBuffer;
}
public void cleanUp() {
for(int vao: vaos) {
GL30.glDeleteVertexArrays(vao);

View File

@ -8,13 +8,14 @@ import org.lwjgl.opengl.GL30;
public class Renderer {
public void prepare() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glClearColor(1,0,0,1);
}
public void render(RawModel rawModel) {
GL30.glBindVertexArray(rawModel.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, rawModel.getVertexCount());
GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
}