Added ability to render multiple chunks. There are bugs in mesh rendering though.

This commit is contained in:
Andrew Lalis 2022-07-04 22:53:02 +02:00
parent 297fcb5a1e
commit 969576b34d
5 changed files with 42 additions and 14 deletions

View File

@ -18,24 +18,30 @@ public class Aos2Client {
public static void main(String[] args) { public static void main(String[] args) {
long windowHandle = initUI(); long windowHandle = initUI();
Chunk chunk = Chunk.random(new Vector3i(0, 0, 0), new Random(1));
Camera cam = new Camera(); Camera cam = new Camera();
glfwSetCursorPosCallback(windowHandle, cam); glfwSetCursorPosCallback(windowHandle, cam);
Chunk chunk = Chunk.random(new Vector3i(0, 0, 0), new Random(1));
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
chunk.setBlockAt(i, 0, 0, (byte) 8); chunk.setBlockAt(i, 0, 0, (byte) 8);
chunk.setBlockAt(0, i, 0, (byte) 40); chunk.setBlockAt(0, i, 0, (byte) 40);
chunk.setBlockAt(0, 0, i, (byte) 120); chunk.setBlockAt(0, 0, i, (byte) 120);
} }
chunk.setBlockAt(0, 15, 0, (byte) 0); // chunk.setBlockAt(0, 15, 0, (byte) 0);
chunk.setBlockAt(1, 15, 0, (byte) 0); // chunk.setBlockAt(1, 15, 0, (byte) 0);
chunk.setBlockAt(2, 15, 0, (byte) 0); // chunk.setBlockAt(2, 15, 0, (byte) 0);
chunk.setBlockAt(2, 15, 1, (byte) 0); // chunk.setBlockAt(2, 15, 1, (byte) 0);
chunk.setBlockAt(0, 0, 0, (byte) 0); // chunk.setBlockAt(0, 0, 0, (byte) 0);
Chunk chunk2 = Chunk.random(new Vector3i(1, 0, 0), new Random(1));
Chunk chunk3 = Chunk.random(new Vector3i(1, 0, 1), new Random(1));
Chunk chunk4 = Chunk.random(new Vector3i(0, 0, 1), new Random(1));
ChunkRenderer chunkRenderer = new ChunkRenderer(); ChunkRenderer chunkRenderer = new ChunkRenderer();
ChunkMesh mesh = new ChunkMesh(chunk);
chunkRenderer.addChunkMesh(mesh); chunkRenderer.addChunkMesh(new ChunkMesh(chunk2));
chunkRenderer.addChunkMesh(new ChunkMesh(chunk3));
chunkRenderer.addChunkMesh(new ChunkMesh(chunk4));
chunkRenderer.addChunkMesh(new ChunkMesh(chunk));
while (!glfwWindowShouldClose(windowHandle)) { while (!glfwWindowShouldClose(windowHandle)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

View File

@ -3,6 +3,7 @@ package nl.andrewl.aos2_client;
import nl.andrewl.aos_core.Pair; import nl.andrewl.aos_core.Pair;
import nl.andrewl.aos_core.model.Chunk; import nl.andrewl.aos_core.model.Chunk;
import org.joml.Vector3f; import org.joml.Vector3f;
import org.joml.Vector3i;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -10,14 +11,20 @@ import java.util.stream.Stream;
import static org.lwjgl.opengl.GL46.*; import static org.lwjgl.opengl.GL46.*;
/**
* Represents a 3d mesh for a chunk.
*/
public class ChunkMesh { public class ChunkMesh {
private final int vboId; private final int vboId;
private final int vaoId; private final int vaoId;
private final int eboId; private final int eboId;
private final int indiciesCount; private int indiciesCount;
private final int[] positionData;
public ChunkMesh(Chunk chunk) { public ChunkMesh(Chunk chunk) {
this.positionData = new int[]{chunk.getPosition().x, chunk.getPosition().y, chunk.getPosition().z};
this.vboId = glGenBuffers(); this.vboId = glGenBuffers();
this.vaoId = glGenVertexArrays(); this.vaoId = glGenVertexArrays();
this.eboId = glGenBuffers(); this.eboId = glGenBuffers();
@ -148,6 +155,10 @@ public class ChunkMesh {
return new Pair<>(vertexData, indexData); return new Pair<>(vertexData, indexData);
} }
public int[] getPositionData() {
return positionData;
}
public void draw() { public void draw() {
// Bind elements. // Bind elements.
glBindBuffer(GL_ARRAY_BUFFER, vboId); glBindBuffer(GL_ARRAY_BUFFER, vboId);

View File

@ -13,6 +13,7 @@ public class ChunkRenderer {
private final int projectionTransformUniform; private final int projectionTransformUniform;
private final int viewTransformUniform; private final int viewTransformUniform;
private final int normalTransformUniform; private final int normalTransformUniform;
private final int chunkPositionUniform;
private final Matrix4f projectionTransform = new Matrix4f().perspective(70, 800 / 600.0f, 0.01f, 100.0f); private final Matrix4f projectionTransform = new Matrix4f().perspective(70, 800 / 600.0f, 0.01f, 100.0f);
@ -27,6 +28,7 @@ public class ChunkRenderer {
this.projectionTransformUniform = shaderProgram.getUniform("projectionTransform"); this.projectionTransformUniform = shaderProgram.getUniform("projectionTransform");
this.viewTransformUniform = shaderProgram.getUniform("viewTransform"); this.viewTransformUniform = shaderProgram.getUniform("viewTransform");
this.normalTransformUniform = shaderProgram.getUniform("normalTransform"); this.normalTransformUniform = shaderProgram.getUniform("normalTransform");
this.chunkPositionUniform = shaderProgram.getUniform("chunkPosition");
// Preemptively load projection transform, which doesn't change much. // Preemptively load projection transform, which doesn't change much.
glUniformMatrix4fv(projectionTransformUniform, false, projectionTransform.get(new float[16])); glUniformMatrix4fv(projectionTransformUniform, false, projectionTransform.get(new float[16]));
@ -42,7 +44,11 @@ public class ChunkRenderer {
glUniformMatrix3fv(normalTransformUniform, false, normalTransform.get(new float[9])); glUniformMatrix3fv(normalTransformUniform, false, normalTransform.get(new float[9]));
shaderProgram.use(); shaderProgram.use();
for (var mesh : chunkMeshes) mesh.draw(); for (var mesh : chunkMeshes) {
// For each chunk, specify its position so that the shaders can draw it offset.
glUniform3iv(chunkPositionUniform, mesh.getPositionData());
mesh.draw();
}
} }
public void free() { public void free() {

View File

@ -7,15 +7,17 @@ layout (location = 2) in vec3 vertexNormalIn;
uniform mat4 projectionTransform; uniform mat4 projectionTransform;
uniform mat4 viewTransform; uniform mat4 viewTransform;
uniform mat3 normalTransform; uniform mat3 normalTransform;
uniform ivec3 chunkPosition;
out vec3 vertexPosition; out vec3 vertexPosition;
out vec3 vertexColor; out vec3 vertexColor;
out vec3 vertexNormal; out vec3 vertexNormal;
void main() { void main() {
// TODO: Add model transform uniform. vec3 realVertexPosition = vertexPositionIn + (chunkPosition * 16);
gl_Position = projectionTransform * viewTransform * vec4(vertexPositionIn, 1.0);
vertexPosition = vertexPositionIn; gl_Position = projectionTransform * viewTransform * vec4(realVertexPosition, 1.0);
vertexPosition = realVertexPosition;
vertexColor = vertexColorIn; vertexColor = vertexColorIn;
vertexNormal = normalize(normalTransform * vertexNormalIn); vertexNormal = normalize(normalTransform * vertexNormalIn);
} }

View File

@ -3,7 +3,6 @@ package nl.andrewl.aos_core.model;
import org.joml.Vector3f; import org.joml.Vector3f;
import org.joml.Vector3i; import org.joml.Vector3i;
import java.util.Arrays;
import java.util.Random; import java.util.Random;
/** /**
@ -32,6 +31,10 @@ public class Chunk {
System.arraycopy(other.blocks, 0, this.blocks, 0, TOTAL_SIZE); System.arraycopy(other.blocks, 0, this.blocks, 0, TOTAL_SIZE);
} }
public Vector3i getPosition() {
return position;
}
public byte getBlockAt(int x, int y, int z) { public byte getBlockAt(int x, int y, int z) {
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || z < 0 || z >= SIZE) return 0; if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || z < 0 || z >= SIZE) return 0;
int idx = x * SIZE * SIZE + y * SIZE + z; int idx = x * SIZE * SIZE + y * SIZE + z;