From 1ccbf8b1574cfc89f4c95709cd66641bdf2f3d12 Mon Sep 17 00:00:00 2001 From: emamaker Date: Tue, 22 Nov 2022 22:49:25 +0100 Subject: [PATCH] rudimentary frustum culling TODO: maybe manually extract planes and check against those --- include/chunkmesh.hpp | 2 +- src/chunkmanager.cpp | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/include/chunkmesh.hpp b/include/chunkmesh.hpp index a7a1662..4d1fb13 100644 --- a/include/chunkmesh.hpp +++ b/include/chunkmesh.hpp @@ -21,6 +21,7 @@ public: Chunk::Chunk *chunk{nullptr}; // static Shader theShader("shaders/shader.vs", "shaders/shader.fs"); + glm::mat4 model = glm::mat4(1.0f); private: void quad(glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, glm::vec3 bottomRight, Block block, bool backFace); @@ -29,7 +30,6 @@ private: std::vector colors; std::vector indices; - glm::mat4 model = glm::mat4(1.0f); GLuint VAO, VBO, EBO, colorBuffer, vIndex{0}; Shader *theShader{nullptr}; diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index 7f58234..8480814 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -20,6 +20,7 @@ namespace chunkmanager { } + int total{0}, toGpu{0}; void update(float deltaTime) { // Iterate over all chunks, in concentric spheres starting fron the player and going outwards @@ -79,6 +80,9 @@ namespace chunkmanager } } } + std::cout << "Total chunks to draw: " << total << ". Sent to GPU: " << toGpu << "\n"; + total = 0; + toGpu = 0; } void updateChunk(uint32_t index, uint16_t i, uint16_t j, uint16_t k) @@ -92,11 +96,30 @@ namespace chunkmanager // std::cout << "Creating new chunk" << i << ", " << j << ", " << k <draw(); + { + glm::vec3 chunk = chunks.at(index)->chunk->getPosition() /*+ glm::vec3(static_cast(CHUNK_SIZE))*/; + + total++; + + int a{0}; + for (int i = 0; i < 8; i++) + { + glm::vec4 vertex = glm::vec4(chunk.x + (float)(i & 1), chunk.y + (float)((i & 2) >> 1), chunk.z + (float)((i & 4) >> 2), 500.0f) * (theCamera.getProjection() * theCamera.getView() * chunks.at(index)->model); + vertex = glm::normalize(vertex); + + a += (-vertex.w <= vertex.x && vertex.x <= vertex.w && -vertex.w <= vertex.y && vertex.y <= vertex.w /*&& -vertex.w < vertex.z && vertex.z < vertex.w*/); + } + if (a) + { + toGpu++; + chunks.at(index)->draw(); + } + } } void destroy() { - for (auto &n : chunks) delete n.second; + for (auto &n : chunks) + delete n.second; } -}; +}; \ No newline at end of file