From 24e6a966a8bd7e3d8bf803c50bdf70692366558b Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 21 Oct 2019 00:33:46 -0700 Subject: [PATCH] Keep exploring --- libraries/hfm/src/hfm/HFM.h | 3 +- libraries/hfm/src/hfm/HFMModelMath.cpp | 52 ++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/libraries/hfm/src/hfm/HFM.h b/libraries/hfm/src/hfm/HFM.h index bfea8a66af..41f2e1d501 100644 --- a/libraries/hfm/src/hfm/HFM.h +++ b/libraries/hfm/src/hfm/HFM.h @@ -261,7 +261,8 @@ public: graphics::MeshPointer _mesh; bool wasCompressed { false }; - MeshIndexedTrianglesPos _meshAsIndexedTrianglePos; + std::vector uniqueVertices; + std::vector trianglesIndices; }; /// A single animation frame. diff --git a/libraries/hfm/src/hfm/HFMModelMath.cpp b/libraries/hfm/src/hfm/HFMModelMath.cpp index cf25c5fed6..b5ff47e6c0 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.cpp +++ b/libraries/hfm/src/hfm/HFMModelMath.cpp @@ -14,6 +14,8 @@ #include #include "ModelFormatLogging.h" +#include + namespace hfm { void forEachIndex(const hfm::MeshPart& meshPart, std::function func) { @@ -142,19 +144,55 @@ ReweightedDeformers getReweightedDeformers(const size_t numMeshVertices, const s MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector srcParts) { + MeshIndexedTrianglesPos dest; + dest.vertices.resize(srcVertices.size()); + + std::vector remap(srcVertices.size()); + { + std::unordered_map uniqueVertices; + int vi = 0; + int vu = 0; + for (const auto& v : srcVertices) { + auto foundIndex = uniqueVertices.find(v); + if (foundIndex != uniqueVertices.end()) { + remap[vi] = foundIndex->second; + } else { + uniqueVertices[v] = vu; + remap[vi] = vu; + dest.vertices[vu] = v; + vu++; + } + ++vi; + } + if (uniqueVertices.size() < srcVertices.size()) { + dest.vertices.resize(uniqueVertices.size()); + dest.vertices.shrink_to_fit(); + + } + } + auto newIndicesCount = 0; for (const auto& part : srcParts) { newIndicesCount += part.triangleIndices.size() + part.quadTrianglesIndices.size(); } - MeshIndexedTrianglesPos dest; - dest.indices.reserve(newIndicesCount); - for (const auto& part : srcParts) { - dest.indices.insert(dest.indices.end(), part.triangleIndices.cbegin(), part.triangleIndices.cend()); - dest.indices.insert(dest.indices.end(), part.quadTrianglesIndices.cbegin(), part.quadTrianglesIndices.cend()); - } + { + dest.indices.resize(newIndicesCount); + int i = 0; + for (const auto& part : srcParts) { + for (const auto& qti : part.quadTrianglesIndices) { + dest.indices[i] = remap[qti]; + ++i; + } + for (const auto& ti : part.quadTrianglesIndices) { + dest.indices[i] = remap[ti]; + ++i; + } - dest.vertices = srcVertices; + // dest.indices.insert(dest.indices.end(), part.quadTrianglesIndices.cbegin(), part.quadTrianglesIndices.cend()); + // dest.indices.insert(dest.indices.end(), part.triangleIndices.cbegin(), part.triangleIndices.cend()); + } + } return dest; }