blender/source/gameengine/Physics/Bullet/CcdGraphicController.cpp
Benoit Bolsee 51b4145841 BGE Scenegraph and View frustrum culling improvement.
This commit contains a number of performance improvements for the
BGE in the Scenegraph (parent relation between objects in the
scene) and view frustrum culling.

The scenegraph improvement consists in avoiding position update
if the object has not moved since last update and the removal
of redundant updates and synchronization with the physics engine.

The view frustrum culling improvement consists in using the DBVT
broadphase facility of Bullet to build a tree of graphical objects
in the scene. The elements of the tree are Aabb boxes (Aligned 
Axis Bounding Boxes) enclosing the objects. This provides good
precision in closed and opened scenes. This new culling system
is enabled by default but just in case, it can be disabled with
a button in the World settings. There is no do_version in this
commit but it will be added before the 2.49 release. For now you
must manually enable the DBVT culling option in World settings
when you open an old file.

The above improvements speed up scenegraph and culling up to 5x.
However, this performance improvement is only visible when
you have hundreds or thousands of objects.

The main interest of the DBVT tree is to allow easy occlusion
culling and automatic LOD system. This will be the object of further
improvements.
2009-04-07 22:14:06 +00:00

113 lines
3.8 KiB
C++

/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "CcdPhysicsEnvironment.h"
#include "CcdGraphicController.h"
#include "btBulletDynamicsCommon.h"
#include "MT_Point3.h"
CcdGraphicController::CcdGraphicController (CcdPhysicsEnvironment* phyEnv, PHY_IMotionState* motionState) :
m_localAabbMin(0.f, 0.f, 0.f),
m_localAabbMax(0.f, 0.f, 0.f),
m_motionState(motionState),
m_phyEnv(phyEnv),
m_handle(NULL),
m_newClientInfo(NULL)
{
}
CcdGraphicController::~CcdGraphicController()
{
if (m_phyEnv)
m_phyEnv->removeCcdGraphicController(this);
if (m_motionState)
delete m_motionState;
}
void CcdGraphicController::setLocalAabb(const btVector3& aabbMin,const btVector3& aabbMax)
{
m_localAabbMin = aabbMin;
m_localAabbMax = aabbMax;
SetGraphicTransform();
}
void CcdGraphicController::setLocalAabb(const MT_Point3& aabbMin,const MT_Point3& aabbMax)
{
m_localAabbMin = btVector3(aabbMin[0],aabbMin[1],aabbMin[2]);
m_localAabbMax = btVector3(aabbMax[0],aabbMax[1],aabbMax[2]);
SetGraphicTransform();
}
void CcdGraphicController::getAabb(btVector3& aabbMin, btVector3& aabbMax)
{
btVector3 pos;
btVector3 scale;
float ori[12];
m_motionState->getWorldPosition(pos.m_floats[0],pos.m_floats[1],pos.m_floats[2]);
m_motionState->getWorldScaling(scale.m_floats[0],scale.m_floats[1],scale.m_floats[2]);
m_motionState->getWorldOrientation(ori);
btMatrix3x3 rot(ori[0], ori[4], ori[8],
ori[1], ori[5], ori[9],
ori[2], ori[6], ori[10]);
btVector3 localAabbMin = m_localAabbMin;
btVector3 localAabbMax = m_localAabbMax;
btVector3 tmpAabbMin = m_localAabbMin * scale;
btVector3 tmpAabbMax = m_localAabbMax * scale;
localAabbMin[0] = (scale.getX() >= 0.) ? tmpAabbMin[0] : tmpAabbMax[0];
localAabbMin[1] = (scale.getY() >= 0.) ? tmpAabbMin[1] : tmpAabbMax[1];
localAabbMin[2] = (scale.getZ() >= 0.) ? tmpAabbMin[2] : tmpAabbMax[2];
localAabbMax[0] = (scale.getX() <= 0.) ? tmpAabbMin[0] : tmpAabbMax[0];
localAabbMax[1] = (scale.getY() <= 0.) ? tmpAabbMin[1] : tmpAabbMax[1];
localAabbMax[2] = (scale.getZ() <= 0.) ? tmpAabbMin[2] : tmpAabbMax[2];
btVector3 localHalfExtents = btScalar(0.5)*(localAabbMax-localAabbMin);
btVector3 localCenter = btScalar(0.5)*(localAabbMax+localAabbMin);
btMatrix3x3 abs_b = rot.absolute();
btVector3 center = rot*localCenter + pos;
btVector3 extent = abs_b*localHalfExtents;
aabbMin = center - extent;
aabbMax = center + extent;
}
bool CcdGraphicController::SetGraphicTransform()
{
if (!m_handle)
return false;
btVector3 aabbMin;
btVector3 aabbMax;
getAabb(aabbMin, aabbMax);
// update Aabb in broadphase
m_phyEnv->getCullingTree()->setAabb(m_handle,aabbMin,aabbMax,NULL);
return true;
}
PHY_IGraphicController* CcdGraphicController::GetReplica(class PHY_IMotionState* motionState)
{
CcdGraphicController* replica = new CcdGraphicController(*this);
replica->m_motionState = motionState;
replica->m_newClientInfo = NULL;
replica->m_handle = NULL;
m_phyEnv->addCcdGraphicController(replica);
return replica;
}