From d78200b71b259b87f401b24906385af4e2553fbe Mon Sep 17 00:00:00 2001 From: ladevieq Date: Mon, 8 Aug 2022 23:38:19 +0200 Subject: Refactored gltf imported and mesh management --- src/mesh.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/mesh.cpp (limited to 'src/mesh.cpp') diff --git a/src/mesh.cpp b/src/mesh.cpp new file mode 100644 index 0000000..c9b5358 --- /dev/null +++ b/src/mesh.cpp @@ -0,0 +1,45 @@ +#include "mesh.hpp" + +static std::vector meshes {}; + +void Mesh::add_submesh(const submesh& submesh) { + auto position_components = attribute_components(ATTRIBUTE_TYPE::POSITION); + submeshes.emplace_back(submesh_info { + (uint32_t)attributes[ATTRIBUTE_TYPE::POSITION].size() / position_components, + (uint32_t)indices.size(), + submesh.index_count, + 0 + }); + + { + auto indices_off = indices.size(); + indices.resize(indices_off + submesh.index_count); + std::memcpy(&indices[indices_off], submesh.indices.data, submesh.indices.length); + } + + for (uint32_t attribute_index { ATTRIBUTE_TYPE::POSITION }; attribute_index < ATTRIBUTE_TYPE::MAX_ATTRIBUTE; attribute_index++) { + const auto& submesh_attribute = submesh.attributes[attribute_index]; + if (submesh_attribute.data == nullptr) continue; + + auto& mesh_attribute = attributes[attribute_index]; + auto attribute_off = mesh_attribute.size(); + auto element_components = attribute_components(static_cast(attribute_index)); + auto elements_count = submesh.vertex_count * element_components; + + mesh_attribute.resize(attribute_off + elements_count); + std::memcpy(&mesh_attribute[attribute_off], submesh_attribute.data, submesh_attribute.length); + } + + vertex_count += submesh.vertex_count; + index_count += submesh.index_count; +} + +[[nodiscard]] Mesh::mesh_instance Mesh::instantiate () const { + return { + *this + }; +} + +void Mesh::register_mesh(const Mesh& mesh) { + meshes.emplace_back(mesh); +} -- cgit v1.3