remove Mesh constructor 'attrs' argument & format Mesh.hpp

This commit is contained in:
MihailRis 2025-04-27 01:13:44 +03:00
parent 47b7472231
commit e357a4eb9f
13 changed files with 46 additions and 42 deletions

View File

@ -9,7 +9,7 @@
Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){ Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){
buffer = std::make_unique<Batch2DVertex[]>(capacity ); buffer = std::make_unique<Batch2DVertex[]>(capacity );
mesh = std::make_unique<Mesh<Batch2DVertex>>(buffer.get(), 0, Batch2DVertex::ATTRIBUTES); mesh = std::make_unique<Mesh<Batch2DVertex>>(buffer.get(), 0);
index = 0; index = 0;
const ubyte pixels[] = { const ubyte pixels[] = {

View File

@ -11,7 +11,7 @@ Batch3D::Batch3D(size_t capacity)
buffer = std::make_unique<Batch3DVertex[]>(capacity); buffer = std::make_unique<Batch3DVertex[]>(capacity);
mesh = std::make_unique<Mesh<Batch3DVertex>>(buffer.get(), 0, Batch3DVertex::ATTRIBUTES); mesh = std::make_unique<Mesh<Batch3DVertex>>(buffer.get(), 0);
index = 0; index = 0;
const ubyte pixels[] = { const ubyte pixels[] = {

View File

@ -7,7 +7,7 @@
LineBatch::LineBatch(size_t capacity) : capacity(capacity) { LineBatch::LineBatch(size_t capacity) : capacity(capacity) {
buffer = std::make_unique<LineVertex[]>(capacity * 2); buffer = std::make_unique<LineVertex[]>(capacity * 2);
mesh = std::make_unique<Mesh<LineVertex>>(buffer.get(), 0, LineVertex::ATTRIBUTES); mesh = std::make_unique<Mesh<LineVertex>>(buffer.get(), 0);
index = 0; index = 0;
} }

View File

@ -8,7 +8,6 @@ struct MeshStats {
static int drawCalls; static int drawCalls;
}; };
template <typename VertexStructure> template <typename VertexStructure>
class Mesh { class Mesh {
unsigned int vao; unsigned int vao;
@ -16,27 +15,33 @@ class Mesh {
unsigned int ibo; unsigned int ibo;
size_t vertexCount; size_t vertexCount;
size_t indexCount; size_t indexCount;
size_t vertexSize;
public: public:
explicit Mesh(const MeshData<VertexStructure>& data); explicit Mesh(const MeshData<VertexStructure>& data);
Mesh(const VertexStructure *vertexBuffer, size_t vertices, const uint32_t *indexBuffer, size_t indices, Mesh(
const VertexAttribute *attrs); const VertexStructure* vertexBuffer,
size_t vertices,
const uint32_t* indexBuffer,
size_t indices
);
Mesh(const VertexStructure *vertexBuffer, size_t vertices, const VertexAttribute *attrs) : Mesh<VertexStructure>( Mesh(const VertexStructure* vertexBuffer, size_t vertices)
vertexBuffer, vertices, nullptr, 0, attrs) { : Mesh<VertexStructure>(vertexBuffer, vertices, nullptr, 0) {};
};
~Mesh(); ~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 vertexBuffer vertex data buffer
/// @param vertexCount number of vertices in new buffer /// @param vertexCount number of vertices in new buffer
/// @param indexBuffer indices buffer /// @param indexBuffer indices buffer
/// @param indexCount number of values in indices buffer /// @param indexCount number of values in indices buffer
void reload(const VertexStructure *vertexBuffer, size_t vertexCount, const uint32_t *indexBuffer = nullptr, void reload(
size_t indexCount = 0); const VertexStructure* vertexBuffer,
size_t vertexCount,
const uint32_t* indexBuffer = nullptr,
size_t indexCount = 0
);
/// @brief Draw mesh with specified primitives type /// @brief Draw mesh with specified primitives type
/// @param primitive primitives type /// @param primitive primitives type
@ -44,8 +49,6 @@ public:
/// @brief Draw mesh as triangles /// @brief Draw mesh as triangles
void draw() const; void draw() const;
/// @brief Total numbers of alive mesh objects
}; };
#include "graphics/core/Mesh.inl" #include "graphics/core/Mesh.inl"

View File

@ -3,14 +3,21 @@
#include "MeshData.hpp" #include "MeshData.hpp"
#include "gl_util.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 <typename VertexStructure> template <typename VertexStructure>
Mesh<VertexStructure>::Mesh(const MeshData<VertexStructure>& data) Mesh<VertexStructure>::Mesh(const MeshData<VertexStructure>& data)
: Mesh( : Mesh(
data.vertices.data(), data.vertices.data(),
data.vertices.size(), data.vertices.size(),
data.indices.data(), data.indices.data(),
data.indices.size(), data.indices.size()
data.attrs.data()
) { ) {
} }
@ -19,18 +26,15 @@ Mesh<VertexStructure>::Mesh(
const VertexStructure* vertexBuffer, const VertexStructure* vertexBuffer,
size_t vertices, size_t vertices,
const uint32_t* indexBuffer, const uint32_t* indexBuffer,
size_t indices, size_t indices
const VertexAttribute* attrs
) )
: vao(0), vbo(0), ibo(0), vertexCount(0), indexCount(0) { : 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++; 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); glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo); glGenBuffers(1, &vbo);
@ -80,7 +84,7 @@ void Mesh<VertexStructure>::reload(
if (vertexBuffer != nullptr && vertexCount != 0) { if (vertexBuffer != nullptr && vertexCount != 0) {
glBufferData( glBufferData(
GL_ARRAY_BUFFER, GL_ARRAY_BUFFER,
vertexCount * vertexSize, vertexCount * sizeof(VertexStructure),
vertexBuffer, vertexBuffer,
GL_STREAM_DRAW GL_STREAM_DRAW
); );

View File

@ -18,7 +18,7 @@ struct VertexAttribute {
bool normalized = false; bool normalized = false;
ubyte count = 0; ubyte count = 0;
[[nodiscard]] uint32_t size() const { [[nodiscard]] constexpr uint32_t size() const {
switch (type) { switch (type) {
case Type::FLOAT: case Type::FLOAT:
return count * sizeof(float); return count * sizeof(float);
@ -31,9 +31,8 @@ struct VertexAttribute {
case Type::UNSIGNED_BYTE: case Type::UNSIGNED_BYTE:
case Type::BYTE: case Type::BYTE:
return count * sizeof(int8_t); return count * sizeof(int8_t);
default:
throw std::runtime_error("VertexAttribute type is not supported");
} }
return 0;
} }
}; };

View File

@ -21,7 +21,7 @@ PostProcessing::PostProcessing(size_t effectSlotsCount)
{{1.0f, -1.0f}}, {{1.0f, -1.0f}},
}; };
quadMesh = std::make_unique<Mesh<PostProcessingVertex>>(meshData, 6, PostProcessingVertex::ATTRIBUTES); quadMesh = std::make_unique<Mesh<PostProcessingVertex>>(meshData, 6);
} }
PostProcessing::~PostProcessing() = default; PostProcessing::~PostProcessing() = default;

View File

@ -43,5 +43,6 @@ namespace gl {
case Type::BYTE: case Type::BYTE:
return GL_BYTE; return GL_BYTE;
} }
return 0;
} }
} }

View File

@ -662,7 +662,7 @@ ChunkMesh BlocksRenderer::render(const Chunk *chunk, const Chunks *chunks) {
build(chunk, chunks); build(chunk, chunks);
return ChunkMesh{std::make_unique<Mesh<ChunkVertex>>( return ChunkMesh{std::make_unique<Mesh<ChunkVertex>>(
vertexBuffer.get(), vertexCount, indexBuffer.get(), indexCount, ChunkVertex::ATTRIBUTES vertexBuffer.get(), vertexCount, indexBuffer.get(), indexCount
), std::move(sortingMesh)}; ), std::move(sortingMesh)};
} }

View File

@ -289,9 +289,7 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
auto& entry = chunkEntries.at(0); auto& entry = chunkEntries.at(0);
if (found->second.sortedMesh == nullptr) { if (found->second.sortedMesh == nullptr) {
found->second.sortedMesh = std::make_unique<Mesh<ChunkVertex>>( found->second.sortedMesh = std::make_unique<Mesh<ChunkVertex>>(
entry.vertexData.data(), entry.vertexData.data(), entry.vertexData.size()
entry.vertexData.size(),
ChunkVertex::ATTRIBUTES
); );
} }
found->second.sortedMesh->draw(); found->second.sortedMesh->draw();
@ -316,7 +314,7 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
} }
write_sorting_mesh_entries(buffer.data(), chunkEntries); write_sorting_mesh_entries(buffer.data(), chunkEntries);
found->second.sortedMesh = std::make_unique<Mesh<ChunkVertex>>( found->second.sortedMesh = std::make_unique<Mesh<ChunkVertex>>(
buffer.data(), size, ChunkVertex::ATTRIBUTES buffer.data(), size
); );
} }
found->second.sortedMesh->draw(); found->second.sortedMesh->draw();

View File

@ -11,7 +11,7 @@ MainBatch::MainBatch(size_t capacity)
: buffer(std::make_unique<MainBatchVertex[]>(capacity)), : buffer(std::make_unique<MainBatchVertex[]>(capacity)),
capacity(capacity), capacity(capacity),
index(0), index(0),
mesh(std::make_unique<Mesh<MainBatchVertex>>(buffer.get(), 0, MainBatchVertex::ATTRIBUTES)) { mesh(std::make_unique<Mesh<MainBatchVertex>>(buffer.get(), 0)) {
const ubyte pixels[] = { const ubyte pixels[] = {
255, 255, 255, 255, 255, 255, 255, 255,

View File

@ -44,7 +44,7 @@ Skybox::Skybox(uint size, Shader& shader)
{{1.0f, -1.0f}} {{1.0f, -1.0f}}
}; };
mesh = std::make_unique<Mesh<SkyboxVertex>>(vertices, 6, SkyboxVertex::ATTRIBUTES); mesh = std::make_unique<Mesh<SkyboxVertex>>(vertices, 6);
sprites.push_back(skysprite { sprites.push_back(skysprite {
"misc/moon", "misc/moon",

View File

@ -10,7 +10,6 @@
#include "coders/byte_utils.hpp" #include "coders/byte_utils.hpp"
#include "content/Content.hpp" #include "content/Content.hpp"
#include "world/files/WorldFiles.hpp" #include "world/files/WorldFiles.hpp"
#include "graphics/core/Mesh.hpp"
#include "lighting/Lightmap.hpp" #include "lighting/Lightmap.hpp"
#include "maths/aabb.hpp" #include "maths/aabb.hpp"
#include "maths/rays.hpp" #include "maths/rays.hpp"