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 1202903
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 27 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
19 changes: 13 additions & 6 deletions Hydrogent/src/HnGeometryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,16 +765,23 @@ class HnGeometryPool::IndexData final : public GeometryPoolData
ExistingData{std::move(_ExistingData)},
Indices{std::move(_Indices)}
{
auto ShiftIndex = [StartVertex](int& Idx) {
if (Idx >= 0)
{
Idx += static_cast<int>(StartVertex);
}
};

if (Indices.IsHolding<pxr::VtVec3iArray>())
{
if (StartVertex != 0)
{
pxr::VtVec3iArray IndicesArray = Indices.UncheckedRemove<pxr::VtVec3iArray>();
for (pxr::GfVec3i& idx : IndicesArray)
{
idx[0] += StartVertex;
idx[1] += StartVertex;
idx[2] += StartVertex;
ShiftIndex(idx[0]);
ShiftIndex(idx[1]);
ShiftIndex(idx[2]);
}
Indices = pxr::VtValue::Take(IndicesArray);
}
Expand All @@ -790,8 +797,8 @@ class HnGeometryPool::IndexData final : public GeometryPoolData
pxr::VtVec2iArray IndicesArray = Indices.UncheckedRemove<pxr::VtVec2iArray>();
for (pxr::GfVec2i& idx : IndicesArray)
{
idx[0] += StartVertex;
idx[1] += StartVertex;
ShiftIndex(idx[0]);
ShiftIndex(idx[1]);
}
Indices = pxr::VtValue::Take(IndicesArray);
}
Expand All @@ -807,7 +814,7 @@ class HnGeometryPool::IndexData final : public GeometryPoolData
{
pxr::VtIntArray IndicesArray = Indices.UncheckedRemove<pxr::VtIntArray>();
for (int& idx : IndicesArray)
idx += StartVertex;
ShiftIndex(idx);
Indices = pxr::VtValue::Take(IndicesArray);
}

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
56 changes: 39 additions & 17 deletions Hydrogent/src/HnMeshUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

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 1202903

Please sign in to comment.