use of com.bulletphysics.collision.shapes.ShapeHull in project jmonkeyengine by jMonkeyEngine.
the class BufferedTriangleCallback method getVertices.
/**
* Processes the given convex shape to retrieve a correctly ordered FloatBuffer to
* construct the shape from with a TriMesh.
*
* @param convexShape the shape to retreieve the vertices from.
* @return the vertices as a FloatBuffer, ordered as Triangles.
*/
private static FloatBuffer getVertices(ConvexShape convexShape) {
// Check there is a hull shape to render
if (convexShape.getUserPointer() == null) {
// create a hull approximation
ShapeHull hull = new ShapeHull(convexShape);
float margin = convexShape.getMargin();
hull.buildHull(margin);
convexShape.setUserPointer(hull);
}
// Assert state - should have a pointer to a hull (shape) that'll be drawn
assert convexShape.getUserPointer() != null : "Should have a shape for the userPointer, instead got null";
ShapeHull hull = (ShapeHull) convexShape.getUserPointer();
// Assert we actually have a shape to render
assert hull.numTriangles() > 0 : "Expecting the Hull shape to have triangles";
int numberOfTriangles = hull.numTriangles();
// The number of bytes needed is: (floats in a vertex) * (vertices in a triangle) * (# of triangles) * (size of float in bytes)
final int numberOfFloats = 3 * 3 * numberOfTriangles;
FloatBuffer vertices = BufferUtils.createFloatBuffer(numberOfFloats);
// Force the limit, set the cap - most number of floats we will use the buffer for
vertices.limit(numberOfFloats);
// Loop variables
final IntArrayList hullIndicies = hull.getIndexPointer();
final List<Vector3f> hullVertices = hull.getVertexPointer();
Vector3f vertexA, vertexB, vertexC;
int index = 0;
for (int i = 0; i < numberOfTriangles; i++) {
// Grab the data for this triangle from the hull
vertexA = hullVertices.get(hullIndicies.get(index++));
vertexB = hullVertices.get(hullIndicies.get(index++));
vertexC = hullVertices.get(hullIndicies.get(index++));
// Put the verticies into the vertex buffer
vertices.put(vertexA.x).put(vertexA.y).put(vertexA.z);
vertices.put(vertexB.x).put(vertexB.y).put(vertexB.z);
vertices.put(vertexC.x).put(vertexC.y).put(vertexC.z);
}
vertices.clear();
return vertices;
}
Aggregations