From 65287b327326709f0d48edbff2b244aefd68ab04 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 17 Nov 2024 00:54:10 +0300 Subject: [PATCH] move chunk vertex attributes info to commons.hpp --- src/graphics/render/BlocksRenderer.cpp | 45 +++++++++++++------------- src/graphics/render/BlocksRenderer.hpp | 1 - src/graphics/render/ChunksRenderer.cpp | 11 +++---- src/graphics/render/commons.hpp | 5 +++ 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/graphics/render/BlocksRenderer.cpp b/src/graphics/render/BlocksRenderer.cpp index 04622e46..8b041b58 100644 --- a/src/graphics/render/BlocksRenderer.cpp +++ b/src/graphics/render/BlocksRenderer.cpp @@ -14,7 +14,6 @@ #include "util/timeutil.hpp" -const uint BlocksRenderer::VERTEX_SIZE = 6; const glm::vec3 BlocksRenderer::SUN_VECTOR (0.411934f, 0.863868f, -0.279161f); BlocksRenderer::BlocksRenderer( @@ -23,7 +22,7 @@ BlocksRenderer::BlocksRenderer( const ContentGfxCache& cache, const EngineSettings& settings ) : content(content), - vertexBuffer(std::make_unique(capacity * VERTEX_SIZE)), + vertexBuffer(std::make_unique(capacity * CHUNK_VERTEX_SIZE)), indexBuffer(std::make_unique(capacity)), vertexOffset(0), indexOffset(0), @@ -87,7 +86,7 @@ void BlocksRenderer::face( const glm::vec4(&lights)[4], const glm::vec4& tint ) { - if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { + if (vertexOffset + CHUNK_VERTEX_SIZE * 4 > capacity) { overflow = true; return; } @@ -127,7 +126,7 @@ void BlocksRenderer::faceAO( const UVRegion& region, bool lights ) { - if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { + if (vertexOffset + CHUNK_VERTEX_SIZE * 4 > capacity) { overflow = true; return; } @@ -165,7 +164,7 @@ void BlocksRenderer::face( glm::vec4 tint, bool lights ) { - if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { + if (vertexOffset + CHUNK_VERTEX_SIZE * 4 > capacity) { overflow = true; return; } @@ -290,7 +289,7 @@ void BlocksRenderer::blockCustomModel( const auto& model = cache.getModel(block->rt.id); for (const auto& mesh : model.meshes) { - if (vertexOffset + BlocksRenderer::VERTEX_SIZE * mesh.vertices.size() > capacity) { + if (vertexOffset + CHUNK_VERTEX_SIZE * mesh.vertices.size() > capacity) { overflow = true; return; } @@ -558,19 +557,19 @@ SortingMeshData BlocksRenderer::renderTranslucent( y + 0.5f, z + chunk->z * CHUNK_D + 0.5f ), - util::Buffer(indexSize * VERTEX_SIZE)}; + util::Buffer(indexSize * CHUNK_VERTEX_SIZE)}; totalSize += entry.vertexData.size(); for (int j = 0; j < indexSize; j++) { std::memcpy( - entry.vertexData.data() + j * VERTEX_SIZE, - vertexBuffer.get() + indexBuffer[j] * VERTEX_SIZE, - sizeof(float) * VERTEX_SIZE + entry.vertexData.data() + j * CHUNK_VERTEX_SIZE, + vertexBuffer.get() + indexBuffer[j] * CHUNK_VERTEX_SIZE, + sizeof(float) * CHUNK_VERTEX_SIZE ); - float& vx = entry.vertexData[j * VERTEX_SIZE + 0]; - float& vy = entry.vertexData[j * VERTEX_SIZE + 1]; - float& vz = entry.vertexData[j * VERTEX_SIZE + 2]; + float& vx = entry.vertexData[j * CHUNK_VERTEX_SIZE + 0]; + float& vy = entry.vertexData[j * CHUNK_VERTEX_SIZE + 1]; + float& vz = entry.vertexData[j * CHUNK_VERTEX_SIZE + 2]; if (!aabbInit) { aabbInit = true; @@ -653,21 +652,23 @@ void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {; } ChunkMeshData BlocksRenderer::createMesh() { - const vattr attrs[]{ {3}, {2}, {1}, {0} }; - return ChunkMeshData{MeshData( - util::Buffer(vertexBuffer.get(), vertexOffset), - util::Buffer(indexBuffer.get(), indexSize), - util::Buffer({{3}, {2}, {1}, {0}}) - ), std::move(sortingMesh)}; + return ChunkMeshData { + MeshData( + util::Buffer(vertexBuffer.get(), vertexOffset), + util::Buffer(indexBuffer.get(), indexSize), + util::Buffer( + CHUNK_VATTRS, sizeof(CHUNK_VATTRS) / sizeof(vattr) + ) + ), + std::move(sortingMesh)}; } ChunkMesh BlocksRenderer::render(const Chunk* chunk, const Chunks* chunks) { build(chunk, chunks); - const vattr attrs[]{ {3}, {2}, {1}, {0} }; - size_t vcount = vertexOffset / BlocksRenderer::VERTEX_SIZE; + size_t vcount = vertexOffset / CHUNK_VERTEX_SIZE; return ChunkMesh{std::make_unique( - vertexBuffer.get(), vcount, indexBuffer.get(), indexSize, attrs + vertexBuffer.get(), vcount, indexBuffer.get(), indexSize, CHUNK_VATTRS ), std::move(sortingMesh)}; } diff --git a/src/graphics/render/BlocksRenderer.hpp b/src/graphics/render/BlocksRenderer.hpp index cdb20749..c0e0086e 100644 --- a/src/graphics/render/BlocksRenderer.hpp +++ b/src/graphics/render/BlocksRenderer.hpp @@ -27,7 +27,6 @@ struct UVRegion; class BlocksRenderer { static const glm::vec3 SUN_VECTOR; - static const uint VERTEX_SIZE; const Content& content; std::unique_ptr vertexBuffer; std::unique_ptr indexBuffer; diff --git a/src/graphics/render/ChunksRenderer.cpp b/src/graphics/render/ChunksRenderer.cpp index e52cd3eb..7176500b 100644 --- a/src/graphics/render/ChunksRenderer.cpp +++ b/src/graphics/render/ChunksRenderer.cpp @@ -49,9 +49,6 @@ public: } }; -const vattr ATTRS[]{ {3}, {2}, {1}, {0} }; -inline constexpr int VERTEX_SIZE = 6; - ChunksRenderer::ChunksRenderer( const Level* level, const Assets& assets, @@ -84,7 +81,7 @@ ChunksRenderer::ChunksRenderer( logger.info() << "created " << threadPool.getWorkersCount() << " workers"; float buf[1]{}; - sortedMesh = std::make_unique(buf, 0, ATTRS); + sortedMesh = std::make_unique(buf, 0, CHUNK_VATTRS); } ChunksRenderer::~ChunksRenderer() { @@ -265,8 +262,8 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) { if (found->second.sortedMesh == nullptr) { found->second.sortedMesh = std::make_unique( entry.vertexData.data(), - entry.vertexData.size() / VERTEX_SIZE, - ATTRS + entry.vertexData.size() / CHUNK_VERTEX_SIZE, + CHUNK_VATTRS ); } found->second.sortedMesh->draw(); @@ -297,7 +294,7 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) { offset += entry.vertexData.size(); } found->second.sortedMesh = std::make_unique( - buffer.data(), size / VERTEX_SIZE, ATTRS + buffer.data(), size / CHUNK_VERTEX_SIZE, CHUNK_VATTRS ); } found->second.sortedMesh->draw(); diff --git a/src/graphics/render/commons.hpp b/src/graphics/render/commons.hpp index 5cd92631..b6d18003 100644 --- a/src/graphics/render/commons.hpp +++ b/src/graphics/render/commons.hpp @@ -7,6 +7,11 @@ #include "graphics/core/MeshData.hpp" #include "util/Buffer.hpp" +/// @brief Chunk mesh vertex attributes +inline const vattr CHUNK_VATTRS[]{ {3}, {2}, {1}, {0} }; +/// @brief Chunk mesh vertex size divided by sizeof(float) +inline constexpr int CHUNK_VERTEX_SIZE = 6; + class Mesh; struct SortingMeshEntry {