don't forget to use the transform of child shapes

This commit is contained in:
Andrew Meadows 2016-07-14 13:30:57 -07:00
parent 1e95e489cb
commit 06d40afeac

View file

@ -20,8 +20,14 @@
float verts[3 * MAX_HULL_POINTS];
void copyHullToMesh(const btShapeHull& hull, model::MeshPointer mesh) {
if ((bool)mesh) {
void copyShapeToMesh(const btTransform& transform, const btConvexShape* shape, model::MeshPointer mesh) {
assert((bool)mesh);
assert(shape);
btShapeHull hull(shape);
const btScalar MARGIN = 0.0f;
hull.buildHull(MARGIN);
const uint32_t* hullIndices = hull.getIndexPointer();
int32_t numIndices = hull.numIndices();
assert(numIndices <= 6 * MAX_HULL_POINTS);
@ -42,10 +48,11 @@ void copyHullToMesh(const btShapeHull& hull, model::MeshPointer mesh) {
int32_t numVertices = hull.numVertices();
assert(numVertices <= MAX_HULL_POINTS);
for (int32_t i = 0; i < numVertices; ++i) {
float* data = verts + 3 * i;
data[0] = hullVertices[i].getX();
data[1] = hullVertices[i].getY();
data[2] = hullVertices[i].getZ();
btVector3 transformedPoint = transform * hullVertices[i];
memcpy(transformedPoint.m_floats, verts + 3 * i, 3 * sizeof(float));
//data[0] = transformedPoint.getX();
//data[1] = transformedPoint.getY();
//data[2] = transformedPoint.getZ();
}
gpu::BufferView::Size numBytes = sizeof(float) * (3 * numVertices);
@ -59,28 +66,25 @@ void copyHullToMesh(const btShapeHull& hull, model::MeshPointer mesh) {
mesh->getIndexBuffer()._buffer->append(numBytes, data);
}
}
}
model::MeshPointer createMeshFromShape(const btCollisionShape* shape) {
if (!shape) {
return std::make_shared<model::Mesh>();
}
model::MeshPointer mesh = std::make_shared<model::Mesh>();
if (shape) {
int32_t shapeType = shape->getShapeType();
if (shapeType == (int32_t)COMPOUND_SHAPE_PROXYTYPE) {
const btScalar MARGIN = 0.0f;
const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(shape);
int32_t numSubShapes = compoundShape->getNumChildShapes();
for (int i = 0; i < numSubShapes; ++i) {
const btCollisionShape* childShape = compoundShape->getChildShape(i);
if (childShape->isConvex()) {
const btConvexShape* convexShape = static_cast<const btConvexShape*>(childShape);
btShapeHull shapeHull(convexShape);
shapeHull.buildHull(MARGIN);
copyHullToMesh(shapeHull, mesh);
copyShapeToMesh(compoundShape->getChildTransform(i), convexShape, mesh);
}
}
} else if (shape->isConvex()) {
const btConvexShape* convexShape = static_cast<const btConvexShape*>(shape);
copyShapeToMesh(btTransform(), convexShape, mesh);
}
}
return mesh;
}