Skip to content

Commit

Permalink
UPBGE: Avoid explicit allocation on heap of physics debug drawer.
Browse files Browse the repository at this point in the history
  • Loading branch information
panzergame committed Aug 5, 2018
1 parent b53c8bf commit 99feb54
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 57 deletions.
90 changes: 36 additions & 54 deletions source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,18 +383,43 @@ class CcdOverlapFilterCallBack : public btOverlapFilterCallback
virtual bool needBroadphaseCollision(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1) const;
};


void CcdPhysicsEnvironment::SetDebugDrawer(btIDebugDraw *debugDrawer)
CcdDebugDraw::CcdDebugDraw()
:m_debugMode(0)
{
}
void CcdDebugDraw::drawLine(const btVector3& from, const btVector3& to, const btVector3& color)
{
if (debugDrawer && m_dynamicsWorld) {
m_dynamicsWorld->setDebugDrawer(debugDrawer);
if (m_debugMode > 0) {
KX_RasterizerDrawDebugLine(ToMt(from), ToMt(to), mt::vec4(color.x(), color.y(), color.z(), 1.0f));
}
m_debugDrawer = debugDrawer;
}

void CcdDebugDraw::reportErrorWarning(const char *warningString)
{
}

void CcdDebugDraw::drawContactPoint(const btVector3& PointOnB, const btVector3& normalOnB, float distance, int lifeTime, const btVector3& color)
{
drawLine(PointOnB, PointOnB + normalOnB, color);
drawSphere(PointOnB, 0.1f, color);
}

void CcdDebugDraw::setDebugMode(int debugMode)
{
m_debugMode = debugMode;
}

int CcdDebugDraw::getDebugMode() const
{
return m_debugMode;
}

void CcdDebugDraw::draw3dText(const btVector3& location, const char *textString)
{
}

CcdPhysicsEnvironment::CcdPhysicsEnvironment(PHY_SolverType solverType, bool useDbvtCulling)
:m_debugDrawer(nullptr),
m_collisionConfiguration(new btSoftBodyRigidBodyCollisionConfiguration()),
:m_collisionConfiguration(new btSoftBodyRigidBodyCollisionConfiguration()),
m_broadphase(new btDbvtBroadphase()),
m_cullingCache(nullptr),
m_cullingTree(nullptr),
Expand Down Expand Up @@ -441,6 +466,8 @@ CcdPhysicsEnvironment::CcdPhysicsEnvironment(PHY_SolverType solverType, bool use
m_dynamicsWorld.reset(new btSoftRigidDynamicsWorldMt(m_dispatcher.get(), m_broadphase.get(), m_solverPool.get(), m_solverMt.get(), m_collisionConfiguration.get()));
m_dynamicsWorld->setInternalTickCallback(&CcdPhysicsEnvironment::StaticSimulationSubtickCallback, this);

m_dynamicsWorld->setDebugDrawer(&m_debugDrawer);

SetGravity(0.0f, 0.0f, -9.81f);
}

Expand Down Expand Up @@ -924,17 +951,12 @@ void CcdPhysicsEnvironment::ProcessFhSprings(double curTime, float interval)

int CcdPhysicsEnvironment::GetDebugMode() const
{
if (m_debugDrawer) {
return m_debugDrawer->getDebugMode();
}
return 0;
return m_debugDrawer.getDebugMode();
}

void CcdPhysicsEnvironment::SetDebugMode(int debugMode)
{
if (m_debugDrawer) {
m_debugDrawer->setDebugMode(debugMode);
}
m_debugDrawer.setDebugMode(debugMode);
}

void CcdPhysicsEnvironment::SetNumIterations(int numIter)
Expand Down Expand Up @@ -2581,45 +2603,6 @@ void CcdPhysicsEnvironment::ExportFile(const std::string& filename)
}
}

struct BlenderDebugDraw : public btIDebugDraw {
BlenderDebugDraw()
:m_debugMode(0)
{
}

int m_debugMode;

virtual void drawLine(const btVector3& from, const btVector3& to, const btVector3& color)
{
if (m_debugMode > 0) {
KX_RasterizerDrawDebugLine(ToMt(from), ToMt(to), mt::vec4(color.x(), color.y(), color.z(), 1.0f));
}
}

virtual void reportErrorWarning(const char *warningString)
{
}

virtual void drawContactPoint(const btVector3& PointOnB, const btVector3& normalOnB, float distance, int lifeTime, const btVector3& color)
{
drawLine(PointOnB, PointOnB + normalOnB, color);
drawSphere(PointOnB, 0.1f, color);
}

virtual void setDebugMode(int debugMode)
{
m_debugMode = debugMode;
}
virtual int getDebugMode() const
{
return m_debugMode;
}
///todo: find out if Blender can do this
virtual void draw3dText(const btVector3& location, const char *textString)
{
}
};

CcdPhysicsEnvironment *CcdPhysicsEnvironment::Create(Scene *blenderscene, bool visualizePhysics)
{
static const PHY_SolverType solverTypeTable[] = {
Expand All @@ -2632,7 +2615,6 @@ CcdPhysicsEnvironment *CcdPhysicsEnvironment::Create(Scene *blenderscene, bool v
CcdPhysicsEnvironment *ccdPhysEnv = new CcdPhysicsEnvironment(solverTypeTable[blenderscene->gm.solverType],
(blenderscene->gm.mode & WO_DBVT_CULLING) != 0);

ccdPhysEnv->SetDebugDrawer(new BlenderDebugDraw());
ccdPhysEnv->SetDeactivationLinearTreshold(blenderscene->gm.lineardeactthreshold);
ccdPhysEnv->SetDeactivationAngularTreshold(blenderscene->gm.angulardeactthreshold);
ccdPhysEnv->SetDeactivationTime(blenderscene->gm.deactivationtime);
Expand Down
20 changes: 17 additions & 3 deletions source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ class btConstraintSolverPoolMt;
/// Find the id of the closest node to a point in a soft body.
int Ccd_FindClosestNode(btSoftBody *sb, const btVector3& worldPoint);

class CcdDebugDraw : public btIDebugDraw
{
private:
int m_debugMode;

public:
CcdDebugDraw();

virtual void drawLine(const btVector3& from, const btVector3& to, const btVector3& color);
virtual void reportErrorWarning(const char *warningString);
virtual void drawContactPoint(const btVector3& PointOnB, const btVector3& normalOnB, float distance, int lifeTime, const btVector3& color);
virtual void setDebugMode(int debugMode);
virtual int getDebugMode() const;
virtual void draw3dText(const btVector3& location, const char *textString);
};

/** CcdPhysicsEnvironment is an experimental mainloop for physics simulation using optional continuous collision detection.
* Physics Environment takes care of stepping the simulation and is a container for physics entities.
* It stores rigidbodies,constraints, materials etc.
Expand All @@ -80,7 +96,7 @@ class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment, public mt::SimdCla
protected:
btVector3 m_gravity;

btIDebugDraw *m_debugDrawer;
CcdDebugDraw m_debugDrawer;

std::unique_ptr<btDefaultCollisionConfiguration> m_collisionConfiguration;
/// broadphase for dynamic world
Expand Down Expand Up @@ -142,8 +158,6 @@ class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment, public mt::SimdCla

/// Perform an integration step of duration 'timeStep'.

virtual void SetDebugDrawer(btIDebugDraw *debugDrawer);

virtual void SetNumIterations(int numIter);
virtual void SetNumTimeSubSteps(int numTimeSubSteps)
{
Expand Down

0 comments on commit 99feb54

Please sign in to comment.