diff --git a/src/graphics/commons/Model.cpp b/src/graphics/commons/Model.cpp index 8889400d4..0615ce2d8 100644 --- a/src/graphics/commons/Model.cpp +++ b/src/graphics/commons/Model.cpp @@ -156,6 +156,12 @@ void Mesh::scale(const glm::vec3& size) { } } +void Mesh::transform(const glm::mat4& matrix) { + for (auto& vertex : vertices) { + vertex.coord = matrix * glm::vec4(vertex.coord, 1.0f); + } +} + void Model::clean() { meshes.erase( std::remove_if(meshes.begin(), meshes.end(), @@ -165,3 +171,20 @@ void Model::clean() { meshes.end() ); } + +void Model::transform(const glm::mat4& matrix) { + for (auto& mesh : meshes) { + mesh.transform(matrix); + } +} + +void Model::merge(const Model& source) { + for (const auto& srcMesh : source.meshes) { + auto& dstMesh = addMesh(srcMesh.texture, srcMesh.shading); + dstMesh.vertices.insert( + dstMesh.vertices.end(), + srcMesh.vertices.begin(), + srcMesh.vertices.end() + ); + } +} diff --git a/src/graphics/commons/Model.hpp b/src/graphics/commons/Model.hpp index ac330c378..36962be88 100644 --- a/src/graphics/commons/Model.hpp +++ b/src/graphics/commons/Model.hpp @@ -72,6 +72,9 @@ namespace model { const glm::mat4& transform ); void scale(const glm::vec3& size); + + /// @brief Apply transformation matrix to vertices coordinates + void transform(const glm::mat4& matrix); }; struct Model { @@ -91,5 +94,12 @@ namespace model { } /// @brief Remove all empty meshes void clean(); + + /// @brief Apply transformation matrix to vertices coordinates + void transform(const glm::mat4& matrix); + + /// @brief Copy or merge meshes from source model to this. + /// Does not remove duplicates + void merge(const Model& source); }; }