From 8fdbf885a8c688ade0ad7ce429863971ddfe57d4 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sat, 9 Nov 2024 09:29:18 -0800 Subject: [PATCH] Hydrogent: use line strip for mesh edges --- Hydrogent/include/HnMeshUtils.hpp | 4 ++- Hydrogent/src/HnMesh.cpp | 4 +-- Hydrogent/src/HnMeshUtils.cpp | 56 +++++++++++++++++++++---------- Hydrogent/src/HnRenderPass.cpp | 2 +- 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/Hydrogent/include/HnMeshUtils.hpp b/Hydrogent/include/HnMeshUtils.hpp index fc5224c2..6bd43243 100644 --- a/Hydrogent/include/HnMeshUtils.hpp +++ b/Hydrogent/include/HnMeshUtils.hpp @@ -82,6 +82,8 @@ class HnMeshUtils final /// Computes the edge indices. /// /// \param[in] UseFaceVertexIndices - Whether to use face vertex indices. + /// \param[in] UseLineStrip - Whether to use line strip topology. + /// If false, line list will be used. /// \return The edge indices. /// /// Example: @@ -102,7 +104,7 @@ class HnMeshUtils final /// /// UseFaceVertexIndices == true /// EdgeIndices = {0, 1, 1, 2, 2, 3, 3, 0, 3, 2, 2, 4, 4, 5, 5, 3} - pxr::VtVec2iArray ComputeEdgeIndices(bool UseFaceVertexIndices) const; + pxr::VtIntArray ComputeEdgeIndices(bool UseFaceVertexIndices, bool UseLineStrip = false) const; /// Computes the point indices. diff --git a/Hydrogent/src/HnMesh.cpp b/Hydrogent/src/HnMesh.cpp index e18be0f3..ba8481de 100644 --- a/Hydrogent/src/HnMesh.cpp +++ b/Hydrogent/src/HnMesh.cpp @@ -320,7 +320,7 @@ void HnMesh::Invalidate() struct HnMesh::StagingIndexData { pxr::VtVec3iArray FaceIndices; - pxr::VtVec2iArray EdgeIndices; + pxr::VtIntArray EdgeIndices; pxr::VtIntArray PointIndices; }; @@ -1085,7 +1085,7 @@ void HnMesh::UpdateIndexData(StagingIndexData& StagingInds, const pxr::VtValue& m_IndexData.Subsets.clear(); } - StagingInds.EdgeIndices = MeshUtils.ComputeEdgeIndices(!m_HasFaceVaryingPrimvars); + StagingInds.EdgeIndices = MeshUtils.ComputeEdgeIndices(!m_HasFaceVaryingPrimvars, true); StagingInds.PointIndices = MeshUtils.ComputePointIndices(m_HasFaceVaryingPrimvars); } diff --git a/Hydrogent/src/HnMeshUtils.cpp b/Hydrogent/src/HnMeshUtils.cpp index 1541d3b4..dc5766b6 100644 --- a/Hydrogent/src/HnMeshUtils.cpp +++ b/Hydrogent/src/HnMeshUtils.cpp @@ -233,36 +233,58 @@ void HnMeshUtils::Triangulate(bool UseFaceVertexIndices, } } -pxr::VtVec2iArray HnMeshUtils::ComputeEdgeIndices(bool UseFaceVertexIndices) const +pxr::VtIntArray HnMeshUtils::ComputeEdgeIndices(bool UseFaceVertexIndices, bool UseLineStrip) const { size_t NumEdges = 0; + size_t NumFaces = 0; // Count the actual number of faces ProcessFaces( [&](size_t FaceId, int StartVertex, int VertCount) { NumEdges += VertCount; + ++NumFaces; }); - pxr::VtVec2iArray EdgeIndices; - EdgeIndices.reserve(NumEdges); - - ProcessFaces( - [&](size_t FaceId, int StartVertex, int VertCount) { - for (int v = 0; v < VertCount - 1; ++v) - EdgeIndices.push_back({StartVertex + v, StartVertex + (v + 1)}); - EdgeIndices.push_back({StartVertex + VertCount - 1, StartVertex}); - }); - VERIFY_EXPR(EdgeIndices.size() == NumEdges); + pxr::VtIntArray EdgeIndices; + if (UseLineStrip) + { + EdgeIndices.reserve(NumEdges + NumFaces * 2); + ProcessFaces( + [&](size_t FaceId, int StartVertex, int VertCount) { + for (int v = 0; v < VertCount; ++v) + { + EdgeIndices.push_back(StartVertex + v); + } + EdgeIndices.push_back(StartVertex); + EdgeIndices.push_back(-1); + }); + VERIFY_EXPR(EdgeIndices.size() == NumEdges + NumFaces * 2); + } + else + { + EdgeIndices.reserve(NumEdges * 2); + ProcessFaces( + [&](size_t FaceId, int StartVertex, int VertCount) { + for (int v = 0; v < VertCount - 1; ++v) + { + EdgeIndices.push_back(StartVertex + v); + EdgeIndices.push_back(StartVertex + (v + 1)); + } + EdgeIndices.push_back(StartVertex + VertCount - 1); + EdgeIndices.push_back(StartVertex); + }); + VERIFY_EXPR(EdgeIndices.size() == NumEdges * 2); + } if (UseFaceVertexIndices) { const int* FaceVertexIndices = m_Topology.GetFaceVertexIndices().cdata(); const size_t NumVertexIndices = m_Topology.GetFaceVertexIndices().size(); - for (pxr::GfVec2i& Edge : EdgeIndices) + for (int& Idx : EdgeIndices) { - int& Idx0 = Edge[0]; - int& Idx1 = Edge[1]; - VERIFY_EXPR(Idx0 < NumVertexIndices && Idx1 < NumVertexIndices); - Idx0 = FaceVertexIndices[Idx0]; - Idx1 = FaceVertexIndices[Idx1]; + if (Idx < 0) + continue; + + VERIFY_EXPR(Idx < NumVertexIndices); + Idx = FaceVertexIndices[Idx]; } } diff --git a/Hydrogent/src/HnRenderPass.cpp b/Hydrogent/src/HnRenderPass.cpp index 2d654e5f..a5bdd435 100644 --- a/Hydrogent/src/HnRenderPass.cpp +++ b/Hydrogent/src/HnRenderPass.cpp @@ -237,7 +237,7 @@ GraphicsPipelineDesc HnRenderPass::GetGraphicsDesc(const HnRenderPassState& RPSt break; case HN_RENDER_MODE_MESH_EDGES: - GraphicsDesc.PrimitiveTopology = PRIMITIVE_TOPOLOGY_LINE_LIST; + GraphicsDesc.PrimitiveTopology = PRIMITIVE_TOPOLOGY_LINE_STRIP; break; case HN_RENDER_MODE_POINTS: