From 44003667197fba6a858c16a35ea8c71b9377781f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 16 Nov 2024 07:41:36 +0300 Subject: [PATCH] feat: actually working slow prototype --- src/graphics/render/BlocksRenderer.cpp | 26 ++++++----- src/graphics/render/ChunksRenderer.cpp | 60 ++++++++++++++++++++++++++ src/graphics/render/ChunksRenderer.hpp | 2 + src/graphics/render/commons.hpp | 5 +++ 4 files changed, 83 insertions(+), 10 deletions(-) diff --git a/src/graphics/render/BlocksRenderer.cpp b/src/graphics/render/BlocksRenderer.cpp index 25e8997d..fcba92c3 100644 --- a/src/graphics/render/BlocksRenderer.cpp +++ b/src/graphics/render/BlocksRenderer.cpp @@ -452,9 +452,9 @@ void BlocksRenderer::render( if (id == 0 || def.drawGroup != drawGroup || state.segment) { continue; } - //if (def.translucent) { - // continue; - //} + if (def.translucent) { + continue; + } const UVRegion texfaces[6] { cache.getRegion(id, 0), cache.getRegion(id, 1), cache.getRegion(id, 2), cache.getRegion(id, 3), @@ -496,7 +496,6 @@ void BlocksRenderer::render( SortingMeshData BlocksRenderer::renderTranslucent( const voxel* voxels, int beginEnds[256][2] ) { - timeutil::ScopeLogTimer log(555); SortingMeshData sortingMesh {{}}; for (const auto drawGroup : *content.drawGroups) { @@ -550,16 +549,23 @@ SortingMeshData BlocksRenderer::renderTranslucent( if (vertexOffset == 0) { continue; } - SortingMeshEntry entry {glm::vec3( - x + chunk->x * CHUNK_W, y, z + chunk->z * CHUNK_D - ), util::Buffer(indexSize * VERTEX_SIZE)}; - + SortingMeshEntry entry { + glm::vec3( + x + chunk->x * CHUNK_W + 0.5f, + y + 0.5f, + z + chunk->z * CHUNK_D + 0.5f + ), + util::Buffer(indexSize * VERTEX_SIZE)}; + for (int j = 0; j < indexSize; j++) { std::memcpy( - entry.vertexData.data(), + entry.vertexData.data() + j * VERTEX_SIZE, vertexBuffer.get() + indexBuffer[j] * VERTEX_SIZE, sizeof(float) * VERTEX_SIZE ); + entry.vertexData[j * VERTEX_SIZE + 0] += chunk->x * CHUNK_W + 0.5f; + entry.vertexData[j * VERTEX_SIZE + 1] += 0.5f; + entry.vertexData[j * VERTEX_SIZE + 2] += chunk->z * CHUNK_D + 0.5f; } sortingMesh.entries.push_back(std::move(entry)); vertexOffset = 0; @@ -569,7 +575,7 @@ SortingMeshData BlocksRenderer::renderTranslucent( return sortingMesh; } -void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) { +void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {; this->chunk = chunk; voxelsBuffer->setPosition( chunk->x * CHUNK_W - voxelBufferPadding, 0, diff --git a/src/graphics/render/ChunksRenderer.cpp b/src/graphics/render/ChunksRenderer.cpp index 847163fc..ed313e08 100644 --- a/src/graphics/render/ChunksRenderer.cpp +++ b/src/graphics/render/ChunksRenderer.cpp @@ -18,6 +18,8 @@ #include #include +#include "util/timeutil.hpp" + static debug::Logger logger("chunks-render"); size_t ChunksRenderer::visibleChunks = 0; @@ -204,4 +206,62 @@ void ChunksRenderer::drawChunks( visibleChunks += drawChunk(indices[i].index, camera, shader, culling); } //} + drawSortedMeshes(camera, shader); +} + +void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) { + const vattr attrs[]{ {3}, {2}, {1}, {0} }; + + std::vector entries; + + auto pposition = camera.position; + + size_t size = 0; + bool culling = settings.graphics.frustumCulling.get(); + for (size_t i = 0; i < indices.size(); i++) { + auto chunk = level.chunks->getChunks()[indices[i].index]; + if (chunk == nullptr || !chunk->flags.lighted) { + continue; + } + const auto& found = meshes.find(glm::ivec2(chunk->x, chunk->z)); + if (found == meshes.end()) { + continue; + } + + glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D); + glm::vec3 max( + chunk->x * CHUNK_W + CHUNK_W, + chunk->top, + chunk->z * CHUNK_D + CHUNK_D + ); + + if (!frustum.isBoxVisible(min, max)) continue; + + auto& chunkEntries = found->second.sortingMesh.entries; + for (auto& entry : chunkEntries) { + entry.distance = glm::distance2(entry.position, pposition); + } + for (const auto& entry : chunkEntries) { + size += entry.vertexData.size(); + entries.push_back(&entry); + } + } + std::sort(entries.begin(), entries.end(), [=](const auto& a, const auto& b) { + return *a < *b; + }); + + util::Buffer buffer(size); + size_t offset = 0; + for (const auto& entry : entries) { + std::memcpy( + (buffer.data() + offset), + entry->vertexData.data(), + entry->vertexData.size() * sizeof(float) + ); + offset += entry->vertexData.size(); + } + Mesh mesh(buffer.data(), size / 6, attrs); + + shader.uniformMatrix("u_model", glm::mat4(1.0f)); + mesh.draw(); } diff --git a/src/graphics/render/ChunksRenderer.hpp b/src/graphics/render/ChunksRenderer.hpp index e44940ea..cd1df2fb 100644 --- a/src/graphics/render/ChunksRenderer.hpp +++ b/src/graphics/render/ChunksRenderer.hpp @@ -75,6 +75,8 @@ public: ); void drawChunks(const Camera& camera, Shader& shader); + void drawSortedMeshes(const Camera& camera, Shader& shader); + void update(); static size_t visibleChunks; diff --git a/src/graphics/render/commons.hpp b/src/graphics/render/commons.hpp index 4101f8a8..3ef8fa26 100644 --- a/src/graphics/render/commons.hpp +++ b/src/graphics/render/commons.hpp @@ -12,6 +12,11 @@ class Mesh; struct SortingMeshEntry { glm::vec3 position; util::Buffer vertexData; + float distance; + + inline bool operator<(const SortingMeshEntry& o) const noexcept { + return distance > o.distance; + } }; struct SortingMeshData {