From 5c6b73ee2b203137d178b49c7a0b2736d4f2c45c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 24 Oct 2024 10:11:57 +0300 Subject: [PATCH] add MeshData --- src/graphics/core/Mesh.cpp | 13 +++++++++++++ src/graphics/core/Mesh.hpp | 7 +++---- src/graphics/core/MeshData.hpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/graphics/core/MeshData.hpp diff --git a/src/graphics/core/Mesh.cpp b/src/graphics/core/Mesh.cpp index ac4e2b14..4e4d7621 100644 --- a/src/graphics/core/Mesh.cpp +++ b/src/graphics/core/Mesh.cpp @@ -4,6 +4,19 @@ int Mesh::meshesCount = 0; int Mesh::drawCalls = 0; +inline size_t calc_vertex_size(const vattr* attrs) { + size_t vertexSize = 0; + for (int i = 0; attrs[i].size; i++) { + vertexSize += attrs[i].size; + } + return vertexSize; +} + +Mesh::Mesh(const MeshData& data) + : Mesh(data.vertices.data(), + data.vertices.size() / calc_vertex_size(data.attrs.data()), + data.attrs.data()) {} + Mesh::Mesh(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices, const vattr* attrs) : ibo(0), vertices(vertices), diff --git a/src/graphics/core/Mesh.hpp b/src/graphics/core/Mesh.hpp index c94154a6..87c8e69b 100644 --- a/src/graphics/core/Mesh.hpp +++ b/src/graphics/core/Mesh.hpp @@ -1,11 +1,9 @@ #pragma once #include -#include "typedefs.hpp" -struct vattr { - ubyte size; -}; +#include "typedefs.hpp" +#include "MeshData.hpp" class Mesh { unsigned int vao; @@ -15,6 +13,7 @@ class Mesh { size_t indices; size_t vertexSize; public: + Mesh(const MeshData& data); Mesh(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices, const vattr* attrs); Mesh(const float* vertexBuffer, size_t vertices, const vattr* attrs) : Mesh(vertexBuffer, vertices, nullptr, 0, attrs) {}; diff --git a/src/graphics/core/MeshData.hpp b/src/graphics/core/MeshData.hpp new file mode 100644 index 00000000..a5b0140c --- /dev/null +++ b/src/graphics/core/MeshData.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include "typedefs.hpp" +#include "util/Buffer.hpp" + +/// @brief Vertex attribute info +struct vattr { + ubyte size; +}; + +/// @brief Raw mesh data structure +struct MeshData { + util::Buffer vertices; + util::Buffer indices; + util::Buffer attrs; + + /// @param vertices vertex data buffer + /// @param indices nullable indices buffer + /// @param attrs vertex attribute sizes (must be null-terminated) + MeshData( + util::Buffer vertices, + util::Buffer indices, + util::Buffer attrs + ) : vertices(std::move(vertices)), + indices(std::move(indices)), + attrs(std::move(attrs)) {} +};