Open mosra opened 8 years ago
Updated version:
std::optional<Trade::MeshData3D> meshData = ...;
if(!meshData || meshData->primitive() != MeshPrimitive::Triangles) {
Error() << "Cannot load collision mesh, skipping";
return;
}
/* this is a collision mesh, convert to bullet mesh */
btIndexedMesh bulletMesh;
bulletMesh.m_numTriangles = meshData->indices().size()/3;
bulletMesh.m_triangleIndexBase = reinterpret_cast<const unsigned char *>(meshData->indices().data());
bulletMesh.m_triangleIndexStride = 3 * sizeof(UnsignedInt);
bulletMesh.m_numVertices = meshData->positions(0).size();
bulletMesh.m_vertexBase = reinterpret_cast<const unsigned char *>(meshData->positions(0).data());
bulletMesh.m_vertexStride = sizeof(Vector3);
bulletMesh.m_indexType = PHY_INTEGER;
bulletMesh.m_vertexType = PHY_FLOAT;
The btIndexedMesh
can then further be used as follows:
btCollisionShape* shape = nullptr;
auto tivArray = new btTriangleIndexVertexArray());
tivArray->addIndexedMesh(bulletMesh, PHY_INTEGER);
if(shapeType == "TriangleMeshShape") {
/* exact shape, but worse performance */
shape = new btBvhTriangleMeshShape(tivArray, true);
} else {
/* convex hull, but better performance */
shape = new btConvexTriangleMeshShape(tivArray, true);
} /* btConvexHullShape can be even more performant */
PS: also CC0, in case anybody cares.
Will a free function in BulletIntegration
namespace suffice?
Yup, why not? :)
As @Squareys mentioned in #19, here's his code snippet for creating Bullet collision mesh directly from Magnum mesh data, licensed under CC0: https://groups.google.com/forum/#!topic/magnum-engine/V3iL35umkwU
Just putting it here since the Google Groups forum seems to be quite abandoned now (which I completely understand, the UI is horrible). I think this snippet may even be useful and general enough to include directly in the Bullet Integration library.