expose debugDraw in gl3

This commit is contained in:
Erwin Coumans
2014-07-29 14:03:15 -07:00
parent cac50c1a8e
commit fb01827aee
9 changed files with 170 additions and 62 deletions

View File

@@ -21,6 +21,9 @@ struct GraphicsPhysicsBridge
virtual void syncPhysicsToGraphics(const btDiscreteDynamicsWorld* rbWorld)
{
}
virtual void createPhysicsDebugDrawer( btDiscreteDynamicsWorld* rbWorld)
{
}
};
struct CommonPhysicsSetup
@@ -35,6 +38,8 @@ public:
virtual void stepSimulation(float deltaTime)=0;
virtual void debugDraw()=0;
virtual bool pickBody(const btVector3& rayFromWorld, const btVector3& rayToWorld) = 0;
virtual bool movePickedBody(const btVector3& rayFromWorld, const btVector3& rayToWorld)=0;
virtual void removePickingConstraint() = 0;

View File

@@ -117,6 +117,15 @@ struct CommonRigidBodySetup : public CommonPhysicsSetup
}
}
virtual void debugDraw()
{
if (m_dynamicsWorld)
{
m_dynamicsWorld->debugDrawWorld();
}
}
virtual bool pickBody(const btVector3& rayFromWorld, const btVector3& rayToWorld)
{
if (m_dynamicsWorld==0)
@@ -197,6 +206,7 @@ struct CommonRigidBodySetup : public CommonPhysicsSetup
btBoxShape* createBoxShape(const btVector3& halfExtents)
{
btBoxShape* box = new btBoxShape(halfExtents);
m_collisionShapes.push_back(box);
return box;
}

View File

@@ -17,6 +17,7 @@ public:
virtual void exitPhysics()=0;
virtual void stepSimulation(float deltaTime)=0;
virtual void renderScene()=0;
virtual void physicsDebugDraw()=0;
virtual bool mouseMoveCallback(float x,float y)=0;
virtual bool mouseButtonCallback(int button, int state, float x, float y)=0;
virtual bool keyboardCallback(int key, int state)=0;
@@ -43,6 +44,9 @@ public:
virtual void renderScene()
{
}
virtual void physicsDebugDraw()
{
}
virtual bool mouseMoveCallback(float x,float y)
{
return false;

View File

@@ -429,6 +429,7 @@ int main(int argc, char* argv[])
prevTimeInMicroseconds = curTimeInMicroseconds;
}
sCurrentDemo->renderScene();
sCurrentDemo->physicsDebugDraw();
}
static int toggle = 1;

View File

@@ -2,6 +2,7 @@
#include "btBulletDynamicsCommon.h"
#include "OpenGLWindow/SimpleOpenGL3App.h"
#include "BulletCollision/CollisionShapes/btShapeHull.h"//to create a tesselation of a generic btConvexShape
#include "MyDebugDrawer.h"
struct GraphicsVertex
{
float pos[4];
@@ -13,9 +14,10 @@ struct GraphicsVertex
struct MyGraphicsPhysicsBridge : public GraphicsPhysicsBridge
{
SimpleOpenGL3App* m_glApp;
MyDebugDrawer* m_debugDraw;
MyGraphicsPhysicsBridge(SimpleOpenGL3App* glApp)
:m_glApp(glApp)
:m_glApp(glApp), m_debugDraw(0)
{
}
virtual void createRigidBodyGraphicsObject(btRigidBody* body, const btVector3& color)
@@ -52,9 +54,9 @@ struct MyGraphicsPhysicsBridge : public GraphicsPhysicsBridge
hull->buildHull(0.0);
{
int strideInBytes = 9*sizeof(float);
int numVertices = hull->numVertices();
int numIndices =hull->numIndices();
//int strideInBytes = 9*sizeof(float);
//int numVertices = hull->numVertices();
//int numIndices =hull->numIndices();
btAlignedObjectArray<GraphicsVertex> gvertices;
btAlignedObjectArray<int> indices;
@@ -86,7 +88,7 @@ struct MyGraphicsPhysicsBridge : public GraphicsPhysicsBridge
vtx.normal[0] =triNormal.x();
vtx.normal[1] =triNormal.y();
vtx.normal[2] =triNormal.z();
vtx.normal[3] =0;
vtx.texcoord[0] = 0.5f;
vtx.texcoord[1] = 0.5f;
@@ -123,13 +125,28 @@ struct MyGraphicsPhysicsBridge : public GraphicsPhysicsBridge
}
m_glApp->m_instancingRenderer->writeTransforms();
}
virtual void createPhysicsDebugDrawer(btDiscreteDynamicsWorld* rbWorld)
{
btAssert(rbWorld);
m_debugDraw = new MyDebugDrawer(m_glApp);
rbWorld->setDebugDrawer(m_debugDraw );
m_debugDraw->setDebugMode(
btIDebugDraw::DBG_DrawWireframe
+btIDebugDraw::DBG_DrawAabb
//btIDebugDraw::DBG_DrawContactPoints
);
}
};
Bullet2RigidBodyDemo::Bullet2RigidBodyDemo(SimpleOpenGL3App* app, CommonPhysicsSetup* physicsSetup)
:m_glApp(app),
m_physicsSetup(physicsSetup),
: m_physicsSetup(physicsSetup),
m_controlPressed(false),
m_altPressed(false)
m_altPressed(false),
m_glApp(app)
{
}
@@ -164,6 +181,12 @@ void Bullet2RigidBodyDemo::renderScene()
m_glApp->m_instancingRenderer->renderScene();
}
void Bullet2RigidBodyDemo::physicsDebugDraw()
{
m_physicsSetup->debugDraw();
}
Bullet2RigidBodyDemo::~Bullet2RigidBodyDemo()
{
}
@@ -251,7 +274,8 @@ bool Bullet2RigidBodyDemo::mouseButtonCallback(int button, int state, float x, f
btVector3 rayFrom = camPos;
btVector3 rayTo = getRayTo(x,y);
bool hasPicked = m_physicsSetup->pickBody(rayFrom, rayTo);
m_physicsSetup->pickBody(rayFrom, rayTo);
}

View File

@@ -3,10 +3,10 @@
#include "LinearMath/btVector3.h"
#include "../../AllBullet2Demos/BulletDemoInterface.h"
#include "BulletDemoInterface.h"
#include "OpenGLWindow/b3gWindowInterface.h"
#include "../../../Demos/CommonPhysicsSetup.h"
#include "../../Demos/CommonPhysicsSetup.h"
class Bullet2RigidBodyDemo : public BulletDemoInterface
@@ -26,6 +26,7 @@ public:
virtual void initPhysics();
virtual void exitPhysics();
virtual void renderScene();
virtual void physicsDebugDraw();
virtual void stepSimulation(float dt);

View File

@@ -2,25 +2,60 @@
#define MY_DEBUG_DRAWER_H
#include "LinearMath/btIDebugDraw.h"
#include "LinearMath/btAlignedObjectArray.h"
#define BT_LINE_BATCH_SIZE 512
struct MyDebugVec3
{
MyDebugVec3(const btVector3 org)
:x(org.x()),
y(org.y()),
z(org.z())
{
}
float x;
float y;
float z;
};
class MyDebugDrawer : public btIDebugDraw
{
SimpleOpenGL3App* m_glApp;
int m_debugMode;
btAlignedObjectArray<MyDebugVec3> m_linePoints;
btAlignedObjectArray<unsigned int> m_lineIndices;
btVector3 m_currentLineColor;
public:
MyDebugDrawer(SimpleOpenGL3App* app)
: m_glApp(app)
,m_debugMode(btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb)
,m_debugMode(btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb),
m_currentLineColor(-1,-1,-1)
{
}
virtual void drawLine(const btVector3& from1,const btVector3& to1,const btVector3& color1)
{
float from[4] = {from1[0],from1[1],from1[2],from1[3]};
float to[4] = {to1[0],to1[1],to1[2],to1[3]};
float color[4] = {color1[0],color1[1],color1[2],color1[3]};
m_glApp->m_instancingRenderer->drawLine(from,to,color);
//float from[4] = {from1[0],from1[1],from1[2],from1[3]};
//float to[4] = {to1[0],to1[1],to1[2],to1[3]};
//float color[4] = {color1[0],color1[1],color1[2],color1[3]};
//m_glApp->m_instancingRenderer->drawLine(from,to,color);
if (m_currentLineColor!=color1 || m_linePoints.size() >= BT_LINE_BATCH_SIZE)
{
flushLines();
m_currentLineColor = color1;
}
MyDebugVec3 from(from1);
MyDebugVec3 to(to1);
m_linePoints.push_back(from);
m_linePoints.push_back(to);
m_lineIndices.push_back(m_lineIndices.size());
m_lineIndices.push_back(m_lineIndices.size());
}
virtual void drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color)
@@ -45,6 +80,25 @@ public:
return m_debugMode;
}
virtual void flushLines()
{
int sz = m_linePoints.size();
if (sz)
{
float debugColor[4];
debugColor[0] = m_currentLineColor.x();
debugColor[1] = m_currentLineColor.y();
debugColor[2] = m_currentLineColor.z();
debugColor[3] = 1.f;
m_glApp->m_instancingRenderer->drawLines(&m_linePoints[0].x,debugColor,
m_linePoints.size(),sizeof(MyDebugVec3),
&m_lineIndices[0],
m_lineIndices.size(),
1);
m_linePoints.clear();
m_lineIndices.clear();
}
}
};

View File

@@ -607,6 +607,13 @@ void FeatherstoneDemo1::renderScene()
m_glApp->m_instancingRenderer->renderScene();
}
void FeatherstoneDemo1::physicsDebugDraw()
{
if (m_dynamicsWorld)
{
m_dynamicsWorld->debugDrawWorld();
}
}
void FeatherstoneDemo1::stepSimulation(float deltaTime)
{

View File

@@ -89,6 +89,8 @@ public:
virtual void initPhysics();
virtual void exitPhysics();
virtual void renderScene();
virtual void physicsDebugDraw();
virtual void stepSimulation(float deltaTime);
};