summaryrefslogtreecommitdiff
path: root/assetFactory/factoryMesh.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'assetFactory/factoryMesh.cpp')
-rw-r--r--assetFactory/factoryMesh.cpp40
1 files changed, 27 insertions, 13 deletions
diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp
index 1665b90..8869efd 100644
--- a/assetFactory/factoryMesh.cpp
+++ b/assetFactory/factoryMesh.cpp
@@ -2,34 +2,48 @@
#include "collections.hpp"
#include "gfx/models/vertex.hpp"
#include "modelFactoryMesh.h"
-#include <glm/ext/matrix_transform.hpp>
Mesh::Ptr
FactoryMesh::createMesh() const
{
- constexpr glm::vec2 NullUV {};
-
ModelFactoryMesh mesh;
for (const auto & use : uses) {
use->createMesh(mesh, 1);
}
- mesh.garbage_collection();
- mesh.triangulate();
mesh.update_face_normals();
mesh.update_vertex_normals();
std::vector<Vertex> vertices;
+ std::vector<unsigned int> indices;
for (const auto & face : mesh.faces()) {
- const auto smooth = mesh.property(mesh.smoothFaceProperty, face);
- const auto colour = mesh.color(face);
- for (const auto & vertex : mesh.fv_range(face)) {
- vertices.emplace_back(mesh.point(vertex), NullUV,
- smooth ? mesh.property(mesh.vertex_normals_pph(), vertex)
- : mesh.property(mesh.face_normals_pph(), face),
- colour);
+ const auto & smooth = mesh.property(mesh.smoothFaceProperty, face);
+ const auto & colour = mesh.color(face);
+
+ std::vector<unsigned int> faceIndices;
+ for (const auto & heh : mesh.fh_range(face)) {
+ const auto & vertex = mesh.to_vertex_handle(heh);
+ const auto & textureUV = mesh.texcoord2D(heh);
+ const auto & point = mesh.point(vertex);
+ const auto & normal = smooth ? mesh.property(mesh.vertex_normals_pph(), vertex)
+ : mesh.property(mesh.face_normals_pph(), face);
+ Vertex outVertex {point, textureUV, normal, colour};
+ if (const auto existingItr = std::find(vertices.rbegin(), vertices.rend(), outVertex);
+ existingItr != vertices.rend()) {
+ faceIndices.push_back(static_cast<unsigned int>(std::distance(existingItr, vertices.rend()) - 1));
+ }
+ else {
+ faceIndices.push_back(static_cast<unsigned int>(vertices.size()));
+ vertices.emplace_back(outVertex);
+ }
+ }
+
+ for (unsigned int i = 2; i < faceIndices.size(); i++) {
+ indices.push_back(faceIndices[0]);
+ indices.push_back(faceIndices[i - 1]);
+ indices.push_back(faceIndices[i]);
}
}
- return std::make_shared<Mesh>(vertices, vectorOfN(vertices.size()));
+ return std::make_shared<Mesh>(vertices, indices);
}
bool