[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [26597] trunk/blender/source/gameengine: BGE: Optimize Soft body conversion: don't create BVH structure.
Benoit Bolsee
benoit.bolsee at online.be
Wed Feb 3 22:41:09 CET 2010
Revision: 26597
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=26597
Author: ben2610
Date: 2010-02-03 22:41:03 +0100 (Wed, 03 Feb 2010)
Log Message:
-----------
BGE: Optimize Soft body conversion: don't create BVH structure.
A btBvhTriangleMeshShape object is created when converting
a mesh to physics, also in case of Soft body although the
soft body will not use it (it only uses the mesh interface).
This patch keeps this system for compatibility with the
KX converter but avoids the creation of the BVH structure,
which consumes a lots of CPU. This should speed up
significantly the conversion of large mesh to softbody.
A secondary optimization is that the sharing of shapeInfo
is extended to rigid body using gImpact. Before it was
only active between static body and soft body.
Modified Paths:
--------------
trunk/blender/source/gameengine/Ketsji/KX_BulletPhysicsController.cpp
trunk/blender/source/gameengine/Ketsji/KX_ConvertPhysicsObjects.cpp
trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp
trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsController.h
Modified: trunk/blender/source/gameengine/Ketsji/KX_BulletPhysicsController.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_BulletPhysicsController.cpp 2010-02-03 21:33:22 UTC (rev 26596)
+++ trunk/blender/source/gameengine/Ketsji/KX_BulletPhysicsController.cpp 2010-02-03 21:41:03 UTC (rev 26597)
@@ -279,13 +279,15 @@
// we will need this to make sure that we remove the right proxy later when unparenting
proxyShapeInfo->m_userData = childCtrl;
proxyShapeInfo->SetProxy(childCtrl->GetShapeInfo()->AddRef());
- // add to parent compound shapeinfo
+ // add to parent compound shapeinfo (increments ref count)
GetShapeInfo()->AddShape(proxyShapeInfo);
// create new bullet collision shape from the object shapeinfo and set scaling
- btCollisionShape* newChildShape = proxyShapeInfo->CreateBulletShape(childCtrl->GetMargin());
+ btCollisionShape* newChildShape = proxyShapeInfo->CreateBulletShape(childCtrl->GetMargin(), childCtrl->getConstructionInfo().m_bGimpact, true);
newChildShape->setLocalScaling(relativeScale);
// add bullet collision shape to parent compound collision shape
compoundShape->addChildShape(proxyShapeInfo->m_childTrans,newChildShape);
+ // proxyShapeInfo is not needed anymore, release it
+ proxyShapeInfo->Release();
// remember we created this shape
childCtrl->m_bulletChildShape = newChildShape;
// recompute inertia of parent
Modified: trunk/blender/source/gameengine/Ketsji/KX_ConvertPhysicsObjects.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_ConvertPhysicsObjects.cpp 2010-02-03 21:33:22 UTC (rev 26596)
+++ trunk/blender/source/gameengine/Ketsji/KX_ConvertPhysicsObjects.cpp 2010-02-03 21:41:03 UTC (rev 26597)
@@ -97,6 +97,7 @@
bool isbulletdyna = false;
bool isbulletsensor = false;
+ bool useGimpact = false;
CcdConstructionInfo ci;
class PHY_IMotionState* motionstate = new KX_MotionState(gameobj->GetSGNode());
class CcdShapeConstructionInfo *shapeInfo = new CcdShapeConstructionInfo();
@@ -121,7 +122,8 @@
shapeInfo->m_radius = objprop->m_radius;
isbulletdyna = objprop->m_dyna;
isbulletsensor = objprop->m_sensor;
-
+ useGimpact = ((isbulletdyna || isbulletsensor) && !objprop->m_softbody);
+
ci.m_localInertiaTensor = btVector3(ci.m_mass/3.f,ci.m_mass/3.f,ci.m_mass/3.f);
btCollisionShape* bm = 0;
@@ -178,24 +180,22 @@
}
case KX_BOUNDPOLYTOPE:
{
- shapeInfo->SetMesh(meshobj, dm,true,false);
+ shapeInfo->SetMesh(meshobj, dm,true);
bm = shapeInfo->CreateBulletShape(ci.m_margin);
break;
}
case KX_BOUNDMESH:
{
- bool useGimpact = ((ci.m_mass || isbulletsensor) && !objprop->m_softbody);
-
// mesh shapes can be shared, check first if we already have a shape on that mesh
- class CcdShapeConstructionInfo *sharedShapeInfo = CcdShapeConstructionInfo::FindMesh(meshobj, dm, false,useGimpact);
+ class CcdShapeConstructionInfo *sharedShapeInfo = CcdShapeConstructionInfo::FindMesh(meshobj, dm, false);
if (sharedShapeInfo != NULL)
{
- delete shapeInfo;
+ shapeInfo->Release();
shapeInfo = sharedShapeInfo;
shapeInfo->AddRef();
} else
{
- shapeInfo->SetMesh(meshobj, dm, false,useGimpact);
+ shapeInfo->SetMesh(meshobj, dm, false);
}
// Soft bodies require welding. Only avoid remove doubles for non-soft bodies!
@@ -204,7 +204,7 @@
shapeInfo->setVertexWeldingThreshold1(objprop->m_soft_welding); //todo: expose this to the UI
}
- bm = shapeInfo->CreateBulletShape(ci.m_margin);
+ bm = shapeInfo->CreateBulletShape(ci.m_margin, useGimpact, !objprop->m_softbody);
//should we compute inertia for dynamic shape?
//bm->calculateLocalInertia(ci.m_mass,ci.m_localInertiaTensor);
@@ -218,7 +218,7 @@
if (!bm)
{
delete motionstate;
- delete shapeInfo;
+ shapeInfo->Release();
return;
}
@@ -268,6 +268,7 @@
compoundShape->calculateLocalInertia(mass,localInertia);
rigidbody->setMassProps(mass,localInertia);
}
+ shapeInfo->Release();
// delete motionstate as it's not used
delete motionstate;
return;
@@ -284,6 +285,7 @@
compoundShape->addChildShape(shapeInfo->m_childTrans,bm);
// now replace the shape
bm = compoundShape;
+ shapeInfo->Release();
shapeInfo = compoundShapeInfo;
}
@@ -395,6 +397,7 @@
ci.m_contactProcessingThreshold = objprop->m_contactProcessingThreshold;//todo: expose this in advanced settings, just like margin, default to 10000 or so
ci.m_bSoft = objprop->m_softbody;
ci.m_bSensor = isbulletsensor;
+ ci.m_bGimpact = useGimpact;
MT_Vector3 scaling = gameobj->NodeGetWorldScaling();
ci.m_scaling.setValue(scaling[0], scaling[1], scaling[2]);
KX_BulletPhysicsController* physicscontroller = new KX_BulletPhysicsController(ci,isbulletdyna,isbulletsensor,objprop->m_hasCompoundChildren);
@@ -544,7 +547,8 @@
shapeInfo->UpdateMesh(from_gameobj, from_meshobj);
/* create the new bullet mesh */
- btCollisionShape* bm= shapeInfo->CreateBulletShape(spc->getConstructionInfo().m_margin);
+ CcdConstructionInfo& cci = spc->getConstructionInfo();
+ btCollisionShape* bm= shapeInfo->CreateBulletShape(cci.m_margin, cci.m_bGimpact, !cci.m_bSoft);
spc->ReplaceControllerShape(bm);
return true;
Modified: trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp
===================================================================
--- trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp 2010-02-03 21:33:22 UTC (rev 26596)
+++ trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp 2010-02-03 21:41:03 UTC (rev 26597)
@@ -236,7 +236,7 @@
}
} else
{
- btBvhTriangleMeshShape* trimeshshape = (btBvhTriangleMeshShape*) m_cci.m_collisionShape;
+ btTriangleMeshShape* trimeshshape = (btTriangleMeshShape*) m_cci.m_collisionShape;
///only deal with meshes that have 1 sub part/component, for now
if (trimeshshape->getMeshInterface()->getNumSubParts()==1)
{
@@ -682,7 +682,7 @@
if (m_shapeInfo)
{
m_shapeInfo->AddRef();
- m_collisionShape = m_shapeInfo->CreateBulletShape(m_cci.m_margin);
+ m_collisionShape = m_shapeInfo->CreateBulletShape(m_cci.m_margin, m_cci.m_bGimpact, !m_cci.m_bSoft);
if (m_collisionShape)
{
@@ -1276,7 +1276,7 @@
if (m_shapeInfo)
{
// This situation does not normally happen
- cinfo.m_collisionShape = m_shapeInfo->CreateBulletShape(m_cci.m_margin);
+ cinfo.m_collisionShape = m_shapeInfo->CreateBulletShape(m_cci.m_margin, m_cci.m_bGimpact, !m_cci.m_bSoft);
}
else if (m_collisionShape)
{
@@ -1379,9 +1379,9 @@
// Shape constructor
std::map<RAS_MeshObject*, CcdShapeConstructionInfo*> CcdShapeConstructionInfo::m_meshShapeMap;
-CcdShapeConstructionInfo* CcdShapeConstructionInfo::FindMesh(RAS_MeshObject* mesh, struct DerivedMesh* dm, bool polytope, bool gimpact)
+CcdShapeConstructionInfo* CcdShapeConstructionInfo::FindMesh(RAS_MeshObject* mesh, struct DerivedMesh* dm, bool polytope)
{
- if (polytope || dm || gimpact)
+ if (polytope || dm)
// not yet supported
return NULL;
@@ -1391,12 +1391,10 @@
return NULL;
}
-bool CcdShapeConstructionInfo::SetMesh(RAS_MeshObject* meshobj, DerivedMesh* dm, bool polytope,bool useGimpact)
+bool CcdShapeConstructionInfo::SetMesh(RAS_MeshObject* meshobj, DerivedMesh* dm, bool polytope)
{
int numpolys, numverts;
- m_useGimpact = useGimpact;
-
// assume no shape information
// no support for dynamic change of shape yet
assert(IsUnused());
@@ -1656,7 +1654,7 @@
}
// sharing only on static mesh at present, if you change that, you must also change in FindMesh
- if (!polytope && !dm && !useGimpact)
+ if (!polytope && !dm)
{
// triangle shape can be shared, store the mesh object in the map
m_meshShapeMap.insert(std::pair<RAS_MeshObject*,CcdShapeConstructionInfo*>(meshobj,this));
@@ -1991,13 +1989,13 @@
return true;
}
-btCollisionShape* CcdShapeConstructionInfo::CreateBulletShape(btScalar margin)
+btCollisionShape* CcdShapeConstructionInfo::CreateBulletShape(btScalar margin, bool useGimpact, bool useBvh)
{
btCollisionShape* collisionShape = 0;
btCompoundShape* compoundShape = 0;
if (m_shapeType == PHY_SHAPE_PROXY && m_shapeProxy != NULL)
- return m_shapeProxy->CreateBulletShape(margin);
+ return m_shapeProxy->CreateBulletShape(margin, useGimpact, useBvh);
switch (m_shapeType)
{
@@ -2037,7 +2035,7 @@
// 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_useGimpact)
+ if (useGimpact)
{
btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(
m_polygonIndexArray.size(),
@@ -2095,14 +2093,14 @@
if(m_unscaledShape) {
DeleteBulletShape(m_unscaledShape, false);
m_unscaledShape->~btBvhTriangleMeshShape();
-
- m_unscaledShape = new(m_unscaledShape) btBvhTriangleMeshShape( indexVertexArrays, true );
+ m_unscaledShape = new(m_unscaledShape) btBvhTriangleMeshShape( indexVertexArrays, true, useBvh );
} else {
- m_unscaledShape = new btBvhTriangleMeshShape( indexVertexArrays, true );
+ m_unscaledShape = new btBvhTriangleMeshShape( indexVertexArrays, true, useBvh );
}
-
m_forceReInstance= false;
- m_unscaledShape->recalcLocalAabb();
+ } else if (useBvh && m_unscaledShape->getOptimizedBvh() == NULL) {
+ // the existing unscaledShape was not build with Bvh, do it now
+ m_unscaledShape->buildOptimizedBvh();
}
collisionShape = new btScaledBvhTriangleMeshShape(m_unscaledShape, btVector3(1.0f,1.0f,1.0f));
collisionShape->setMargin(margin);
@@ -2117,7 +2115,7 @@
sit != m_shapeArray.end();
sit++)
{
- collisionShape = (*sit)->CreateBulletShape(margin);
+ collisionShape = (*sit)->CreateBulletShape(margin, useGimpact, useBvh);
if (collisionShape)
{
collisionShape->setLocalScaling((*sit)->m_childScale);
@@ -2133,6 +2131,7 @@
void CcdShapeConstructionInfo::AddShape(CcdShapeConstructionInfo* shapeInfo)
{
m_shapeArray.push_back(shapeInfo);
+ shapeInfo->AddRef();
}
CcdShapeConstructionInfo::~CcdShapeConstructionInfo()
Modified: trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsController.h
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list