From e357a4eb9f33ee83138e37c6e5981d730decf90a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 27 Apr 2025 01:13:44 +0300 Subject: [PATCH] remove Mesh constructor 'attrs' argument & format Mesh.hpp --- src/graphics/core/Batch2D.cpp | 2 +- src/graphics/core/Batch3D.cpp | 2 +- src/graphics/core/LineBatch.cpp | 2 +- src/graphics/core/Mesh.hpp | 33 ++++++++++++++------------ src/graphics/core/Mesh.inl | 28 ++++++++++++---------- src/graphics/core/MeshData.hpp | 5 ++-- src/graphics/core/PostProcessing.cpp | 2 +- src/graphics/core/gl_util.hpp | 1 + src/graphics/render/BlocksRenderer.cpp | 2 +- src/graphics/render/ChunksRenderer.cpp | 6 ++--- src/graphics/render/MainBatch.cpp | 2 +- src/graphics/render/Skybox.cpp | 2 +- src/voxels/Chunks.cpp | 1 - 13 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/graphics/core/Batch2D.cpp b/src/graphics/core/Batch2D.cpp index 0da82ccd..38224a3f 100644 --- a/src/graphics/core/Batch2D.cpp +++ b/src/graphics/core/Batch2D.cpp @@ -9,7 +9,7 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){ buffer = std::make_unique(capacity ); - mesh = std::make_unique>(buffer.get(), 0, Batch2DVertex::ATTRIBUTES); + mesh = std::make_unique>(buffer.get(), 0); index = 0; const ubyte pixels[] = { diff --git a/src/graphics/core/Batch3D.cpp b/src/graphics/core/Batch3D.cpp index bd79b127..43d161f6 100644 --- a/src/graphics/core/Batch3D.cpp +++ b/src/graphics/core/Batch3D.cpp @@ -11,7 +11,7 @@ Batch3D::Batch3D(size_t capacity) buffer = std::make_unique(capacity); - mesh = std::make_unique>(buffer.get(), 0, Batch3DVertex::ATTRIBUTES); + mesh = std::make_unique>(buffer.get(), 0); index = 0; const ubyte pixels[] = { diff --git a/src/graphics/core/LineBatch.cpp b/src/graphics/core/LineBatch.cpp index a1448768..9e33398a 100644 --- a/src/graphics/core/LineBatch.cpp +++ b/src/graphics/core/LineBatch.cpp @@ -7,7 +7,7 @@ LineBatch::LineBatch(size_t capacity) : capacity(capacity) { buffer = std::make_unique(capacity * 2); - mesh = std::make_unique>(buffer.get(), 0, LineVertex::ATTRIBUTES); + mesh = std::make_unique>(buffer.get(), 0); index = 0; } diff --git a/src/graphics/core/Mesh.hpp b/src/graphics/core/Mesh.hpp index 559c5cc9..3d428322 100644 --- a/src/graphics/core/Mesh.hpp +++ b/src/graphics/core/Mesh.hpp @@ -8,35 +8,40 @@ struct MeshStats { static int drawCalls; }; - -template +template class Mesh { unsigned int vao; unsigned int vbo; unsigned int ibo; size_t vertexCount; size_t indexCount; - size_t vertexSize; - public: - explicit Mesh(const MeshData &data); + explicit Mesh(const MeshData& data); - Mesh(const VertexStructure *vertexBuffer, size_t vertices, const uint32_t *indexBuffer, size_t indices, - const VertexAttribute *attrs); + Mesh( + const VertexStructure* vertexBuffer, + size_t vertices, + const uint32_t* indexBuffer, + size_t indices + ); - Mesh(const VertexStructure *vertexBuffer, size_t vertices, const VertexAttribute *attrs) : Mesh( - vertexBuffer, vertices, nullptr, 0, attrs) { - }; + Mesh(const VertexStructure* vertexBuffer, size_t vertices) + : Mesh(vertexBuffer, vertices, nullptr, 0) {}; ~Mesh(); - /// @brief Update GL vertex and index buffers data without changing VAO attributes + /// @brief Update GL vertex and index buffers data without changing VAO + /// attributes /// @param vertexBuffer vertex data buffer /// @param vertexCount number of vertices in new buffer /// @param indexBuffer indices buffer /// @param indexCount number of values in indices buffer - void reload(const VertexStructure *vertexBuffer, size_t vertexCount, const uint32_t *indexBuffer = nullptr, - size_t indexCount = 0); + void reload( + const VertexStructure* vertexBuffer, + size_t vertexCount, + const uint32_t* indexBuffer = nullptr, + size_t indexCount = 0 + ); /// @brief Draw mesh with specified primitives type /// @param primitive primitives type @@ -44,8 +49,6 @@ public: /// @brief Draw mesh as triangles void draw() const; - - /// @brief Total numbers of alive mesh objects }; #include "graphics/core/Mesh.inl" diff --git a/src/graphics/core/Mesh.inl b/src/graphics/core/Mesh.inl index 33812958..390c2f9a 100644 --- a/src/graphics/core/Mesh.inl +++ b/src/graphics/core/Mesh.inl @@ -3,14 +3,21 @@ #include "MeshData.hpp" #include "gl_util.hpp" +inline constexpr size_t calc_size(const VertexAttribute attrs[]) { + size_t vertexSize = 0; + for (int i = 0; attrs[i].count; i++) { + vertexSize += attrs[i].size(); + } + return vertexSize; +} + template Mesh::Mesh(const MeshData& data) : Mesh( data.vertices.data(), data.vertices.size(), data.indices.data(), - data.indices.size(), - data.attrs.data() + data.indices.size() ) { } @@ -19,18 +26,15 @@ Mesh::Mesh( const VertexStructure* vertexBuffer, size_t vertices, const uint32_t* indexBuffer, - size_t indices, - const VertexAttribute* attrs + size_t indices ) : vao(0), vbo(0), ibo(0), vertexCount(0), indexCount(0) { + static_assert( + calc_size(VertexStructure::ATTRIBUTES) == sizeof(VertexStructure) + ); + + const auto& attrs = VertexStructure::ATTRIBUTES; MeshStats::meshesCount++; - vertexSize = 0; - for (int i = 0; attrs[i].count; i++) { - vertexSize += attrs[i].size(); - } - if (vertexSize != sizeof(VertexStructure)) { - throw std::runtime_error("Vertex size mismatch!"); - } glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); @@ -80,7 +84,7 @@ void Mesh::reload( if (vertexBuffer != nullptr && vertexCount != 0) { glBufferData( GL_ARRAY_BUFFER, - vertexCount * vertexSize, + vertexCount * sizeof(VertexStructure), vertexBuffer, GL_STREAM_DRAW ); diff --git a/src/graphics/core/MeshData.hpp b/src/graphics/core/MeshData.hpp index 80f66366..82954b23 100644 --- a/src/graphics/core/MeshData.hpp +++ b/src/graphics/core/MeshData.hpp @@ -18,7 +18,7 @@ struct VertexAttribute { bool normalized = false; ubyte count = 0; - [[nodiscard]] uint32_t size() const { + [[nodiscard]] constexpr uint32_t size() const { switch (type) { case Type::FLOAT: return count * sizeof(float); @@ -31,9 +31,8 @@ struct VertexAttribute { case Type::UNSIGNED_BYTE: case Type::BYTE: return count * sizeof(int8_t); - default: - throw std::runtime_error("VertexAttribute type is not supported"); } + return 0; } }; diff --git a/src/graphics/core/PostProcessing.cpp b/src/graphics/core/PostProcessing.cpp index 93d85830..2968cb4d 100644 --- a/src/graphics/core/PostProcessing.cpp +++ b/src/graphics/core/PostProcessing.cpp @@ -21,7 +21,7 @@ PostProcessing::PostProcessing(size_t effectSlotsCount) {{1.0f, -1.0f}}, }; - quadMesh = std::make_unique>(meshData, 6, PostProcessingVertex::ATTRIBUTES); + quadMesh = std::make_unique>(meshData, 6); } PostProcessing::~PostProcessing() = default; diff --git a/src/graphics/core/gl_util.hpp b/src/graphics/core/gl_util.hpp index f3446fea..da2abfb5 100644 --- a/src/graphics/core/gl_util.hpp +++ b/src/graphics/core/gl_util.hpp @@ -43,5 +43,6 @@ namespace gl { case Type::BYTE: return GL_BYTE; } + return 0; } } diff --git a/src/graphics/render/BlocksRenderer.cpp b/src/graphics/render/BlocksRenderer.cpp index 983a4168..79cca542 100644 --- a/src/graphics/render/BlocksRenderer.cpp +++ b/src/graphics/render/BlocksRenderer.cpp @@ -662,7 +662,7 @@ ChunkMesh BlocksRenderer::render(const Chunk *chunk, const Chunks *chunks) { build(chunk, chunks); return ChunkMesh{std::make_unique>( - vertexBuffer.get(), vertexCount, indexBuffer.get(), indexCount, ChunkVertex::ATTRIBUTES + vertexBuffer.get(), vertexCount, indexBuffer.get(), indexCount ), std::move(sortingMesh)}; } diff --git a/src/graphics/render/ChunksRenderer.cpp b/src/graphics/render/ChunksRenderer.cpp index 1c5ca1a4..199d818b 100644 --- a/src/graphics/render/ChunksRenderer.cpp +++ b/src/graphics/render/ChunksRenderer.cpp @@ -289,9 +289,7 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) { auto& entry = chunkEntries.at(0); if (found->second.sortedMesh == nullptr) { found->second.sortedMesh = std::make_unique>( - entry.vertexData.data(), - entry.vertexData.size(), - ChunkVertex::ATTRIBUTES + entry.vertexData.data(), entry.vertexData.size() ); } found->second.sortedMesh->draw(); @@ -316,7 +314,7 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) { } write_sorting_mesh_entries(buffer.data(), chunkEntries); found->second.sortedMesh = std::make_unique>( - buffer.data(), size, ChunkVertex::ATTRIBUTES + buffer.data(), size ); } found->second.sortedMesh->draw(); diff --git a/src/graphics/render/MainBatch.cpp b/src/graphics/render/MainBatch.cpp index bd8d04b9..34976819 100644 --- a/src/graphics/render/MainBatch.cpp +++ b/src/graphics/render/MainBatch.cpp @@ -11,7 +11,7 @@ MainBatch::MainBatch(size_t capacity) : buffer(std::make_unique(capacity)), capacity(capacity), index(0), - mesh(std::make_unique>(buffer.get(), 0, MainBatchVertex::ATTRIBUTES)) { + mesh(std::make_unique>(buffer.get(), 0)) { const ubyte pixels[] = { 255, 255, 255, 255, diff --git a/src/graphics/render/Skybox.cpp b/src/graphics/render/Skybox.cpp index b724de32..d78b3917 100644 --- a/src/graphics/render/Skybox.cpp +++ b/src/graphics/render/Skybox.cpp @@ -44,7 +44,7 @@ Skybox::Skybox(uint size, Shader& shader) {{1.0f, -1.0f}} }; - mesh = std::make_unique>(vertices, 6, SkyboxVertex::ATTRIBUTES); + mesh = std::make_unique>(vertices, 6); sprites.push_back(skysprite { "misc/moon", diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index ce533889..60422c60 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -10,7 +10,6 @@ #include "coders/byte_utils.hpp" #include "content/Content.hpp" #include "world/files/WorldFiles.hpp" -#include "graphics/core/Mesh.hpp" #include "lighting/Lightmap.hpp" #include "maths/aabb.hpp" #include "maths/rays.hpp"