From 2e0ed7b057a72895dfd7a59ba749ffe9c9800873 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Sat, 11 Feb 2023 22:25:40 +0100 Subject: [PATCH] clear mesh data after sending to gpu --- include/chunkmesh.hpp | 2 +- src/chunkmesh.cpp | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/chunkmesh.hpp b/include/chunkmesh.hpp index 4d1fb13..a4e1cc2 100644 --- a/include/chunkmesh.hpp +++ b/include/chunkmesh.hpp @@ -31,7 +31,7 @@ private: std::vector indices; - GLuint VAO, VBO, EBO, colorBuffer, vIndex{0}; + GLuint VAO{0}, VBO{0}, EBO{0}, colorBuffer{0}, vIndex{0}; Shader *theShader{nullptr}; }; diff --git a/src/chunkmesh.cpp b/src/chunkmesh.cpp index b3d737e..9b43e94 100755 --- a/src/chunkmesh.cpp +++ b/src/chunkmesh.cpp @@ -10,6 +10,11 @@ ChunkMesh::~ChunkMesh() { + if(this->VAO) glDeleteVertexArrays(1, &(this->VAO)); + if(this->EBO) glDeleteBuffers(1, &(this->EBO)); + if(this->VBO) glDeleteBuffers(1, &(this->VBO)); + if(this->colorBuffer) glDeleteBuffers(1, &(this->colorBuffer)); + delete this->chunk; delete (this->theShader); } @@ -202,7 +207,7 @@ void ChunkMesh::mesh() if (blocks) delete blocks; - if (vertices.size() > 0) + if (vIndex > 0) { // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). @@ -223,15 +228,24 @@ void ChunkMesh::mesh() glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); + // glDisableVertexAttribArray(0); + // glDisableVertexAttribArray(1); glBindVertexArray(0); + + vIndex = (GLuint)(indices.size()); + + vertices.clear(); + indices.clear(); + colors.clear(); } + } void ChunkMesh::draw() { // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // wireframe mode - if (vertices.size() > 0) + if (vIndex > 0) { theShader->use(); theShader->setMat4("model", this->model); @@ -239,7 +253,7 @@ void ChunkMesh::draw() theShader->setMat4("projection", theCamera.getProjection()); glBindVertexArray(VAO); - glDrawElements(GL_TRIANGLES, static_cast(indices.size()), GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, vIndex , GL_UNSIGNED_INT, 0); glBindVertexArray(0); } }