From 927e08acdba2ede624145dad7f37f3cab6265776 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Sun, 20 Oct 2019 23:59:59 -0700 Subject: [PATCH 1/8] Explore the simple mesh pure pos triangle list idea --- libraries/hfm/src/hfm/HFM.h | 2 ++ libraries/hfm/src/hfm/HFMModelMath.cpp | 20 ++++++++++++++++++++ libraries/hfm/src/hfm/HFMModelMath.h | 10 ++++++++++ 3 files changed, 32 insertions(+) diff --git a/libraries/hfm/src/hfm/HFM.h b/libraries/hfm/src/hfm/HFM.h index c61f03d070..bfea8a66af 100644 --- a/libraries/hfm/src/hfm/HFM.h +++ b/libraries/hfm/src/hfm/HFM.h @@ -260,6 +260,8 @@ public: graphics::MeshPointer _mesh; bool wasCompressed { false }; + + MeshIndexedTrianglesPos _meshAsIndexedTrianglePos; }; /// A single animation frame. diff --git a/libraries/hfm/src/hfm/HFMModelMath.cpp b/libraries/hfm/src/hfm/HFMModelMath.cpp index 0026378060..cf25c5fed6 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.cpp +++ b/libraries/hfm/src/hfm/HFMModelMath.cpp @@ -139,4 +139,24 @@ ReweightedDeformers getReweightedDeformers(const size_t numMeshVertices, const s return reweightedDeformers; } + +MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector srcParts) { + + 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.vertices = srcVertices; + + return dest; +} + }; diff --git a/libraries/hfm/src/hfm/HFMModelMath.h b/libraries/hfm/src/hfm/HFMModelMath.h index b80adad3d0..59c64fc490 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.h +++ b/libraries/hfm/src/hfm/HFMModelMath.h @@ -36,6 +36,16 @@ public: const uint16_t DEFAULT_SKINNING_WEIGHTS_PER_VERTEX = 4; ReweightedDeformers getReweightedDeformers(const size_t numMeshVertices, const std::vector skinClusters, const uint16_t weightsPerVertex = DEFAULT_SKINNING_WEIGHTS_PER_VERTEX); + + +struct MeshIndexedTrianglesPos { +public: + std::vector vertices; + std::vector indices; +}; + +MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector srcParts); + }; #endif // #define hifi_hfm_ModelMath_h From 24e6a966a8bd7e3d8bf803c50bdf70692366558b Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 21 Oct 2019 00:33:46 -0700 Subject: [PATCH 2/8] 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; } From 465e8c3e18b20c266731882fddf2149308157df9 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 21 Oct 2019 16:29:35 -0700 Subject: [PATCH 3/8] Prototyping the slim mesh generation --- libraries/fbx/src/FBXSerializer_Mesh.cpp | 1 + libraries/hfm/src/hfm/HFM.h | 1 + libraries/hfm/src/hfm/HFMModelMath.cpp | 19 +++++++++++++------ libraries/hfm/src/hfm/HFMModelMath.h | 3 ++- .../src/model-networking/ModelCache.cpp | 8 ++++++++ libraries/shared/src/GLMHelpers.h | 10 ++++++++++ 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/libraries/fbx/src/FBXSerializer_Mesh.cpp b/libraries/fbx/src/FBXSerializer_Mesh.cpp index e687f5e9f2..895059c6ad 100644 --- a/libraries/fbx/src/FBXSerializer_Mesh.cpp +++ b/libraries/fbx/src/FBXSerializer_Mesh.cpp @@ -174,6 +174,7 @@ void appendIndex(MeshData& data, QVector& indices, int index, bool deduplic data.indices.insert(vertex, newIndex); data.extracted.newIndices.insert(vertexIndex, newIndex); data.extracted.mesh.vertices.append(position); + data.extracted.mesh.positions.emplace_back(position); data.extracted.mesh.originalIndices.append(vertexIndex); data.extracted.mesh.normals.append(normal); data.extracted.mesh.texCoords.append(vertex.texCoord); diff --git a/libraries/hfm/src/hfm/HFM.h b/libraries/hfm/src/hfm/HFM.h index 41f2e1d501..ee3a25fc46 100644 --- a/libraries/hfm/src/hfm/HFM.h +++ b/libraries/hfm/src/hfm/HFM.h @@ -235,6 +235,7 @@ public: std::vector parts; + std::vector positions; QVector vertices; QVector normals; QVector tangents; diff --git a/libraries/hfm/src/hfm/HFMModelMath.cpp b/libraries/hfm/src/hfm/HFMModelMath.cpp index b5ff47e6c0..f34a43bf19 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.cpp +++ b/libraries/hfm/src/hfm/HFMModelMath.cpp @@ -16,6 +16,9 @@ #include +#include +#include + namespace hfm { void forEachIndex(const hfm::MeshPart& meshPart, std::function func) { @@ -142,12 +145,13 @@ ReweightedDeformers getReweightedDeformers(const size_t numMeshVertices, const s } -MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector srcParts) { +const MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector& srcParts) { MeshIndexedTrianglesPos dest; - dest.vertices.resize(srcVertices.size()); + // dest.vertices.resize(srcVertices.size()); + dest.vertices = srcVertices; - std::vector remap(srcVertices.size()); + /* std::vector remap(srcVertices.size()); { std::unordered_map uniqueVertices; int vi = 0; @@ -170,7 +174,7 @@ MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector vertices; std::vector indices; + std::vector parts; // Offset in the indices, Number of indices }; -MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector srcParts); +const MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector& srcParts); }; diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index bb911c6914..f427326f68 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -29,6 +29,8 @@ #include #include +#include + Q_LOGGING_CATEGORY(trace_resource_parse_geometry, "trace.resource.parse.geometry") class GeometryExtra { @@ -320,6 +322,7 @@ void ModelResource::setGeometryDefinition(HFMModel::Pointer hfmModel, const Mate _hfmModel = hfmModel; _materialMapping = materialMapping; + // Copy materials QHash materialIDAtlas; for (const HFMMaterial& material : _hfmModel->materials) { @@ -328,11 +331,16 @@ void ModelResource::setGeometryDefinition(HFMModel::Pointer hfmModel, const Mate } std::shared_ptr meshes = std::make_shared(); + std::vector triangleListMeshes = std::vector(); int meshID = 0; for (const HFMMesh& mesh : _hfmModel->meshes) { // Copy mesh pointers meshes->emplace_back(mesh._mesh); meshID++; + + auto simpleMesh = hfm::generateMeshIndexedTrianglePos(mesh.positions, mesh.parts); + + triangleListMeshes.emplace_back(simpleMesh); } _meshes = meshes; diff --git a/libraries/shared/src/GLMHelpers.h b/libraries/shared/src/GLMHelpers.h index cfb4bb6398..5787295da6 100644 --- a/libraries/shared/src/GLMHelpers.h +++ b/libraries/shared/src/GLMHelpers.h @@ -392,4 +392,14 @@ inline glm::vec4 extractFov( const glm::mat4& m) { return result; } +inline bool operator<(const glm::vec3& lhs, const glm::vec3& rhs) { + return (lhs.x < rhs.x) || ( + (lhs.x == rhs.x) && ( + (lhs.y < rhs.y) || ( + (lhs.y == rhs.y) && (lhs.z < rhs.z) + ) + ) + ); +} + #endif // hifi_GLMHelpers_h From e9ce467eb9e7d42aafdaccecb0a2c7aa3ca3cff3 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 22 Oct 2019 01:43:08 -0700 Subject: [PATCH 4/8] Crahs because of ? --- libraries/fbx/src/FBXSerializer_Mesh.cpp | 1 - libraries/hfm/src/hfm/HFM.h | 14 +++++-- libraries/hfm/src/hfm/HFMModelMath.cpp | 9 ++-- libraries/hfm/src/hfm/HFMModelMath.h | 13 +----- .../model-baker/src/model-baker/Baker.cpp | 41 ++++++++++++++++--- .../src/model-networking/ModelCache.cpp | 5 --- 6 files changed, 53 insertions(+), 30 deletions(-) diff --git a/libraries/fbx/src/FBXSerializer_Mesh.cpp b/libraries/fbx/src/FBXSerializer_Mesh.cpp index 895059c6ad..e687f5e9f2 100644 --- a/libraries/fbx/src/FBXSerializer_Mesh.cpp +++ b/libraries/fbx/src/FBXSerializer_Mesh.cpp @@ -174,7 +174,6 @@ void appendIndex(MeshData& data, QVector& indices, int index, bool deduplic data.indices.insert(vertex, newIndex); data.extracted.newIndices.insert(vertexIndex, newIndex); data.extracted.mesh.vertices.append(position); - data.extracted.mesh.positions.emplace_back(position); data.extracted.mesh.originalIndices.append(vertexIndex); data.extracted.mesh.normals.append(normal); data.extracted.mesh.texCoords.append(vertex.texCoord); diff --git a/libraries/hfm/src/hfm/HFM.h b/libraries/hfm/src/hfm/HFM.h index ee3a25fc46..419f47d680 100644 --- a/libraries/hfm/src/hfm/HFM.h +++ b/libraries/hfm/src/hfm/HFM.h @@ -229,14 +229,22 @@ public: bool needTangentSpace() const; }; + +/// Simple Triangle List vertices; + std::vector indices; + std::vector parts; // Offset in the indices, Number of indices +}; + /// A single mesh (with optional blendshapes). class Mesh { public: std::vector parts; - std::vector positions; QVector vertices; + std::vector _vertices; QVector normals; QVector tangents; QVector colors; @@ -255,6 +263,8 @@ public: // Blendshape attributes QVector blendshapes; + // Simple Triangle List Mesh generated during baking + hfm::TriangleListMesh triangleListMesh; QVector originalIndices; // Original indices of the vertices unsigned int meshIndex; // the order the meshes appeared in the object file @@ -262,8 +272,6 @@ public: graphics::MeshPointer _mesh; bool wasCompressed { false }; - 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 f34a43bf19..1678d5d405 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.cpp +++ b/libraries/hfm/src/hfm/HFMModelMath.cpp @@ -145,11 +145,12 @@ ReweightedDeformers getReweightedDeformers(const size_t numMeshVertices, const s } -const MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector& srcParts) { +const TriangleListMesh generateTriangleListMesh(const std::vector& srcVertices, const std::vector& srcParts) { - MeshIndexedTrianglesPos dest; - // dest.vertices.resize(srcVertices.size()); - dest.vertices = srcVertices; + TriangleListMesh dest; + + // just copy vertices + dest.vertices.insert(dest.vertices.end(), srcVertices.cbegin(), srcVertices.cend()); /* std::vector remap(srcVertices.size()); { diff --git a/libraries/hfm/src/hfm/HFMModelMath.h b/libraries/hfm/src/hfm/HFMModelMath.h index 38d3262042..dc397c5e6f 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.h +++ b/libraries/hfm/src/hfm/HFMModelMath.h @@ -25,8 +25,7 @@ void calculateExtentsForShape(hfm::Shape& shape, const std::vector& m void calculateExtentsForModel(Extents& modelExtents, const std::vector& shapes); -class ReweightedDeformers { -public: +struct ReweightedDeformers { std::vector indices; std::vector weights; uint16_t weightsPerVertex { 0 }; @@ -37,15 +36,7 @@ const uint16_t DEFAULT_SKINNING_WEIGHTS_PER_VERTEX = 4; ReweightedDeformers getReweightedDeformers(const size_t numMeshVertices, const std::vector skinClusters, const uint16_t weightsPerVertex = DEFAULT_SKINNING_WEIGHTS_PER_VERTEX); - -struct MeshIndexedTrianglesPos { -public: - std::vector vertices; - std::vector indices; - std::vector parts; // Offset in the indices, Number of indices -}; - -const MeshIndexedTrianglesPos generateMeshIndexedTrianglePos(const std::vector& srcVertices, const std::vector& srcParts); +const TriangleListMesh generateTriangleListMesh(const std::vector& srcVertices, const std::vector& srcParts); }; diff --git a/libraries/model-baker/src/model-baker/Baker.cpp b/libraries/model-baker/src/model-baker/Baker.cpp index c63495c169..c6c8be4bdd 100644 --- a/libraries/model-baker/src/model-baker/Baker.cpp +++ b/libraries/model-baker/src/model-baker/Baker.cpp @@ -23,6 +23,7 @@ #include "CalculateExtentsTask.h" #include "BuildDracoMeshTask.h" #include "ParseFlowDataTask.h" +#include namespace baker { @@ -49,6 +50,29 @@ namespace baker { } }; + class BuildMeshTriangleListTask { + public: + using Input = std::vector; + using Output = std::vector; + using JobModel = Job::ModelIO; + + void run(const BakeContextPointer& context, const Input& input, Output& output) { + const auto& meshesIn = input; + auto& indexedTrianglesMeshOut = output; + indexedTrianglesMeshOut.clear(); + indexedTrianglesMeshOut.resize(meshesIn.size()); + + for (int i = 0; i < meshesIn.size(); i++) { + auto& mesh = meshesIn[i]; + + auto meshPointer = const_cast (&mesh); + meshPointer->_vertices = meshPointer->vertices.toStdVector(); + + indexedTrianglesMeshOut[i] = hfm::generateTriangleListMesh(meshPointer->_vertices, mesh.parts); + } + } + }; + class BuildBlendshapesTask { public: using Input = VaryingSet3, std::vector>; @@ -80,21 +104,23 @@ namespace baker { class BuildMeshesTask { public: - using Input = VaryingSet5, std::vector, NormalsPerMesh, TangentsPerMesh, BlendshapesPerMesh>; + using Input = VaryingSet6, std::vector, std::vector, NormalsPerMesh, TangentsPerMesh, BlendshapesPerMesh>; using Output = std::vector; using JobModel = Job::ModelIO; void run(const BakeContextPointer& context, const Input& input, Output& output) { auto& meshesIn = input.get0(); int numMeshes = (int)meshesIn.size(); - auto& graphicsMeshesIn = input.get1(); - auto& normalsPerMeshIn = input.get2(); - auto& tangentsPerMeshIn = input.get3(); - auto& blendshapesPerMeshIn = input.get4(); + auto& triangleListMeshesIn = input.get1(); + auto& graphicsMeshesIn = input.get2(); + auto& normalsPerMeshIn = input.get3(); + auto& tangentsPerMeshIn = input.get4(); + auto& blendshapesPerMeshIn = input.get5(); auto meshesOut = meshesIn; for (int i = 0; i < numMeshes; i++) { auto& meshOut = meshesOut[i]; + meshOut.triangleListMesh = triangleListMeshesIn[i]; meshOut._mesh = safeGet(graphicsMeshesIn, i); meshOut.normals = QVector::fromStdVector(safeGet(normalsPerMeshIn, i)); meshOut.tangents = QVector::fromStdVector(safeGet(tangentsPerMeshIn, i)); @@ -162,6 +188,9 @@ namespace baker { const auto collectShapeVerticesInputs = CollectShapeVerticesTask::Input(meshesIn, shapesIn, jointsIn, skinDeformersIn).asVarying(); const auto shapeVerticesPerJoint = model.addJob("CollectShapeVertices", collectShapeVerticesInputs); + // Build the slim triangle list mesh for each hfm::mesh + const auto triangleListMeshes = model.addJob("BuildMeshTriangleListTask", meshesIn); + // Build the graphics::MeshPointer for each hfm::Mesh const auto buildGraphicsMeshInputs = BuildGraphicsMeshTask::Input(meshesIn, url, meshIndicesToModelNames, normalsPerMesh, tangentsPerMesh, shapesIn, skinDeformersIn).asVarying(); const auto graphicsMeshes = model.addJob("BuildGraphicsMesh", buildGraphicsMeshInputs); @@ -200,7 +229,7 @@ namespace baker { // Combine the outputs into a new hfm::Model const auto buildBlendshapesInputs = BuildBlendshapesTask::Input(blendshapesPerMeshIn, normalsPerBlendshapePerMesh, tangentsPerBlendshapePerMesh).asVarying(); const auto blendshapesPerMeshOut = model.addJob("BuildBlendshapes", buildBlendshapesInputs); - const auto buildMeshesInputs = BuildMeshesTask::Input(meshesIn, graphicsMeshes, normalsPerMesh, tangentsPerMesh, blendshapesPerMeshOut).asVarying(); + const auto buildMeshesInputs = BuildMeshesTask::Input(meshesIn, triangleListMeshes, graphicsMeshes, normalsPerMesh, tangentsPerMesh, blendshapesPerMeshOut).asVarying(); const auto meshesOut = model.addJob("BuildMeshes", buildMeshesInputs); const auto buildModelInputs = BuildModelTask::Input(hfmModelIn, meshesOut, jointsOut, jointRotationOffsets, jointIndices, flowData, shapeVerticesPerJoint, shapesOut, modelExtentsOut).asVarying(); const auto hfmModelOut = model.addJob("BuildModel", buildModelInputs); diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index f427326f68..e9674d6d26 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -331,16 +331,11 @@ void ModelResource::setGeometryDefinition(HFMModel::Pointer hfmModel, const Mate } std::shared_ptr meshes = std::make_shared(); - std::vector triangleListMeshes = std::vector(); int meshID = 0; for (const HFMMesh& mesh : _hfmModel->meshes) { // Copy mesh pointers meshes->emplace_back(mesh._mesh); meshID++; - - auto simpleMesh = hfm::generateMeshIndexedTrianglePos(mesh.positions, mesh.parts); - - triangleListMeshes.emplace_back(simpleMesh); } _meshes = meshes; From 7d37a064f2572a97d60b0c9039c6661d1ab96e02 Mon Sep 17 00:00:00 2001 From: sabrina-shanman Date: Wed, 30 Oct 2019 16:22:00 -0700 Subject: [PATCH 5/8] Fix crash --- libraries/hfm/src/hfm/HFMModelMath.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/hfm/src/hfm/HFMModelMath.cpp b/libraries/hfm/src/hfm/HFMModelMath.cpp index 1678d5d405..1086fb711c 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.cpp +++ b/libraries/hfm/src/hfm/HFMModelMath.cpp @@ -190,7 +190,7 @@ const TriangleListMesh generateTriangleListMesh(const std::vector& sr dest.indices[i] = qti; //remap[qti]; ++i; } - for (const auto& ti : part.quadTrianglesIndices) { + for (const auto& ti : part.triangleIndices) { dest.indices[i] = ti; //remap[ti]; ++i; } From 023d73a25da3b83ba3aea03acdc109043def44e3 Mon Sep 17 00:00:00 2001 From: sabrina-shanman Date: Thu, 31 Oct 2019 10:48:38 -0700 Subject: [PATCH 6/8] Finish TriangleListMesh generation, rename some things --- libraries/hfm/src/hfm/HFM.h | 2 +- libraries/hfm/src/hfm/HFMModelMath.cpp | 48 +++++++++---------- .../model-baker/src/model-baker/Baker.cpp | 2 +- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/libraries/hfm/src/hfm/HFM.h b/libraries/hfm/src/hfm/HFM.h index 419f47d680..07dc17c02c 100644 --- a/libraries/hfm/src/hfm/HFM.h +++ b/libraries/hfm/src/hfm/HFM.h @@ -230,7 +230,7 @@ public: }; -/// Simple Triangle List vertices; std::vector indices; diff --git a/libraries/hfm/src/hfm/HFMModelMath.cpp b/libraries/hfm/src/hfm/HFMModelMath.cpp index 1086fb711c..1aeaf6d2b9 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.cpp +++ b/libraries/hfm/src/hfm/HFMModelMath.cpp @@ -16,6 +16,9 @@ #include +// TODO: Remove after testing +#include + #include #include @@ -144,38 +147,36 @@ ReweightedDeformers getReweightedDeformers(const size_t numMeshVertices, const s return reweightedDeformers; } - const TriangleListMesh generateTriangleListMesh(const std::vector& srcVertices, const std::vector& srcParts) { TriangleListMesh dest; - // just copy vertices - dest.vertices.insert(dest.vertices.end(), srcVertices.cbegin(), srcVertices.cend()); + // copy vertices for now + dest.vertices = srcVertices; - /* std::vector remap(srcVertices.size()); + std::vector oldToNewIndex(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; + std::unordered_map uniqueVertexToNewIndex; + int oldIndex = 0; + int newIndex = 0; + for (const auto& srcVertex : srcVertices) { + auto foundIndex = uniqueVertexToNewIndex.find(srcVertex); + if (foundIndex != uniqueVertexToNewIndex.end()) { + oldToNewIndex[oldIndex] = foundIndex->second; } else { - uniqueVertices[v] = vu; - remap[vi] = vu; - dest.vertices[vu] = v; - vu++; + uniqueVertexToNewIndex[srcVertex] = newIndex; + oldToNewIndex[oldIndex] = newIndex; + dest.vertices[newIndex] = srcVertex; + ++newIndex; } - ++vi; + ++oldIndex; } - if (uniqueVertices.size() < srcVertices.size()) { - dest.vertices.resize(uniqueVertices.size()); + if (uniqueVertexToNewIndex.size() < srcVertices.size()) { + dest.vertices.resize(uniqueVertexToNewIndex.size()); dest.vertices.shrink_to_fit(); - } } -*/ + auto newIndicesCount = 0; for (const auto& part : srcParts) { newIndicesCount += part.triangleIndices.size() + part.quadTrianglesIndices.size(); @@ -187,18 +188,15 @@ const TriangleListMesh generateTriangleListMesh(const std::vector& sr for (const auto& part : srcParts) { glm::ivec2 spart(i, 0); for (const auto& qti : part.quadTrianglesIndices) { - dest.indices[i] = qti; //remap[qti]; + dest.indices[i] = oldToNewIndex[qti]; ++i; } for (const auto& ti : part.triangleIndices) { - dest.indices[i] = ti; //remap[ti]; + dest.indices[i] = oldToNewIndex[ti]; ++i; } spart.y = i - spart.x; dest.parts.push_back(spart); - - // dest.indices.insert(dest.indices.end(), part.quadTrianglesIndices.cbegin(), part.quadTrianglesIndices.cend()); - // dest.indices.insert(dest.indices.end(), part.triangleIndices.cbegin(), part.triangleIndices.cend()); } } diff --git a/libraries/model-baker/src/model-baker/Baker.cpp b/libraries/model-baker/src/model-baker/Baker.cpp index c6c8be4bdd..f17db7397e 100644 --- a/libraries/model-baker/src/model-baker/Baker.cpp +++ b/libraries/model-baker/src/model-baker/Baker.cpp @@ -65,7 +65,7 @@ namespace baker { for (int i = 0; i < meshesIn.size(); i++) { auto& mesh = meshesIn[i]; - auto meshPointer = const_cast (&mesh); + auto meshPointer = const_cast(&mesh); meshPointer->_vertices = meshPointer->vertices.toStdVector(); indexedTrianglesMeshOut[i] = hfm::generateTriangleListMesh(meshPointer->_vertices, mesh.parts); From 297ce9d88bbe94fabf95823fb804a2b89731cc08 Mon Sep 17 00:00:00 2001 From: sabrina-shanman Date: Thu, 31 Oct 2019 13:42:19 -0700 Subject: [PATCH 7/8] Remove debug --- libraries/hfm/src/hfm/HFM.h | 1 - libraries/hfm/src/hfm/HFMModelMath.cpp | 4 ---- .../model-networking/src/model-networking/ModelCache.cpp | 3 --- 3 files changed, 8 deletions(-) diff --git a/libraries/hfm/src/hfm/HFM.h b/libraries/hfm/src/hfm/HFM.h index 07dc17c02c..ec06832f22 100644 --- a/libraries/hfm/src/hfm/HFM.h +++ b/libraries/hfm/src/hfm/HFM.h @@ -244,7 +244,6 @@ public: std::vector parts; QVector vertices; - std::vector _vertices; QVector normals; QVector tangents; QVector colors; diff --git a/libraries/hfm/src/hfm/HFMModelMath.cpp b/libraries/hfm/src/hfm/HFMModelMath.cpp index 1aeaf6d2b9..d0288d684c 100644 --- a/libraries/hfm/src/hfm/HFMModelMath.cpp +++ b/libraries/hfm/src/hfm/HFMModelMath.cpp @@ -12,13 +12,9 @@ #include "HFMModelMath.h" #include -#include "ModelFormatLogging.h" #include -// TODO: Remove after testing -#include - #include #include diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index e9674d6d26..bb911c6914 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -29,8 +29,6 @@ #include #include -#include - Q_LOGGING_CATEGORY(trace_resource_parse_geometry, "trace.resource.parse.geometry") class GeometryExtra { @@ -322,7 +320,6 @@ void ModelResource::setGeometryDefinition(HFMModel::Pointer hfmModel, const Mate _hfmModel = hfmModel; _materialMapping = materialMapping; - // Copy materials QHash materialIDAtlas; for (const HFMMaterial& material : _hfmModel->materials) { From 6666df6137f79b50c79f5ac7293dca60e58a4dfc Mon Sep 17 00:00:00 2001 From: sabrina-shanman Date: Thu, 31 Oct 2019 14:49:28 -0700 Subject: [PATCH 8/8] Fix build warnings/errors --- libraries/model-baker/src/model-baker/Baker.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/libraries/model-baker/src/model-baker/Baker.cpp b/libraries/model-baker/src/model-baker/Baker.cpp index f17db7397e..bd39b3178f 100644 --- a/libraries/model-baker/src/model-baker/Baker.cpp +++ b/libraries/model-baker/src/model-baker/Baker.cpp @@ -62,13 +62,10 @@ namespace baker { indexedTrianglesMeshOut.clear(); indexedTrianglesMeshOut.resize(meshesIn.size()); - for (int i = 0; i < meshesIn.size(); i++) { + for (size_t i = 0; i < meshesIn.size(); i++) { auto& mesh = meshesIn[i]; - - auto meshPointer = const_cast(&mesh); - meshPointer->_vertices = meshPointer->vertices.toStdVector(); - - indexedTrianglesMeshOut[i] = hfm::generateTriangleListMesh(meshPointer->_vertices, mesh.parts); + const auto verticesStd = mesh.vertices.toStdVector(); + indexedTrianglesMeshOut[i] = hfm::generateTriangleListMesh(verticesStd, mesh.parts); } } };