From bff9e6ad4f067a950895ae2fbb39837cd79e51c9 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Tue, 18 Jul 2023 21:18:12 +0200 Subject: [PATCH] move normal data to own array --- include/chunk.hpp | 2 +- include/chunkmesher.hpp | 1 + src/chunk.cpp | 2 ++ src/chunkmesher.cpp | 36 +++++++++++++++--------------------- textures/oak_log.png | Bin 0 -> 263 bytes 5 files changed, 19 insertions(+), 22 deletions(-) create mode 100644 textures/oak_log.png diff --git a/include/chunk.hpp b/include/chunk.hpp index e29f226..ae547df 100644 --- a/include/chunk.hpp +++ b/include/chunk.hpp @@ -56,7 +56,7 @@ namespace Chunk std::unique_ptr getBlocksArray(int* len) { return (this->blocks.toArray(len)); } public: - GLuint VAO{0}, VBO{0}, EBO{0}, colorBuffer{0}, numTriangles{0}; + GLuint VAO{0}, VBO{0}, EBO{0}, colorBuffer{0}, normalsBuffer{0}, numTriangles{0}; std::atomic unload_timer{0}; private: diff --git a/include/chunkmesher.hpp b/include/chunkmesher.hpp index ab5ea3e..97a834c 100644 --- a/include/chunkmesher.hpp +++ b/include/chunkmesher.hpp @@ -18,6 +18,7 @@ namespace chunkmesher{ GLuint numVertices{0}; std::vector vertices; + std::vector normals; std::vector colors; std::vector indices; }; diff --git a/src/chunk.cpp b/src/chunk.cpp index 66feb49..fd868ed 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -29,6 +29,7 @@ namespace Chunk void Chunk::createBuffers(){ glGenVertexArrays(1, &(this->VAO)); glGenBuffers(1, &(this->colorBuffer)); + glGenBuffers(1, &(this->normalsBuffer)); glGenBuffers(1, &(this->VBO)); glGenBuffers(1, &(this->EBO)); @@ -36,6 +37,7 @@ namespace Chunk void Chunk::deleteBuffers(){ glDeleteBuffers(1, &(this->colorBuffer)); + glDeleteBuffers(1, &(this->normalsBuffer)); glDeleteBuffers(1, &(this->VBO)); glDeleteBuffers(1, &(this->EBO)); glDeleteVertexArrays(1, &(this->VAO)); diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index 3fcc324..db521e1 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -228,25 +228,24 @@ void sendtogpu(MeshData* mesh_data) // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). glBindVertexArray(mesh_data->chunk->VAO); - glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->VBO); - glBufferData(GL_ARRAY_BUFFER, mesh_data->vertices.size() * sizeof(GLfloat), &(mesh_data->vertices[0]), GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh_data->chunk->EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh_data->indices.size() * sizeof(GLuint), &(mesh_data->indices[0]), GL_STATIC_DRAW); // position attribute - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0); + glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->VBO); + glBufferData(GL_ARRAY_BUFFER, mesh_data->vertices.size() * sizeof(GLfloat), &(mesh_data->vertices[0]), GL_STATIC_DRAW); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); glEnableVertexAttribArray(0); // normal attribute - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3* - sizeof(float))); + glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->normalsBuffer); + glBufferData(GL_ARRAY_BUFFER, mesh_data->normals.size() * sizeof(GLfloat), &(mesh_data->normals[0]), GL_STATIC_DRAW); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)(0)); glEnableVertexAttribArray(1); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh_data->chunk->EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh_data->indices.size() * sizeof(GLuint), &(mesh_data->indices[0]), GL_STATIC_DRAW); - // texcoords attribute glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->colorBuffer); glBufferData(GL_ARRAY_BUFFER, mesh_data->colors.size() * sizeof(GLfloat), &(mesh_data->colors[0]), GL_STATIC_DRAW); - glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); @@ -257,6 +256,7 @@ void sendtogpu(MeshData* mesh_data) // once data has been sent to the GPU, it can be cleared from system RAM mesh_data->vertices.clear(); + mesh_data->normals.clear(); mesh_data->indices.clear(); mesh_data->colors.clear(); } @@ -271,30 +271,24 @@ void quad(MeshData* mesh_data, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec mesh_data->vertices.push_back(bottomLeft.x); mesh_data->vertices.push_back(bottomLeft.y); mesh_data->vertices.push_back(bottomLeft.z); - mesh_data->vertices.push_back(normal.x); - mesh_data->vertices.push_back(normal.y); - mesh_data->vertices.push_back(normal.z); mesh_data->vertices.push_back(bottomRight.x); mesh_data->vertices.push_back(bottomRight.y); mesh_data->vertices.push_back(bottomRight.z); - mesh_data->vertices.push_back(normal.x); - mesh_data->vertices.push_back(normal.y); - mesh_data->vertices.push_back(normal.z); mesh_data->vertices.push_back(topLeft.x); mesh_data->vertices.push_back(topLeft.y); mesh_data->vertices.push_back(topLeft.z); - mesh_data->vertices.push_back(normal.x); - mesh_data->vertices.push_back(normal.y); - mesh_data->vertices.push_back(normal.z); mesh_data->vertices.push_back(topRight.x); mesh_data->vertices.push_back(topRight.y); mesh_data->vertices.push_back(topRight.z); - mesh_data->vertices.push_back(normal.x); - mesh_data->vertices.push_back(normal.y); - mesh_data->vertices.push_back(normal.z); + + for(int i = 0; i < 4; i++){ + mesh_data->normals.push_back(normal.x); + mesh_data->normals.push_back(normal.y); + mesh_data->normals.push_back(normal.z); + } // texcoords if(dim == 0){ diff --git a/textures/oak_log.png b/textures/oak_log.png new file mode 100644 index 0000000000000000000000000000000000000000..842ffab5eba99da085658a83875185615d7ba6e3 GIT binary patch literal 263 zcmV+i0r>ujP)Kn4&WeQ-WEtf$C=UKJUm1l($mU{vCUIG$E=~i5Tl%L(mx32&! zU;?O$wqu$coG!j`YowW^C2`iSY&F+vkg{#gnEwLr4S=nQoDGqb{Qx%j^Z0&MCCLB) N002ovPDHLkV1ivOZl?eM literal 0 HcmV?d00001