From a93906d0f1bb76713b0aed591e784a441588fa2f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 17 May 2024 21:30:01 +0300 Subject: [PATCH] small optimization --- src/graphics/render/WorldRenderer.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index f6dda983..15372abe 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -115,22 +115,23 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) { renderer->update(); // [warning] this whole method is not thread-safe for chunks + std::vector indices; for (size_t i = 0; i < chunks->volume; i++){ if (chunks->chunks[i] == nullptr) continue; - indices.push_back(i); + indices.emplace_back(i); } float px = camera->position.x / (float)CHUNK_W - 0.5f; float pz = camera->position.z / (float)CHUNK_D - 0.5f; std::sort(indices.begin(), indices.end(), [chunks, px, pz](auto i, auto j) { const auto& a = chunks->chunks[i]; const auto& b = chunks->chunks[j]; - return ((a->x - px)*(a->x - px) + - (a->z - pz)*(a->z - pz) - > - (b->x - px)*(b->x - px) + - (b->z - pz)*(b->z - pz)); + auto adx = (a->x - px); + auto adz = (a->z - pz); + auto bdx = (b->x - px); + auto bdz = (b->z - pz); + return (adx*adx + adz*adz > bdx*bdx + bdz*bdz); }); bool culling = engine->getSettings().graphics.frustumCulling.get(); if (culling) {