Skip to content

Commit

Permalink
Hydrogent: use line strip for mesh edges
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Nov 9, 2024
1 parent 20abb7f commit e7c0284
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
4 changes: 3 additions & 1 deletion Hydrogent/include/HnMeshUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Hydrogent/src/HnMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ void HnMesh::Invalidate()
struct HnMesh::StagingIndexData
{
pxr::VtVec3iArray FaceIndices;
pxr::VtVec2iArray EdgeIndices;
pxr::VtIntArray EdgeIndices;
pxr::VtIntArray PointIndices;
};

Expand Down Expand Up @@ -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);
}

Expand Down
55 changes: 38 additions & 17 deletions Hydrogent/src/HnMeshUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,36 +233,57 @@ 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;
ProcessFaces(
[&](size_t FaceId, int StartVertex, int VertCount) {
NumEdges += VertCount;
});

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)
{
const size_t NumFaces = m_Topology.GetFaceVertexCounts().size();
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(0xFFFFFFFF);
});
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 == 0xFFFFFFFF)
continue;

VERIFY_EXPR(Idx < NumVertexIndices);
Idx = FaceVertexIndices[Idx];
}
}

Expand Down
2 changes: 1 addition & 1 deletion Hydrogent/src/HnRenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e7c0284

Please sign in to comment.