From b4dd3be584e886af4190bf7992befe0798ff27c3 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Sun, 26 Mar 2023 19:57:39 +0200 Subject: [PATCH] chunkmesher: push vertices normals to GPU --- include/chunkmesher.hpp | 3 ++- shaders/shader.fs | 4 +++- shaders/shader.vs | 9 +++++++-- src/chunkmesher.cpp | 34 ++++++++++++++++++++++++++++------ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/include/chunkmesher.hpp b/include/chunkmesher.hpp index 3f985e9..5f50b98 100644 --- a/include/chunkmesher.hpp +++ b/include/chunkmesher.hpp @@ -16,7 +16,8 @@ namespace chunkmesher{ void sendtogpu(Chunk::Chunk* chunk); void draw(Chunk::Chunk* chunk, glm::mat4 model); - void quad(Chunk::Chunk* chunk, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, glm::vec3 bottomRight, Block block, bool backFace); + void quad(Chunk::Chunk* chunk, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, + glm::vec3 bottomRight, glm::vec3 normal, Block block, bool backFace); } diff --git a/shaders/shader.fs b/shaders/shader.fs index 8e6a06a..6b13a29 100644 --- a/shaders/shader.fs +++ b/shaders/shader.fs @@ -1,10 +1,12 @@ #version 330 core in vec4 vColor; +in vec3 vNormal; + out vec4 FragColor; void main() { FragColor = vColor; // FragColor= vec4(1.0f, 1.0f, 1.0f, 1.0f); -} \ No newline at end of file +} diff --git a/shaders/shader.vs b/shaders/shader.vs index af49fa5..ae918f5 100644 --- a/shaders/shader.vs +++ b/shaders/shader.vs @@ -1,15 +1,20 @@ #version 330 core + layout (location = 0) in vec3 aPos; -layout (location = 1) in vec3 aColor; +layout (location = 1) in vec3 aNormal; +layout (location = 2) in vec3 aColor; uniform mat4 model; uniform mat4 view; uniform mat4 projection; out vec4 vColor; +out vec3 vNormal; void main() { vColor = vec4(aColor, 1.0f); + vNormal = aNormal; + gl_Position = projection * view * model * vec4(aPos, 1.0); -} \ No newline at end of file +} diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index 800bf1e..623fab2 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -151,6 +151,7 @@ void mesh(Chunk::Chunk* chunk) glm::vec3(x[0] + du[0] + dv[0], x[1] + du[1] + dv[1], x[2] + du[2] + dv[2]), glm::vec3(x[0] + dv[0], x[1] + dv[1], x[2] + dv[2]), + glm::vec3(backFace ? q[0] : -q[0], backFace ? q[1] : -q[1], backFace ? q[2] : -q[2] ), mask[n], backFace); } @@ -192,17 +193,24 @@ void sendtogpu(Chunk::Chunk* chunk) glBindBuffer(GL_ARRAY_BUFFER, chunk->VBO); glBufferData(GL_ARRAY_BUFFER, chunk->vertices.size() * sizeof(GLfloat), &(chunk->vertices[0]), GL_STATIC_DRAW); + // position attribute + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0); + glEnableVertexAttribArray(0); + + // normal attribute + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3* + sizeof(float))); + glEnableVertexAttribArray(1); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, chunk->EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, chunk->indices.size() * sizeof(GLuint), &(chunk->indices[0]), GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); - glEnableVertexAttribArray(0); - + // color attribute glBindBuffer(GL_ARRAY_BUFFER, chunk->colorBuffer); glBufferData(GL_ARRAY_BUFFER, chunk->colors.size() * sizeof(GLfloat), &(chunk->colors[0]), GL_STATIC_DRAW); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); glBindVertexArray(0); @@ -236,24 +244,38 @@ void draw(Chunk::Chunk* chunk, glm::mat4 model) } } -void quad(Chunk::Chunk* chunk, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, glm::vec3 bottomRight, Block block, bool backFace) +void quad(Chunk::Chunk* chunk, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, + glm::vec3 bottomRight, glm::vec3 normal, Block block, bool backFace) { chunk->vertices.push_back(bottomLeft.x); chunk->vertices.push_back(bottomLeft.y); chunk->vertices.push_back(bottomLeft.z); + chunk->vertices.push_back(normal.x); + chunk->vertices.push_back(normal.y); + chunk->vertices.push_back(normal.z); chunk->vertices.push_back(bottomRight.x); chunk->vertices.push_back(bottomRight.y); chunk->vertices.push_back(bottomRight.z); + chunk->vertices.push_back(normal.x); + chunk->vertices.push_back(normal.y); + chunk->vertices.push_back(normal.z); chunk->vertices.push_back(topLeft.x); chunk->vertices.push_back(topLeft.y); chunk->vertices.push_back(topLeft.z); + chunk->vertices.push_back(normal.x); + chunk->vertices.push_back(normal.y); + chunk->vertices.push_back(normal.z); chunk->vertices.push_back(topRight.x); chunk->vertices.push_back(topRight.y); chunk->vertices.push_back(topRight.z); + chunk->vertices.push_back(normal.x); + chunk->vertices.push_back(normal.y); + chunk->vertices.push_back(normal.z); + if (backFace) {