BGE patch: use new btScaledBvhTriangleMeshShape to allow shape sharing between replicas and avoid BVH rebuild in case of scaling. This will save memory and speed up greatly the instantiation of static mesh.

This commit is contained in:
Benoit Bolsee 2008-09-13 16:03:11 +00:00
parent 63ecf9966e
commit 8925ae6042
3 changed files with 68 additions and 43 deletions

@ -15,7 +15,7 @@ subject to the following restrictions:
#include "CcdPhysicsController.h"
#include "btBulletDynamicsCommon.h"
#include "BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h"
#include "PHY_IMotionState.h"
#include "CcdPhysicsEnvironment.h"
#include "RAS_MeshObject.h"
@ -904,15 +904,26 @@ btCollisionShape* CcdShapeConstructionInfo::CreateBulletShape()
break;
case PHY_SHAPE_MESH:
// Let's use the latest btScaledBvhTriangleMeshShape: it allows true sharing of
// triangle mesh information between duplicates => drastic performance increase when
// duplicating complex mesh objects.
// BUT it causes a small performance decrease when sharing is not required:
// 9 multiplications/additions and one function call for each triangle that passes the mid phase filtering
// One possible optimization is to use directly the btBvhTriangleMeshShape when the scale is 1,1,1
// and btScaledBvhTriangleMeshShape otherwise.
if (!m_unscaledShape)
{
collisionMeshData = new btTriangleMesh();
// m_vertexArray is necessarily a multiple of 3
for (std::vector<btPoint3>::iterator it=m_vertexArray.begin(); it != m_vertexArray.end(); )
{
collisionMeshData->addTriangle(*it++,*it++,*it++);
}
concaveShape = new btBvhTriangleMeshShape( collisionMeshData, true );
concaveShape->recalcLocalAabb();
collisionShape = concaveShape;
// this shape will be shared and not deleted until shapeInfo is deleted
m_unscaledShape = new btBvhTriangleMeshShape( collisionMeshData, true );
m_unscaledShape->recalcLocalAabb();
}
collisionShape = new btScaledBvhTriangleMeshShape(m_unscaledShape, btVector3(1.0f,1.0f,1.0f));
break;
case PHY_SHAPE_COMPOUND:
@ -953,7 +964,10 @@ CcdShapeConstructionInfo::~CcdShapeConstructionInfo()
childShape->Release();
childShape = nextShape;
}
if (m_unscaledShape)
{
DeleteBulletShape(m_unscaledShape);
}
m_vertexArray.clear();
}

@ -49,6 +49,7 @@ public:
m_halfExtend(0.f,0.f,0.f),
m_childScale(1.0f,1.0f,1.0f),
m_nextShape(NULL),
m_unscaledShape(NULL),
m_refCount(1)
{
m_childTrans.setIdentity();
@ -72,6 +73,10 @@ public:
void AddShape(CcdShapeConstructionInfo* shapeInfo);
btTriangleMeshShape* GetMeshShape(void)
{
return m_unscaledShape;
}
CcdShapeConstructionInfo* GetNextShape()
{
return m_nextShape;
@ -108,6 +113,8 @@ public:
protected:
CcdShapeConstructionInfo* m_nextShape; // for compound shape
btBvhTriangleMeshShape* m_unscaledShape;// holds the shared unscale BVH mesh shape,
// the actual shape is of type btScaledBvhTriangleMeshShape
int m_refCount; // this class is shared between replicas
// keep track of users so that we can release it
};

@ -815,6 +815,10 @@ PHY_IPhysicsController* CcdPhysicsEnvironment::rayTest(PHY_IRayCastFilterCallbac
// Bullet returns the normal from "outside".
// If the user requests the real normal, compute it now
if (filterCallback.m_faceNormal)
{
// mesh shapes are shared and stored in the shapeInfo
btTriangleMeshShape* triangleShape = shapeInfo->GetMeshShape();
if (triangleShape)
{
// this code is copied from Bullet
btVector3 triangle[3];
@ -826,7 +830,6 @@ PHY_IPhysicsController* CcdPhysicsEnvironment::rayTest(PHY_IRayCastFilterCallbac
int indexstride;
int numfaces;
PHY_ScalarType indicestype;
btTriangleMeshShape* triangleShape = (btTriangleMeshShape*)shape;
btStridingMeshInterface* meshInterface = triangleShape->getMeshInterface();
meshInterface->getLockedReadOnlyVertexIndexBase(
@ -841,7 +844,7 @@ PHY_IPhysicsController* CcdPhysicsEnvironment::rayTest(PHY_IRayCastFilterCallbac
0);
unsigned int* gfxbase = (unsigned int*)(indexbase+rayCallback.m_hitTriangleIndex*indexstride);
const btVector3& meshScaling = meshInterface->getScaling();
const btVector3& meshScaling = shape->getLocalScaling();
for (int j=2;j>=0;j--)
{
int graphicsindex = indicestype==PHY_SHORT?((unsigned short*)gfxbase)[j]:gfxbase[j];
@ -858,6 +861,7 @@ PHY_IPhysicsController* CcdPhysicsEnvironment::rayTest(PHY_IRayCastFilterCallbac
}
}
}
}
if (rayCallback.m_hitNormalWorld.length2() > (SIMD_EPSILON*SIMD_EPSILON))
{
rayCallback.m_hitNormalWorld.normalize();