2016-05-13 06:03:12 +00:00
|
|
|
|
|
|
|
#include "CollisionShape2TriangleMesh.h"
|
|
|
|
|
|
|
|
#include "btBulletCollisionCommon.h"
|
2018-09-23 21:17:31 +00:00
|
|
|
#include "BulletCollision/CollisionShapes/btShapeHull.h" //to create a tesselation of a generic btConvexShape
|
2018-11-13 05:46:11 +00:00
|
|
|
#include "BulletCollision/CollisionShapes/btConvexPolyhedron.h"
|
2016-05-13 06:03:12 +00:00
|
|
|
|
|
|
|
void CollisionShape2TriangleMesh(btCollisionShape* collisionShape, const btTransform& parentTransform, btAlignedObjectArray<btVector3>& vertexPositions, btAlignedObjectArray<btVector3>& vertexNormals, btAlignedObjectArray<int>& indicesOut)
|
|
|
|
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
//todo: support all collision shape types
|
2016-05-13 06:03:12 +00:00
|
|
|
switch (collisionShape->getShapeType())
|
|
|
|
{
|
|
|
|
case SOFTBODY_SHAPE_PROXYTYPE:
|
|
|
|
{
|
|
|
|
//skip the soft body collision shape for now
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STATIC_PLANE_PROXYTYPE:
|
|
|
|
{
|
|
|
|
//draw a box, oriented along the plane normal
|
|
|
|
const btStaticPlaneShape* staticPlaneShape = static_cast<const btStaticPlaneShape*>(collisionShape);
|
|
|
|
btScalar planeConst = staticPlaneShape->getPlaneConstant();
|
|
|
|
const btVector3& planeNormal = staticPlaneShape->getPlaneNormal();
|
|
|
|
btVector3 planeOrigin = planeNormal * planeConst;
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 vec0, vec1;
|
|
|
|
btPlaneSpace1(planeNormal, vec0, vec1);
|
2016-05-13 06:03:12 +00:00
|
|
|
btScalar vecLen = 100.f;
|
|
|
|
btVector3 verts[4];
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
verts[0] = planeOrigin + vec0 * vecLen + vec1 * vecLen;
|
|
|
|
verts[1] = planeOrigin - vec0 * vecLen + vec1 * vecLen;
|
|
|
|
verts[2] = planeOrigin - vec0 * vecLen - vec1 * vecLen;
|
|
|
|
verts[3] = planeOrigin + vec0 * vecLen - vec1 * vecLen;
|
|
|
|
|
2016-05-13 06:03:12 +00:00
|
|
|
int startIndex = vertexPositions.size();
|
2018-09-23 21:17:31 +00:00
|
|
|
indicesOut.push_back(startIndex + 0);
|
|
|
|
indicesOut.push_back(startIndex + 1);
|
|
|
|
indicesOut.push_back(startIndex + 2);
|
|
|
|
indicesOut.push_back(startIndex + 0);
|
|
|
|
indicesOut.push_back(startIndex + 2);
|
|
|
|
indicesOut.push_back(startIndex + 3);
|
2016-05-13 06:03:12 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 triNormal = parentTransform.getBasis() * planeNormal;
|
2016-05-13 06:03:12 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
|
|
|
btVector3 vtxPos;
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 pos = parentTransform * verts[i];
|
2016-05-13 06:03:12 +00:00
|
|
|
vertexPositions.push_back(pos);
|
|
|
|
vertexNormals.push_back(triNormal);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TRIANGLE_MESH_SHAPE_PROXYTYPE:
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
btBvhTriangleMeshShape* trimesh = (btBvhTriangleMeshShape*)collisionShape;
|
2016-05-13 06:03:12 +00:00
|
|
|
btVector3 trimeshScaling = trimesh->getLocalScaling();
|
|
|
|
btStridingMeshInterface* meshInterface = trimesh->getMeshInterface();
|
|
|
|
btAlignedObjectArray<btVector3> vertices;
|
|
|
|
btAlignedObjectArray<int> indices;
|
2018-09-23 21:17:31 +00:00
|
|
|
|
|
|
|
for (int partId = 0; partId < meshInterface->getNumSubParts(); partId++)
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
const unsigned char* vertexbase = 0;
|
2016-05-13 06:03:12 +00:00
|
|
|
int numverts = 0;
|
|
|
|
PHY_ScalarType type = PHY_INTEGER;
|
|
|
|
int stride = 0;
|
2018-09-23 21:17:31 +00:00
|
|
|
const unsigned char* indexbase = 0;
|
2016-05-13 06:03:12 +00:00
|
|
|
int indexstride = 0;
|
|
|
|
int numfaces = 0;
|
|
|
|
PHY_ScalarType indicestype = PHY_INTEGER;
|
|
|
|
//PHY_ScalarType indexType=0;
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2016-05-13 06:03:12 +00:00
|
|
|
btVector3 triangleVerts[3];
|
2018-09-23 21:17:31 +00:00
|
|
|
meshInterface->getLockedReadOnlyVertexIndexBase(&vertexbase, numverts, type, stride, &indexbase, indexstride, numfaces, indicestype, partId);
|
|
|
|
btVector3 aabbMin, aabbMax;
|
|
|
|
|
|
|
|
for (int triangleIndex = 0; triangleIndex < numfaces; triangleIndex++)
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
unsigned int* gfxbase = (unsigned int*)(indexbase + triangleIndex * indexstride);
|
|
|
|
|
|
|
|
for (int j = 2; j >= 0; j--)
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
int graphicsindex = indicestype == PHY_SHORT ? ((unsigned short*)gfxbase)[j] : gfxbase[j];
|
2016-05-13 06:03:12 +00:00
|
|
|
if (type == PHY_FLOAT)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
float* graphicsbase = (float*)(vertexbase + graphicsindex * stride);
|
2016-05-13 06:03:12 +00:00
|
|
|
triangleVerts[j] = btVector3(
|
2018-09-23 21:17:31 +00:00
|
|
|
graphicsbase[0] * trimeshScaling.getX(),
|
|
|
|
graphicsbase[1] * trimeshScaling.getY(),
|
|
|
|
graphicsbase[2] * trimeshScaling.getZ());
|
2016-05-13 06:03:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
double* graphicsbase = (double*)(vertexbase + graphicsindex * stride);
|
|
|
|
triangleVerts[j] = btVector3(btScalar(graphicsbase[0] * trimeshScaling.getX()),
|
|
|
|
btScalar(graphicsbase[1] * trimeshScaling.getY()),
|
|
|
|
btScalar(graphicsbase[2] * trimeshScaling.getZ()));
|
2016-05-13 06:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
indices.push_back(vertices.size());
|
|
|
|
vertices.push_back(triangleVerts[0]);
|
|
|
|
indices.push_back(vertices.size());
|
|
|
|
vertices.push_back(triangleVerts[1]);
|
|
|
|
indices.push_back(vertices.size());
|
|
|
|
vertices.push_back(triangleVerts[2]);
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 triNormal = (triangleVerts[1] - triangleVerts[0]).cross(triangleVerts[2] - triangleVerts[0]);
|
2017-01-31 02:12:09 +00:00
|
|
|
btScalar dot = triNormal.dot(triNormal);
|
2016-05-13 06:03:12 +00:00
|
|
|
|
2017-01-31 02:12:09 +00:00
|
|
|
//cull degenerate triangles
|
2018-09-23 21:17:31 +00:00
|
|
|
if (dot >= SIMD_EPSILON * SIMD_EPSILON)
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
2017-01-31 02:12:09 +00:00
|
|
|
triNormal /= btSqrt(dot);
|
|
|
|
for (int v = 0; v < 3; v++)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 pos = parentTransform * triangleVerts[v];
|
2017-01-31 02:12:09 +00:00
|
|
|
indicesOut.push_back(vertexPositions.size());
|
|
|
|
vertexPositions.push_back(pos);
|
|
|
|
vertexNormals.push_back(triNormal);
|
|
|
|
}
|
2016-05-13 06:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2016-05-13 06:03:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
if (collisionShape->isConvex())
|
|
|
|
{
|
|
|
|
btConvexShape* convex = (btConvexShape*)collisionShape;
|
|
|
|
{
|
2018-11-13 05:46:11 +00:00
|
|
|
const btConvexPolyhedron* pol = 0;
|
|
|
|
if (convex->isPolyhedral())
|
|
|
|
{
|
|
|
|
btPolyhedralConvexShape* poly = (btPolyhedralConvexShape*)convex;
|
|
|
|
pol = poly->getConvexPolyhedron();
|
|
|
|
}
|
2019-03-25 21:41:58 +00:00
|
|
|
|
2018-11-13 05:46:11 +00:00
|
|
|
if (pol)
|
|
|
|
{
|
2020-03-17 06:32:26 +00:00
|
|
|
int baseIndex = vertexPositions.size();
|
2018-11-13 05:46:11 +00:00
|
|
|
for (int v = 0; v < pol->m_vertices.size(); v++)
|
|
|
|
{
|
|
|
|
vertexPositions.push_back(pol->m_vertices[v]);
|
|
|
|
btVector3 norm = pol->m_vertices[v];
|
|
|
|
norm.safeNormalize();
|
|
|
|
vertexNormals.push_back(norm);
|
|
|
|
}
|
|
|
|
for (int f = 0; f < pol->m_faces.size(); f++)
|
|
|
|
{
|
|
|
|
for (int ii = 2; ii < pol->m_faces[f].m_indices.size(); ii++)
|
|
|
|
{
|
2020-03-17 06:32:26 +00:00
|
|
|
indicesOut.push_back(baseIndex+pol->m_faces[f].m_indices[0]);
|
|
|
|
indicesOut.push_back(baseIndex + pol->m_faces[f].m_indices[ii - 1]);
|
|
|
|
indicesOut.push_back(baseIndex + pol->m_faces[f].m_indices[ii]);
|
2018-11-13 05:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 21:41:58 +00:00
|
|
|
}
|
2018-11-13 05:46:11 +00:00
|
|
|
else
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
2018-11-13 05:46:11 +00:00
|
|
|
btShapeHull* hull = new btShapeHull(convex);
|
|
|
|
hull->buildHull(0.0, 1);
|
|
|
|
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
2018-11-13 05:46:11 +00:00
|
|
|
//int strideInBytes = 9*sizeof(float);
|
|
|
|
//int numVertices = hull->numVertices();
|
|
|
|
//int numIndices =hull->numIndices();
|
|
|
|
|
|
|
|
for (int t = 0; t < hull->numTriangles(); t++)
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
2018-11-13 05:46:11 +00:00
|
|
|
btVector3 triNormal;
|
|
|
|
|
|
|
|
int index0 = hull->getIndexPointer()[t * 3 + 0];
|
|
|
|
int index1 = hull->getIndexPointer()[t * 3 + 1];
|
|
|
|
int index2 = hull->getIndexPointer()[t * 3 + 2];
|
|
|
|
btVector3 pos0 = parentTransform * hull->getVertexPointer()[index0];
|
|
|
|
btVector3 pos1 = parentTransform * hull->getVertexPointer()[index1];
|
|
|
|
btVector3 pos2 = parentTransform * hull->getVertexPointer()[index2];
|
|
|
|
triNormal = (pos1 - pos0).cross(pos2 - pos0);
|
|
|
|
triNormal.safeNormalize();
|
|
|
|
|
|
|
|
for (int v = 0; v < 3; v++)
|
|
|
|
{
|
|
|
|
int index = hull->getIndexPointer()[t * 3 + v];
|
|
|
|
btVector3 pos = parentTransform * hull->getVertexPointer()[index];
|
|
|
|
indicesOut.push_back(vertexPositions.size());
|
|
|
|
vertexPositions.push_back(pos);
|
|
|
|
vertexNormals.push_back(triNormal);
|
|
|
|
}
|
2016-05-13 06:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-13 05:46:11 +00:00
|
|
|
delete hull;
|
2016-05-13 06:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
|
|
|
if (collisionShape->isCompound())
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
btCompoundShape* compound = (btCompoundShape*)collisionShape;
|
|
|
|
for (int i = 0; i < compound->getNumChildShapes(); i++)
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
|
|
|
btTransform childWorldTrans = parentTransform * compound->getChildTransform(i);
|
2018-09-23 21:17:31 +00:00
|
|
|
CollisionShape2TriangleMesh(compound->getChildShape(i), childWorldTrans, vertexPositions, vertexNormals, indicesOut);
|
2016-05-13 06:03:12 +00:00
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2016-05-13 06:03:12 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (collisionShape->getShapeType() == SDF_SHAPE_PROXYTYPE)
|
2018-04-09 04:10:03 +00:00
|
|
|
{
|
|
|
|
//not yet
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2018-04-09 04:10:03 +00:00
|
|
|
{
|
|
|
|
btAssert(0);
|
|
|
|
}
|
2016-05-13 06:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|