expose debugDraw in gl3
This commit is contained in:
@@ -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
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user