rudimentary frustum culling

TODO: maybe manually extract planes and check against those
intervalmaps-array-y
emamaker 2022-11-22 22:49:25 +01:00
parent 8116791dcf
commit 1ccbf8b157
2 changed files with 27 additions and 4 deletions

View File

@ -21,6 +21,7 @@ public:
Chunk::Chunk *chunk{nullptr}; Chunk::Chunk *chunk{nullptr};
// static Shader theShader("shaders/shader.vs", "shaders/shader.fs"); // static Shader theShader("shaders/shader.vs", "shaders/shader.fs");
glm::mat4 model = glm::mat4(1.0f);
private: private:
void quad(glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, glm::vec3 bottomRight, Block block, bool backFace); 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<GLfloat> colors; std::vector<GLfloat> colors;
std::vector<GLuint> indices; std::vector<GLuint> indices;
glm::mat4 model = glm::mat4(1.0f);
GLuint VAO, VBO, EBO, colorBuffer, vIndex{0}; GLuint VAO, VBO, EBO, colorBuffer, vIndex{0};
Shader *theShader{nullptr}; Shader *theShader{nullptr};

View File

@ -20,6 +20,7 @@ namespace chunkmanager
{ {
} }
int total{0}, toGpu{0};
void update(float deltaTime) void update(float deltaTime)
{ {
// Iterate over all chunks, in concentric spheres starting fron the player and going outwards // 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) 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 <<std::endl; // std::cout << "Creating new chunk" << i << ", " << j << ", " << k <<std::endl;
} }
else else
chunks.at(index)->draw(); {
glm::vec3 chunk = chunks.at(index)->chunk->getPosition() /*+ glm::vec3(static_cast<float>(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() void destroy()
{ {
for (auto &n : chunks) delete n.second; for (auto &n : chunks)
delete n.second;
} }
}; };