made shape construction virtual, to allow destruction of memory of btCollisionShape

This commit is contained in:
erwin.coumans
2008-10-04 01:11:40 +00:00
parent 23b2767100
commit b281057337
2 changed files with 471 additions and 320 deletions

View File

@@ -93,6 +93,11 @@ int btRigidBodyColladaInfo::getUid()
int btRigidConstraintColladaInfo::getUid() int btRigidConstraintColladaInfo::getUid()
{ {
if (m_typedConstraint->getUid()==-1) if (m_typedConstraint->getUid()==-1)
@@ -2624,6 +2629,110 @@ char* ColladaConverter::fixFileName(const char* lpCmdLine)
return m_cleaned_filename; return m_cleaned_filename;
} }
btCollisionShape* ColladaConverter::createPlaneShape(const btVector3& planeNormal,btScalar planeConstant)
{
btCollisionShape* planeShape = new btStaticPlaneShape(planeNormal,planeConstant);
m_allocatedCollisionShapes.push_back(planeShape);
return planeShape;
}
btCollisionShape* ColladaConverter::createBoxShape(const btVector3& halfExtents)
{
btCollisionShape* shape = new btBoxShape(halfExtents);
m_allocatedCollisionShapes.push_back(shape);
return shape;
}
btCollisionShape* ColladaConverter::createSphereShape(btScalar radius)
{
btCollisionShape* shape = new btSphereShape(radius);
m_allocatedCollisionShapes.push_back(shape);
return shape;
}
btCompoundShape* ColladaConverter::createCompoundShape()
{
btCompoundShape* shape = new btCompoundShape();
m_allocatedCollisionShapes.push_back(shape);
return shape;
}
btCollisionShape* ColladaConverter::createCylinderShapeY(btScalar radius,btScalar height)
{
btCollisionShape* shape = new btCylinderShape(btVector3(radius,height,radius));
m_allocatedCollisionShapes.push_back(shape);
return shape;
}
btTriangleMesh* ColladaConverter::createTriangleMeshContainer()
{
btTriangleMesh* meshContainer = new btTriangleMesh(m_use32bitIndices,m_use4componentVertices);
return meshContainer;
}
btConvexHullShape* ColladaConverter::createConvexHullShape()
{
btConvexHullShape* shape = new btConvexHullShape();
m_allocatedCollisionShapes.push_back(shape);
return shape;
}
btCollisionShape* ColladaConverter::createBvhTriangleMeshShape(btTriangleMesh* trimesh)
{
bool useQuantizedAabbCompression = true;
btCollisionShape* shape = new btBvhTriangleMeshShape(trimesh,useQuantizedAabbCompression);
m_allocatedCollisionShapes.push_back(shape);
return shape;
}
btCollisionShape* ColladaConverter::createConvexTriangleMeshShape(btTriangleMesh* trimesh)
{
btCollisionShape* shape = new btConvexTriangleMeshShape(trimesh);
m_allocatedCollisionShapes.push_back(shape);
return shape;
}
int ColladaConverter::getNumCollisionShapes() const
{
return m_allocatedCollisionShapes.size();
}
btCollisionShape* ColladaConverter::getCollisionShape(int shapeIndex)
{
return m_allocatedCollisionShapes[shapeIndex];
}
void ColladaConverter::deleteAllocatedCollisionShapes()
{
int i;
for (int i=0;i<m_allocatedCollisionShapes.size();i++)
{
delete m_allocatedCollisionShapes[i];
}
m_allocatedCollisionShapes.clear();
}
void ColladaConverter::deleteShape(btCollisionShape* shape)
{
for (int i=0;i<m_allocatedCollisionShapes.size();i++)
{
if (m_allocatedCollisionShapes[i]==shape)
{
delete m_allocatedCollisionShapes[i];
m_allocatedCollisionShapes.swap( i,m_allocatedCollisionShapes.size()-1);
m_allocatedCollisionShapes.pop_back();
break;
}
}
}
void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBodyOutput& rbOutput) void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBodyOutput& rbOutput)
{ {
@@ -2695,6 +2804,20 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
{ {
domRigid_body::domTechnique_common::domShapeRef shapeRef = techniqueRef->getShape_array()[s]; domRigid_body::domTechnique_common::domShapeRef shapeRef = techniqueRef->getShape_array()[s];
rbOutput.m_colShape = 0;
//future shape instancing
//rbOutput.m_colShape = findShapeForShapeRef(&shapeRef)
if (rbOutput.m_colShape)
{
} else
{
if (shapeRef->getPlane()) if (shapeRef->getPlane())
{ {
domPlaneRef planeRef = shapeRef->getPlane(); domPlaneRef planeRef = shapeRef->getPlane();
@@ -2703,7 +2826,7 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
const domFloat4 planeEq = planeRef->getEquation()->getValue(); const domFloat4 planeEq = planeRef->getEquation()->getValue();
btVector3 planeNormal(planeEq.get(0),planeEq.get(1),planeEq.get(2)); btVector3 planeNormal(planeEq.get(0),planeEq.get(1),planeEq.get(2));
btScalar planeConstant = planeEq.get(3)*m_unitMeterScaling; btScalar planeConstant = planeEq.get(3)*m_unitMeterScaling;
rbOutput.m_colShape = new btStaticPlaneShape(planeNormal,planeConstant); rbOutput.m_colShape = createPlaneShape(planeNormal,planeConstant);
} }
} }
@@ -2716,14 +2839,14 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
float x = halfExtents.get(0)*m_unitMeterScaling; float x = halfExtents.get(0)*m_unitMeterScaling;
float y = halfExtents.get(1)*m_unitMeterScaling; float y = halfExtents.get(1)*m_unitMeterScaling;
float z = halfExtents.get(2)*m_unitMeterScaling; float z = halfExtents.get(2)*m_unitMeterScaling;
rbOutput.m_colShape = new btBoxShape(btVector3(x,y,z)); rbOutput.m_colShape = createBoxShape(btVector3(x,y,z));
} }
if (shapeRef->getSphere()) if (shapeRef->getSphere())
{ {
domSphereRef sphereRef = shapeRef->getSphere(); domSphereRef sphereRef = shapeRef->getSphere();
domSphere::domRadiusRef radiusRef = sphereRef->getRadius(); domSphere::domRadiusRef radiusRef = sphereRef->getRadius();
domFloat radius = radiusRef->getValue()*m_unitMeterScaling; domFloat radius = radiusRef->getValue()*m_unitMeterScaling;
rbOutput.m_colShape = new btSphereShape(radius); rbOutput.m_colShape = createSphereShape(radius);
} }
if (shapeRef->getCylinder()) if (shapeRef->getCylinder())
@@ -2734,8 +2857,7 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
domFloat radius0 = radius2.get(0)*m_unitMeterScaling; domFloat radius0 = radius2.get(0)*m_unitMeterScaling;
//Cylinder around the local Y axis //Cylinder around the local Y axis
rbOutput.m_colShape = new btCylinderShape(btVector3(radius0,height,radius0)); rbOutput.m_colShape = createCylinderShapeY(radius0,height);
} }
if (shapeRef->getInstance_geometry()) if (shapeRef->getInstance_geometry())
@@ -2753,7 +2875,7 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
if (meshRef->getTriangles_array().getCount()) if (meshRef->getTriangles_array().getCount())
{ {
btTriangleMesh* trimesh = new btTriangleMesh(m_use32bitIndices,m_use4componentVertices); btTriangleMesh* trimesh = createTriangleMeshContainer();
for (unsigned int tg = 0;tg<meshRef->getTriangles_array().getCount();tg++) for (unsigned int tg = 0;tg<meshRef->getTriangles_array().getCount();tg++)
@@ -2839,12 +2961,13 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
if (rbOutput.m_isDynamics) if (rbOutput.m_isDynamics)
{ {
printf("moving concave <mesh> not supported, transformed into convex\n"); printf("moving concave <mesh> not supported, transformed into convex\n");
rbOutput.m_colShape = new btConvexTriangleMeshShape(trimesh); rbOutput.m_colShape = createConvexTriangleMeshShape(trimesh);
} else } else
{ {
printf("static concave triangle <mesh> added\n"); printf("static concave triangle <mesh> added\n");
bool useQuantizedAabbCompression = true;
rbOutput.m_colShape = new btBvhTriangleMeshShape(trimesh,useQuantizedAabbCompression); rbOutput.m_colShape = createBvhTriangleMeshShape(trimesh);
//rbOutput.m_colShape = new btBvhTriangleMeshShape(trimesh); //rbOutput.m_colShape = new btBvhTriangleMeshShape(trimesh);
//rbOutput.m_colShape = new btConvexTriangleMeshShape(trimesh); //rbOutput.m_colShape = new btConvexTriangleMeshShape(trimesh);
@@ -2855,7 +2978,7 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
} else } else
{ {
btConvexHullShape* convexHull = new btConvexHullShape(); btConvexHullShape* convexHull = createConvexHullShape();
int numAddedVerts = 0; int numAddedVerts = 0;
const domVerticesRef vertsRef = meshRef->getVertices(); const domVerticesRef vertsRef = meshRef->getVertices();
@@ -2895,7 +3018,7 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
//rbOutput.m_colShape->setTypedUserInfo (new btShapeColladaInfo (geom)); //rbOutput.m_colShape->setTypedUserInfo (new btShapeColladaInfo (geom));
} else } else
{ {
delete convexHull; deleteShape( convexHull);
printf("no vertices found for convex hull\n"); printf("no vertices found for convex hull\n");
} }
@@ -2924,7 +3047,7 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
btConvexHullShape* convexHullShape = new btConvexHullShape(0,0); btConvexHullShape* convexHullShape = createConvexHullShape();
//it is quite a trick to get to the vertices, using Collada. //it is quite a trick to get to the vertices, using Collada.
//we are not there yet... //we are not there yet...
@@ -3044,7 +3167,7 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
printf("created convexHullShape with %i points\n",convexHullShape->getNumVertices()); printf("created convexHullShape with %i points\n",convexHullShape->getNumVertices());
} else } else
{ {
delete convexHullShape; deleteShape( convexHullShape);
printf("failed to create convexHullShape\n"); printf("failed to create convexHullShape\n");
} }
@@ -3070,7 +3193,7 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
if (!rbOutput.m_compoundShape) if (!rbOutput.m_compoundShape)
{ {
rbOutput.m_compoundShape = new btCompoundShape(); rbOutput.m_compoundShape = createCompoundShape();
} }
btTransform localTransform; btTransform localTransform;
@@ -3090,6 +3213,12 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
} }
} }
}
//for future shape instancing
//storeShapeRef(shapeRef,rbOutput.m_colShape);
}//for each shape }//for each shape
} }

View File

@@ -50,6 +50,8 @@ public:
}; };
class btRigidConstraintColladaInfo class btRigidConstraintColladaInfo
{ {
public: public:
@@ -78,10 +80,14 @@ class ColladaConverter
{ {
char m_cleaned_filename[513]; char m_cleaned_filename[513];
protected: protected:
btDynamicsWorld* m_dynamicsWorld; btDynamicsWorld* m_dynamicsWorld;
btAlignedObjectArray<class btCollisionShape*> m_allocatedCollisionShapes;
btHashMap<btHashKeyPtr<btRigidBodyColladaInfo*>,btRigidBodyColladaInfo*> m_rbUserInfoHashMap; btHashMap<btHashKeyPtr<btRigidBodyColladaInfo*>,btRigidBodyColladaInfo*> m_rbUserInfoHashMap;
btHashMap<btHashKeyPtr<btRigidConstraintColladaInfo*>,btRigidConstraintColladaInfo*> m_constraintUserInfoHashMap; btHashMap<btHashKeyPtr<btRigidConstraintColladaInfo*>,btRigidConstraintColladaInfo*> m_constraintUserInfoHashMap;
@@ -213,6 +219,22 @@ public:
{ {
}; };
virtual btCollisionShape* createPlaneShape(const btVector3& planeNormal,btScalar planeConstant);
virtual btCollisionShape* createBoxShape(const btVector3& halfExtents);
virtual btCollisionShape* createSphereShape(btScalar radius);
virtual btCollisionShape* createCylinderShapeY(btScalar radius,btScalar height);
virtual class btTriangleMesh* createTriangleMeshContainer();
virtual btCollisionShape* createBvhTriangleMeshShape(btTriangleMesh* trimesh);
virtual btCollisionShape* createConvexTriangleMeshShape(btTriangleMesh* trimesh);
virtual class btConvexHullShape* createConvexHullShape();
virtual class btCompoundShape* createCompoundShape();
int getNumCollisionShapes() const;
btCollisionShape* getCollisionShape(int shapeIndex);
void deleteAllocatedCollisionShapes();
void deleteShape(btCollisionShape* shape);
char* getLastFileName(); char* getLastFileName();
char* fixFileName(const char* lpCmdLine); char* fixFileName(const char* lpCmdLine);