From 40cc01f79c6308b2e4b64fab73cb27d4221a648e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 19 Mar 2023 23:32:47 +0000 Subject: Dedupe vertices during asset factory mesh build This calls Vertex::== far too many times, but it's not (yet) enough to be a problem --- assetFactory/factoryMesh.cpp | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index 6cca388..8869efd 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -16,23 +16,31 @@ FactoryMesh::createMesh() const std::vector vertices; std::vector indices; for (const auto & face : mesh.faces()) { - const auto smooth = mesh.property(mesh.smoothFaceProperty, face); - const auto colour = mesh.color(face); - auto hrange = mesh.fh_range(face); - const unsigned int start = static_cast(vertices.size()); - for (const auto & heh : hrange) { + const auto & smooth = mesh.property(mesh.smoothFaceProperty, face); + const auto & colour = mesh.color(face); + + std::vector faceIndices; + for (const auto & heh : mesh.fh_range(face)) { const auto & vertex = mesh.to_vertex_handle(heh); - const auto textureUV = mesh.texcoord2D(heh); - vertices.emplace_back(mesh.point(vertex), textureUV, - smooth ? mesh.property(mesh.vertex_normals_pph(), vertex) - : mesh.property(mesh.face_normals_pph(), face), - colour); + 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(std::distance(existingItr, vertices.rend()) - 1)); + } + else { + faceIndices.push_back(static_cast(vertices.size())); + vertices.emplace_back(outVertex); + } } - const auto vcount = std::distance(hrange.begin(), hrange.end()); - for (unsigned int i = 2; i < vcount; i++) { - indices.push_back(start); - indices.push_back(start + i - 1); - indices.push_back(start + i); + + 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(vertices, indices); -- cgit v1.2.3