minor cleanup of btgui/demo3 stuff, much more demo cleanup is needed

moved some files in btgui/Bullet3AppSupport
This commit is contained in:
Erwin Coumans
2014-09-16 12:08:24 -07:00
parent 3240d790e4
commit 07e2dcc749
67 changed files with 161 additions and 526 deletions

View File

@@ -2,7 +2,7 @@
#define BASIC_DEMO_H
#include "LinearMath/btVector3.h"
#include "Bullet2RigidBodyDemo.h"
#include "Bullet3AppSupport/Bullet2RigidBodyDemo.h"
#include "../../../Demos/BasicDemo/BasicDemoPhysicsSetup.h"

View File

@@ -1,318 +0,0 @@
#include "Bullet2RigidBodyDemo.h"
#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];
float normal[3];
float texcoord[2];
};
struct MyGraphicsPhysicsBridge : public GraphicsPhysicsBridge
{
SimpleOpenGL3App* m_glApp;
MyDebugDrawer* m_debugDraw;
MyGraphicsPhysicsBridge(SimpleOpenGL3App* glApp)
:m_glApp(glApp), m_debugDraw(0)
{
}
virtual void createRigidBodyGraphicsObject(btRigidBody* body, const btVector3& color)
{
createCollisionObjectGraphicsObject(body,color);
}
virtual void createCollisionObjectGraphicsObject(btCollisionObject* body, const btVector3& color)
{
btCollisionShape* shape = body->getCollisionShape();
btTransform startTransform = body->getWorldTransform();
int graphicsShapeId = shape->getUserIndex();
btAssert(graphicsShapeId >= 0);
btVector3 localScaling = shape->getLocalScaling();
int graphicsInstanceId = m_glApp->m_instancingRenderer->registerGraphicsInstance(graphicsShapeId, startTransform.getOrigin(), startTransform.getRotation(), color, localScaling);
body->setUserIndex(graphicsInstanceId);
}
virtual void createCollisionShapeGraphicsObject(btCollisionShape* collisionShape)
{
//already has a graphics object?
if (collisionShape->getUserIndex()>=0)
return;
//todo: support all collision shape types
switch (collisionShape->getShapeType())
{
case BOX_SHAPE_PROXYTYPE:
{
btBoxShape* box = (btBoxShape*)collisionShape;
btVector3 halfExtents = box->getHalfExtentsWithMargin();
int cubeShapeId = m_glApp->registerCubeShape(halfExtents.x(), halfExtents.y(), halfExtents.z());
box->setUserIndex(cubeShapeId);
break;
}
case TRIANGLE_MESH_SHAPE_PROXYTYPE:
{
break;
}
default:
{
if (collisionShape->isConvex())
{
btConvexShape* convex = (btConvexShape*)collisionShape;
{
btShapeHull* hull = new btShapeHull(convex);
hull->buildHull(0.0);
{
//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];
int index1 = hull->getIndexPointer()[t*3+1];
int index2 = hull->getIndexPointer()[t*3+2];
btVector3 pos0 =hull->getVertexPointer()[index0];
btVector3 pos1 =hull->getVertexPointer()[index1];
btVector3 pos2 =hull->getVertexPointer()[index2];
triNormal = (pos1-pos0).cross(pos2-pos0);
triNormal.normalize();
for (int v=0;v<3;v++)
{
int index = hull->getIndexPointer()[t*3+v];
GraphicsVertex vtx;
btVector3 pos =hull->getVertexPointer()[index];
vtx.pos[0] = pos.x();
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.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);
}
}
} else
{
btAssert(0);
}
}
};
}
virtual void syncPhysicsToGraphics(const btDiscreteDynamicsWorld* rbWorld)
{
int numCollisionObjects = rbWorld->getNumCollisionObjects();
for (int i = 0; i<numCollisionObjects; i++)
{
btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i];
btVector3 pos = colObj->getWorldTransform().getOrigin();
btQuaternion orn = colObj->getWorldTransform().getRotation();
int index = colObj->getUserIndex();
if (index >= 0)
{
m_glApp->m_instancingRenderer->writeSingleInstanceTransformToCPU(pos, orn, index);
}
}
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
);
}
virtual CommonParameterInterface* getParameterInterface()
{
return m_glApp->m_parameterInterface;
}
virtual void setUpAxis(int axis)
{
m_glApp->setUpAxis(axis);
}
};
Bullet2RigidBodyDemo::Bullet2RigidBodyDemo(SimpleOpenGL3App* app, CommonPhysicsSetup* physicsSetup)
: m_physicsSetup(physicsSetup),
m_controlPressed(false),
m_altPressed(false),
m_glApp(app)
{
}
void Bullet2RigidBodyDemo::initPhysics()
{
MyGraphicsPhysicsBridge glBridge(m_glApp);
glBridge.setUpAxis(1);
m_physicsSetup->initPhysics(glBridge);
m_glApp->m_instancingRenderer->writeTransforms();
}
void Bullet2RigidBodyDemo::exitPhysics()
{
m_physicsSetup->exitPhysics();
}
void Bullet2RigidBodyDemo::stepSimulation(float deltaTime)
{
m_physicsSetup->stepSimulation(deltaTime);
}
void Bullet2RigidBodyDemo::renderScene()
{
//sync graphics -> physics world transforms
MyGraphicsPhysicsBridge glBridge(m_glApp);
m_physicsSetup->syncPhysicsToGraphics(glBridge);
m_glApp->m_instancingRenderer->renderScene();
}
void Bullet2RigidBodyDemo::physicsDebugDraw()
{
m_physicsSetup->debugDraw();
}
Bullet2RigidBodyDemo::~Bullet2RigidBodyDemo()
{
}
btVector3 Bullet2RigidBodyDemo::getRayTo(int x,int y)
{
if (!m_glApp->m_instancingRenderer)
{
btAssert(0);
return btVector3(0,0,0);
}
float top = 1.f;
float bottom = -1.f;
float nearPlane = 1.f;
float tanFov = (top-bottom)*0.5f / nearPlane;
float fov = b3Scalar(2.0) * b3Atan(tanFov);
btVector3 camPos,camTarget;
m_glApp->m_instancingRenderer->getCameraPosition(camPos);
m_glApp->m_instancingRenderer->getCameraTargetPosition(camTarget);
btVector3 rayFrom = camPos;
btVector3 rayForward = (camTarget-camPos);
rayForward.normalize();
float farPlane = 10000.f;
rayForward*= farPlane;
btVector3 rightOffset;
btVector3 cameraUp=btVector3(0,0,0);
cameraUp[m_glApp->getUpAxis()]=1;
btVector3 vertical = cameraUp;
btVector3 hor;
hor = rayForward.cross(vertical);
hor.normalize();
vertical = hor.cross(rayForward);
vertical.normalize();
float tanfov = tanf(0.5f*fov);
hor *= 2.f * farPlane * tanfov;
vertical *= 2.f * farPlane * tanfov;
b3Scalar aspect;
float width = m_glApp->m_instancingRenderer->getScreenWidth();
float height = m_glApp->m_instancingRenderer->getScreenHeight();
aspect = width / height;
hor*=aspect;
btVector3 rayToCenter = rayFrom + rayForward;
btVector3 dHor = hor * 1.f/width;
btVector3 dVert = vertical * 1.f/height;
btVector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical;
rayTo += btScalar(x) * dHor;
rayTo -= btScalar(y) * dVert;
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)
{
if (state==1)
{
if(button==0 && (!m_altPressed && !m_controlPressed))
{
btVector3 camPos;
m_glApp->m_instancingRenderer->getCameraPosition(camPos);
btVector3 rayFrom = camPos;
btVector3 rayTo = getRayTo(x,y);
m_physicsSetup->pickBody(rayFrom, rayTo);
}
} else
{
if (button==0)
{
m_physicsSetup->removePickingConstraint();
//remove p2p
}
}
//printf("button=%d, state=%d\n",button,state);
return false;
}

View File

@@ -1,56 +0,0 @@
#ifndef BULLET2_RIGIDBODY_DEMO_H
#define BULLET2_RIGIDBODY_DEMO_H
#include "LinearMath/btVector3.h"
#include "../../AllBullet2Demos/BulletDemoInterface.h"
#include "OpenGLWindow/b3gWindowInterface.h"
#include "../../../Demos/CommonPhysicsSetup.h"
class Bullet2RigidBodyDemo : public BulletDemoInterface
{
CommonPhysicsSetup* m_physicsSetup;
public:
bool m_controlPressed;
bool m_altPressed;
public:
struct SimpleOpenGL3App* m_glApp;
Bullet2RigidBodyDemo(SimpleOpenGL3App* app, CommonPhysicsSetup* physicsSetup);
virtual void initPhysics();
virtual void exitPhysics();
virtual void renderScene();
virtual void physicsDebugDraw();
virtual void stepSimulation(float dt);
virtual CommonPhysicsSetup* getPhysicsSetup()
{
return m_physicsSetup;
}
virtual ~Bullet2RigidBodyDemo();
btVector3 getRayTo(int x,int y);
virtual bool mouseMoveCallback(float x,float y);
virtual bool mouseButtonCallback(int button, int state, float x, float y);
virtual bool keyboardCallback(int key, int state)
{
if (key==B3G_CONTROL)
{
m_controlPressed = (state==1);
}
if (key==B3G_ALT)
{
m_altPressed = (state==1);
}
return false;
}
};
#endif //BULLET2_RIGIDBODY_DEMO_H

View File

@@ -1,107 +0,0 @@
#ifndef MY_DEBUG_DRAWER_H
#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_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);
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)
{
drawLine(PointOnB,PointOnB+normalOnB,color);
}
virtual void reportErrorWarning(const char* warningString)
{
}
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();
}
}
};
#endif //MY_DEBUG_DRAWER_H

View File

@@ -2,7 +2,7 @@
#define CHAIN_DEMO_H
#include "LinearMath/btVector3.h"
#include "../BasicDemo/Bullet2RigidBodyDemo.h"
#include "Bullet3AppSupport/Bullet2RigidBodyDemo.h"

View File

@@ -1,16 +1,16 @@
#ifndef CONSTAINT_PHYSICS_SETUP_H
#define CONSTAINT_PHYSICS_SETUP_H
#include "../../../Demos/CommonRigidBodySetup.h"
struct ConstraintPhysicsSetup : public CommonRigidBodySetup
{
ConstraintPhysicsSetup();
virtual ~ConstraintPhysicsSetup();
virtual void initPhysics(GraphicsPhysicsBridge& gfxBridge);
virtual void stepSimulation(float deltaTime);
};
#endif //CONSTAINT_PHYSICS_SETUP_H
#ifndef CONSTAINT_PHYSICS_SETUP_H
#define CONSTAINT_PHYSICS_SETUP_H
#include "Bullet3AppSupport/CommonRigidBodySetup.h"
struct ConstraintPhysicsSetup : public CommonRigidBodySetup
{
ConstraintPhysicsSetup();
virtual ~ConstraintPhysicsSetup();
virtual void initPhysics(GraphicsPhysicsBridge& gfxBridge);
virtual void stepSimulation(float deltaTime);
};
#endif //CONSTAINT_PHYSICS_SETUP_H

View File

@@ -4,7 +4,7 @@
#include "LinearMath/btVector3.h"
#include "../../AllBullet2Demos/BulletDemoInterface.h"
#include "Bullet3AppSupport/BulletDemoInterface.h"
#include "LinearMath/btAlignedObjectArray.h"

View File

@@ -1,12 +1,12 @@
#ifndef _LUA_PHYSICS_SETUP_H
#define _LUA_PHYSICS_SETUP_H
#include "../Demos/CommonPhysicsSetup.h"
#include "Bullet3AppSupport/CommonPhysicsSetup.h"
//we don't derive from CommonRigidBodySetup because we
//create and own our own dynamics world (one or more)
//at run-time
struct LuaPhysicsSetup : public CommonPhysicsSetup
//at run-time
struct LuaPhysicsSetup : public CommonPhysicsSetup
{
LuaPhysicsSetup(class SimpleOpenGL3App* app);
@@ -18,9 +18,9 @@ struct LuaPhysicsSetup : public CommonPhysicsSetup
class btNNCGConstraintSolver* m_solver;
class btDiscreteDynamicsWorld* m_dynamicsWorld;
class SimpleOpenGL3App* m_glApp;
virtual void initPhysics(GraphicsPhysicsBridge& gfxBridge);
virtual void initPhysics(GraphicsPhysicsBridge& gfxBridge);
virtual void exitPhysics();
virtual void stepSimulation(float deltaTime);
@@ -36,8 +36,8 @@ struct LuaPhysicsSetup : public CommonPhysicsSetup
virtual btRigidBody* createRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape, const btVector4& color=btVector4(1,0,0,1));
virtual btBoxShape* createBoxShape(const btVector3& halfExtents);
};
};
#endif //_LUA_PHYSICS_SETUP_H

View File

@@ -1,7 +1,7 @@
#ifndef TEST_JOINT_TORQUE_SETUP_H
#define TEST_JOINT_TORQUE_SETUP_H
#include "../../../Demos/CommonMultiBodySetup.h"
#include "Bullet3AppSupport/CommonMultiBodySetup.h"
struct TestJointTorqueSetup : public CommonMultiBodySetup
{

View File

@@ -2,7 +2,7 @@
#define RAGDOLL_DEMO_H
#include "../../../Demos/CommonRigidBodySetup.h"
#include "Bullet3AppSupport/CommonRigidBodySetup.h"
#include "../BasicDemo/BasicDemo.h"
struct BulletDemoInterface;