Skip to content

Commit

Permalink
MyMesh: split off IndexBuffer conversion from trianglesToLines()
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Apr 23, 2024
1 parent 557e401 commit 7e11b4f
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions HeartLibrary/src/main/java/jme3utilities/MyMesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -1501,20 +1501,24 @@ public static void translate(Mesh mesh, Vector3f offset) {
}

/**
* Convert mesh triangles to lines.
* Convert IndexBuffer triangles to lines.
*
* @param mesh the Mesh to modify (not null,
* mode=Triangles/TriangleFan/TriangleStrip)
* @param indexList the IndexBuffer to convert (not null, size a multiple of
* 3, unaffected)
* @param numVertices the number of vertices in the mesh (>0)
* @return a new buffer
*/
public static void trianglesToLines(Mesh mesh) {
Validate.nonNull(mesh, "mesh");
Validate.require(hasTriangles(mesh), "contain triangles");
public static IndexBuffer trianglesToLines(
IndexBuffer indexList, int numVertices) {
Validate.nonNull(indexList, "index list");
Validate.require(
(indexList.size() % MyMesh.vpt) == 0, "size a multiple of 3");
Validate.positive(numVertices, "number of vertices");

IndexBuffer indexList = mesh.getIndicesAsList();
int numTriangles = indexList.size() / vpt;
Set<IntPair> edgeSet = new HashSet<>(vpt * numTriangles);
int numTriangles = indexList.size() / MyMesh.vpt;
Set<IntPair> edgeSet = new HashSet<>(MyMesh.vpt * numTriangles);
for (int triIndex = 0; triIndex < numTriangles; ++triIndex) {
int intOffset = vpt * triIndex;
int intOffset = MyMesh.vpt * triIndex;
int ti0 = indexList.get(intOffset);
int ti1 = indexList.get(intOffset + 1);
int ti2 = indexList.get(intOffset + 2);
Expand All @@ -1524,19 +1528,38 @@ public static void trianglesToLines(Mesh mesh) {
edgeSet.add(new IntPair(ti1, ti2));
}
int numEdges = edgeSet.size();
int numIndices = vpe * numEdges;
int numIndices = MyMesh.vpe * numEdges;

IndexBuffer result
= IndexBuffer.createIndexBuffer(numVertices, numIndices);
for (IntPair edge : edgeSet) {
result.put(edge.smaller());
result.put(edge.larger());
}
Buffer ibData = result.getBuffer();
ibData.flip();

return result;
}

/**
* Convert mesh triangles to lines.
*
* @param mesh the Mesh to modify (not null,
* mode=Triangles/TriangleFan/TriangleStrip)
*/
public static void trianglesToLines(Mesh mesh) {
Validate.nonNull(mesh, "mesh");
Validate.require(hasTriangles(mesh), "contain triangles");

IndexBuffer indexList = mesh.getIndicesAsList();
int numVertices = mesh.getVertexCount();
IndexBuffer ib = trianglesToLines(indexList, numVertices);

mesh.clearBuffer(VertexBuffer.Type.Index);

IndexBuffer ib = IndexBuffer.createIndexBuffer(numVertices, numIndices);
for (IntPair edge : edgeSet) {
ib.put(edge.smaller());
ib.put(edge.larger());
}
VertexBuffer.Format ibFormat = ib.getFormat();
Buffer ibData = ib.getBuffer();
ibData.flip();
mesh.setBuffer(VertexBuffer.Type.Index, vpe, ibFormat, ibData);

mesh.setMode(Mesh.Mode.Lines);
Expand Down

0 comments on commit 7e11b4f

Please sign in to comment.