Fixed chunk rendering flow.

This commit is contained in:
Andrew Lalis 2022-07-05 16:42:45 +02:00
parent d34a4aa017
commit be6832c6a4
5 changed files with 19 additions and 31 deletions

View File

@ -2,8 +2,6 @@ package nl.andrewl.aos2_client.render;
import nl.andrewl.aos_core.model.Chunk; import nl.andrewl.aos_core.model.Chunk;
import java.util.Arrays;
import static org.lwjgl.opengl.GL46.*; import static org.lwjgl.opengl.GL46.*;
/** /**
@ -36,34 +34,23 @@ public class ChunkMesh {
return positionData; return positionData;
} }
/**
* Generates and loads this chunk's mesh into the allocated OpenGL buffers.
*/
private void loadMesh() { private void loadMesh() {
long start = System.currentTimeMillis();
var meshData = ChunkMeshGenerator.generateMesh(chunk); var meshData = ChunkMeshGenerator.generateMesh(chunk);
long dur = System.currentTimeMillis() - start; this.indiciesCount = meshData.indexBuffer().limit();
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));
glBindBuffer(GL_ARRAY_BUFFER, vboId); 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); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, meshData.indices(), GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, meshData.indexBuffer(), GL_STATIC_DRAW);
int size = glGetBufferParameteri(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE);
System.out.println(size);
} }
/**
* Initializes this mesh's vertex array attribute settings.
*/
private void initVertexArrayAttributes() { private void initVertexArrayAttributes() {
glBindVertexArray(vaoId); glBindVertexArray(vaoId);
// Vertex position floats. // Vertex position floats.
@ -77,10 +64,12 @@ public class ChunkMesh {
glVertexAttribPointer(2, 3, GL_FLOAT, false, 9 * Float.BYTES, 6 * Float.BYTES); glVertexAttribPointer(2, 3, GL_FLOAT, false, 9 * Float.BYTES, 6 * Float.BYTES);
} }
/**
* Draws the chunk mesh.
*/
public void draw() { public void draw() {
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId);
glBindVertexArray(vaoId); glBindVertexArray(vaoId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId);
glDrawElements(GL_TRIANGLES, indiciesCount, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, indiciesCount, GL_UNSIGNED_INT, 0);
} }

View File

@ -4,6 +4,6 @@ import java.nio.FloatBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
public record ChunkMeshData( public record ChunkMeshData(
FloatBuffer vertexData, FloatBuffer vertexBuffer,
IntBuffer indices IntBuffer indexBuffer
) {} ) {}

View File

@ -13,8 +13,8 @@ public final class ChunkMeshGenerator {
private ChunkMeshGenerator() {} private ChunkMeshGenerator() {}
public static ChunkMeshData generateMesh(Chunk chunk) { public static ChunkMeshData generateMesh(Chunk chunk) {
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(20000); FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(300000);
IntBuffer indexBuffer = BufferUtils.createIntBuffer(5000); IntBuffer indexBuffer = BufferUtils.createIntBuffer(100000);
int idx = 0; int idx = 0;
for (int i = 0; i < Chunk.TOTAL_SIZE; i++) { for (int i = 0; i < Chunk.TOTAL_SIZE; i++) {
var pos = Chunk.idxToXyz(i); var pos = Chunk.idxToXyz(i);

View File

@ -2,7 +2,6 @@ package nl.andrewl.aos2_client.render;
import nl.andrewl.aos2_client.Camera; import nl.andrewl.aos2_client.Camera;
import nl.andrewl.aos_core.model.Chunk; import nl.andrewl.aos_core.model.Chunk;
import org.joml.Matrix3f;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import java.util.ArrayList; import java.util.ArrayList;
@ -25,7 +24,7 @@ public class ChunkRenderer {
public ChunkRenderer() { public ChunkRenderer() {
this.shaderProgram = new ShaderProgram.Builder() this.shaderProgram = new ShaderProgram.Builder()
.withShader("shader/chunk/vertex.glsl", GL_VERTEX_SHADER) .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(); .build();
shaderProgram.use(); shaderProgram.use();
this.projectionTransformUniform = shaderProgram.getUniform("projectionTransform"); this.projectionTransformUniform = shaderProgram.getUniform("projectionTransform");

View File

@ -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. * 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; public static final int TOTAL_SIZE = SIZE * SIZE * SIZE;
private final byte[] blocks = new byte[TOTAL_SIZE]; private final byte[] blocks = new byte[TOTAL_SIZE];