From be6832c6a42c74418e83dbfbc8f56016e3ec67d3 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Tue, 5 Jul 2022 16:42:45 +0200 Subject: [PATCH] Fixed chunk rendering flow. --- .../andrewl/aos2_client/render/ChunkMesh.java | 37 +++++++------------ .../aos2_client/render/ChunkMeshData.java | 4 +- .../render/ChunkMeshGenerator.java | 4 +- .../aos2_client/render/ChunkRenderer.java | 3 +- .../java/nl/andrewl/aos_core/model/Chunk.java | 2 +- 5 files changed, 19 insertions(+), 31 deletions(-) diff --git a/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMesh.java b/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMesh.java index 9c1413a..0a2def4 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMesh.java +++ b/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMesh.java @@ -2,8 +2,6 @@ package nl.andrewl.aos2_client.render; import nl.andrewl.aos_core.model.Chunk; -import java.util.Arrays; - import static org.lwjgl.opengl.GL46.*; /** @@ -36,34 +34,23 @@ public class ChunkMesh { return positionData; } + /** + * Generates and loads this chunk's mesh into the allocated OpenGL buffers. + */ private void loadMesh() { - long start = System.currentTimeMillis(); var meshData = ChunkMeshGenerator.generateMesh(chunk); - long dur = System.currentTimeMillis() - start; - System.out.printf( - "Generated chunk mesh in %d ms with %d vertices and %d indices, and %d faces. Vertex data size: %d%n", - dur, - meshData.vertexData().limit() / 9, - meshData.indices().limit(), - meshData.indices().limit() / 4, - meshData.vertexData().limit() - ); - this.indiciesCount = meshData.indices().limit(); - int[] data = new int[indiciesCount]; - meshData.indices().get(data); - meshData.indices().flip(); - System.out.println(Arrays.toString(data)); + this.indiciesCount = meshData.indexBuffer().limit(); glBindBuffer(GL_ARRAY_BUFFER, vboId); - glBufferData(GL_ARRAY_BUFFER, meshData.vertexData(), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, meshData.vertexBuffer(), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, meshData.indices(), GL_STATIC_DRAW); - - int size = glGetBufferParameteri(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE); - System.out.println(size); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, meshData.indexBuffer(), GL_STATIC_DRAW); } + /** + * Initializes this mesh's vertex array attribute settings. + */ private void initVertexArrayAttributes() { glBindVertexArray(vaoId); // Vertex position floats. @@ -77,10 +64,12 @@ public class ChunkMesh { glVertexAttribPointer(2, 3, GL_FLOAT, false, 9 * Float.BYTES, 6 * Float.BYTES); } + /** + * Draws the chunk mesh. + */ public void draw() { - glBindBuffer(GL_ARRAY_BUFFER, vboId); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId); glBindVertexArray(vaoId); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId); glDrawElements(GL_TRIANGLES, indiciesCount, GL_UNSIGNED_INT, 0); } diff --git a/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMeshData.java b/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMeshData.java index 13f5510..6bb789e 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMeshData.java +++ b/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMeshData.java @@ -4,6 +4,6 @@ import java.nio.FloatBuffer; import java.nio.IntBuffer; public record ChunkMeshData( - FloatBuffer vertexData, - IntBuffer indices + FloatBuffer vertexBuffer, + IntBuffer indexBuffer ) {} diff --git a/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMeshGenerator.java b/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMeshGenerator.java index 67e750d..8bceb91 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMeshGenerator.java +++ b/client/src/main/java/nl/andrewl/aos2_client/render/ChunkMeshGenerator.java @@ -13,8 +13,8 @@ public final class ChunkMeshGenerator { private ChunkMeshGenerator() {} public static ChunkMeshData generateMesh(Chunk chunk) { - FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(20000); - IntBuffer indexBuffer = BufferUtils.createIntBuffer(5000); + FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(300000); + IntBuffer indexBuffer = BufferUtils.createIntBuffer(100000); int idx = 0; for (int i = 0; i < Chunk.TOTAL_SIZE; i++) { var pos = Chunk.idxToXyz(i); diff --git a/client/src/main/java/nl/andrewl/aos2_client/render/ChunkRenderer.java b/client/src/main/java/nl/andrewl/aos2_client/render/ChunkRenderer.java index db89aaa..bebfae4 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/render/ChunkRenderer.java +++ b/client/src/main/java/nl/andrewl/aos2_client/render/ChunkRenderer.java @@ -2,7 +2,6 @@ package nl.andrewl.aos2_client.render; import nl.andrewl.aos2_client.Camera; import nl.andrewl.aos_core.model.Chunk; -import org.joml.Matrix3f; import org.joml.Matrix4f; import java.util.ArrayList; @@ -25,7 +24,7 @@ public class ChunkRenderer { public ChunkRenderer() { this.shaderProgram = new ShaderProgram.Builder() .withShader("shader/chunk/vertex.glsl", GL_VERTEX_SHADER) - .withShader("shader/chunk/normal_fragment.glsl", GL_FRAGMENT_SHADER) + .withShader("shader/chunk/fragment.glsl", GL_FRAGMENT_SHADER) .build(); shaderProgram.use(); this.projectionTransformUniform = shaderProgram.getUniform("projectionTransform"); diff --git a/core/src/main/java/nl/andrewl/aos_core/model/Chunk.java b/core/src/main/java/nl/andrewl/aos_core/model/Chunk.java index cbdee1a..5eba16b 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/Chunk.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/Chunk.java @@ -12,7 +12,7 @@ public class Chunk { /** * The size of a chunk, in terms of the number of blocks on one axis of the cube. */ - public static final int SIZE = 4; + public static final int SIZE = 16; public static final int TOTAL_SIZE = SIZE * SIZE * SIZE; private final byte[] blocks = new byte[TOTAL_SIZE];