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
@@ -28,13 +31,15 @@ struct CommonPhysicsSetup
public:
virtual ~CommonPhysicsSetup() {}
virtual void initPhysics(GraphicsPhysicsBridge& gfxBridge) = 0;
virtual void exitPhysics()=0;
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;
@@ -42,7 +47,7 @@ public:
virtual void syncPhysicsToGraphics(GraphicsPhysicsBridge& gfxBridge) = 0;
virtual btRigidBody* createRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape, const btVector4& color=btVector4(1,0,0,1))=0;
virtual btBoxShape* createBoxShape(const btVector3& halfExtents)=0;
};

View File

@@ -15,14 +15,14 @@ struct CommonRigidBodySetup : public CommonPhysicsSetup
btConstraintSolver* m_solver;
btDefaultCollisionConfiguration* m_collisionConfiguration;
btDiscreteDynamicsWorld* m_dynamicsWorld;
//data for picking objects
class btRigidBody* m_pickedBody;
class btTypedConstraint* m_pickedConstraint;
btVector3 m_oldPickingPos;
btVector3 m_hitPos;
btScalar m_oldPickingDist;
CommonRigidBodySetup()
:m_broadphase(0),
m_dispatcher(0),
@@ -33,35 +33,35 @@ struct CommonRigidBodySetup : public CommonPhysicsSetup
m_pickedConstraint(0)
{
}
virtual void createEmptyDynamicsWorld()
{
///collision configuration contains default setup for memory, collision setup
m_collisionConfiguration = new btDefaultCollisionConfiguration();
//m_collisionConfiguration->setConvexConvexMultipointIterations();
///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
m_broadphase = new btDbvtBroadphase();
///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
m_solver = sol;
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collisionConfiguration);
m_dynamicsWorld->setGravity(btVector3(0, -10, 0));
}
virtual void stepSimulation(float deltaTime)
{
if (m_dynamicsWorld)
{
m_dynamicsWorld->stepSimulation(deltaTime);
}
}
}
virtual void exitPhysics()
@@ -97,18 +97,18 @@ struct CommonRigidBodySetup : public CommonPhysicsSetup
delete shape;
}
m_collisionShapes.clear();
delete m_dynamicsWorld;
delete m_solver;
delete m_broadphase;
delete m_dispatcher;
delete m_collisionConfiguration;
}
virtual void syncPhysicsToGraphics(GraphicsPhysicsBridge& gfxBridge)
{
if (m_dynamicsWorld)
@@ -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)
@@ -127,7 +136,7 @@ struct CommonRigidBodySetup : public CommonPhysicsSetup
m_dynamicsWorld->rayTest(rayFromWorld, rayToWorld, rayCallback);
if (rayCallback.hasHit())
{
btVector3 pickPos = rayCallback.m_hitPointWorld;
btRigidBody* body = (btRigidBody*)btRigidBody::upcast(rayCallback.m_collisionObject);
if (body)
@@ -148,8 +157,8 @@ struct CommonRigidBodySetup : public CommonPhysicsSetup
p2p->m_setting.m_tau = 0.001f;
}
}
// pickObject(pickPos, rayCallback.m_collisionObject);
m_oldPickingPos = rayToWorld;
m_hitPos = pickPos;
@@ -167,13 +176,13 @@ struct CommonRigidBodySetup : public CommonPhysicsSetup
if (pickCon)
{
//keep it at the same picking distance
btVector3 newPivotB;
btVector3 dir = rayToWorld - rayFromWorld;
dir.normalize();
dir *= m_oldPickingDist;
newPivotB = rayFromWorld + dir;
pickCon->setPivotB(newPivotB);
return true;
@@ -191,12 +200,13 @@ struct CommonRigidBodySetup : public CommonPhysicsSetup
m_pickedBody = 0;
}
}
btBoxShape* createBoxShape(const btVector3& halfExtents)
{
btBoxShape* box = new btBoxShape(halfExtents);
m_collisionShapes.push_back(box);
return box;
}

View File

@@ -12,11 +12,12 @@ public:
virtual ~BulletDemoInterface()
{
}
virtual void initPhysics()=0;
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)
@@ -41,7 +43,7 @@ struct MyGraphicsPhysicsBridge : public GraphicsPhysicsBridge
box->setUserIndex(cubeShapeId);
break;
}
default:
{
if (collisionShape->isConvex())
@@ -50,18 +52,18 @@ struct MyGraphicsPhysicsBridge : public GraphicsPhysicsBridge
{
btShapeHull* hull = new btShapeHull(convex);
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;
for (int t=0;t<hull->numTriangles();t++)
{
btVector3 triNormal;
int index0 = hull->getIndexPointer()[t*3+0];
@@ -82,20 +84,20 @@ struct MyGraphicsPhysicsBridge : public GraphicsPhysicsBridge
vtx.pos[1] = pos.y();
vtx.pos[2] = pos.z();
vtx.pos[3] = 0.f;
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;
indices.push_back(gvertices.size());
gvertices.push_back(vtx);
}
}
int shapeId = m_glApp->m_instancingRenderer->registerShape(&gvertices[0].pos[0],gvertices.size(),&indices[0],indices.size());
convex->setUserIndex(shapeId);
}
@@ -123,15 +125,30 @@ 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)
{
}
void Bullet2RigidBodyDemo::initPhysics()
{
@@ -143,7 +160,7 @@ void Bullet2RigidBodyDemo::initPhysics()
void Bullet2RigidBodyDemo::exitPhysics()
{
m_physicsSetup->exitPhysics();
}
@@ -164,6 +181,12 @@ void Bullet2RigidBodyDemo::renderScene()
m_glApp->m_instancingRenderer->renderScene();
}
void Bullet2RigidBodyDemo::physicsDebugDraw()
{
m_physicsSetup->debugDraw();
}
Bullet2RigidBodyDemo::~Bullet2RigidBodyDemo()
{
}
@@ -213,7 +236,7 @@ btVector3 Bullet2RigidBodyDemo::getRayTo(int x,int y)
float height = m_glApp->m_instancingRenderer->getScreenHeight();
aspect = width / height;
hor*=aspect;
@@ -228,14 +251,14 @@ btVector3 Bullet2RigidBodyDemo::getRayTo(int x,int y)
return rayTo;
}
bool Bullet2RigidBodyDemo::mouseMoveCallback(float x,float y)
{
btVector3 rayTo = getRayTo(x, y);
btVector3 rayFrom;
m_glApp->m_instancingRenderer->getCameraPosition(rayFrom);
m_physicsSetup->movePickedBody(rayFrom,rayTo);
return false;
}
bool Bullet2RigidBodyDemo::mouseButtonCallback(int button, int state, float x, float y)
@@ -251,9 +274,10 @@ 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);
}
} else
{

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)
@@ -34,17 +69,36 @@ public:
virtual void draw3dText(const btVector3& location,const char* textString)
{
}
virtual void setDebugMode(int debugMode)
{
m_debugMode = debugMode;
}
virtual int getDebugMode() const
{
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);
};