Fixed chunk rendering flow.
This commit is contained in:
		
							parent
							
								
									d34a4aa017
								
							
						
					
					
						commit
						be6832c6a4
					
				| 
						 | 
				
			
			@ -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);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,6 +4,6 @@ import java.nio.FloatBuffer;
 | 
			
		|||
import java.nio.IntBuffer;
 | 
			
		||||
 | 
			
		||||
public record ChunkMeshData(
 | 
			
		||||
		FloatBuffer vertexData,
 | 
			
		||||
		IntBuffer indices
 | 
			
		||||
		FloatBuffer vertexBuffer,
 | 
			
		||||
		IntBuffer indexBuffer
 | 
			
		||||
) {}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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];
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue