#pragma warning( disable : 4819 )
#define FREEGLUT_STATIC
#include <btBulletCollisionCommon.h>
#include <btBulletDynamicsCommon.h>
#include <BulletSoftBody/btSoftRigidDynamicsWorld.h>
#include <BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h>
#include <boost/shared_ptr.hpp>
#include <GL/freeglut.h>
#include <iostream>
#include <vector>
#include <crtdbg.h>
#ifdef _DEBUG
#pragma comment( lib, "BulletCollision_d.lib" )
#pragma comment( lib, "BulletDynamics_d.lib" )
#pragma comment( lib, "BulletSoftBody_d.lib" )
#pragma comment( lib, "LinearMath_d.lib" )
#else
#pragma comment( lib, "BulletCollision.lib" )
#pragma comment( lib, "BulletDynamics.lib" )
#pragma comment( lib, "BulletSoftBody.lib" )
#pragma comment( lib, "LinearMath.lib" )
#endif
typedef boost::shared_ptr< btCollisionShape > ShapePtr;
typedef boost::shared_ptr< btMotionState > MotionStatePtr;
typedef boost::shared_ptr< btCollisionObject > ObjectPtr;
boost::shared_ptr< btDynamicsWorld > g_World;
boost::shared_ptr< btCollisionDispatcher > g_Dispatcher;
boost::shared_ptr< btBroadphaseInterface > g_Broadpahse;
boost::shared_ptr< btCollisionConfiguration > g_Configuration;
boost::shared_ptr< btConstraintSolver > g_Solver;
boost::shared_ptr< btIDebugDraw > g_DebugDraw;
std::vector< ShapePtr > g_Shapes;
std::vector< MotionStatePtr > g_MotionStates;
std::vector< ObjectPtr > g_Objects;
btIDebugDraw* CreateDebugDraw();
void CreatePlaneGround();
void CreateBox();
void InitPhysics()
{
//メモリ管理者
g_Configuration.reset( new btSoftBodyRigidBodyCollisionConfiguration );
//高精度の衝突検知
g_Dispatcher.reset( new btCollisionDispatcher( g_Configuration.get()) );
//低精度の衝突検知
g_Broadpahse.reset( new btDbvtBroadphase );
//ジョイントなどの管理
g_Solver.reset( new btSequentialImpulseConstraintSolver );
//エンジン管理者
g_World.reset( new btSoftRigidDynamicsWorld( g_Dispatcher.get(), g_Broadpahse.get(), g_Solver.get(), g_Configuration.get() ) );
//デバッグ描画
g_DebugDraw.reset( CreateDebugDraw() );
g_World->setDebugDrawer( g_DebugDraw.get() );
//重力の設定
g_World->setGravity( btVector3( 0.0f, -9.81f, 0.0f ) );
CreatePlaneGround();
CreateBox();
}
void idle()
{
//シミュレーションを実行
g_World->stepSimulation( 1.0f / 60.0f, 1 );
glutPostRedisplay();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//デバッグ用描画
g_World->debugDrawWorld();
glutSwapBuffers();
}
void release()
{
for( unsigned int i = 0; i < g_Objects.size(); i++ ){
g_World->removeCollisionObject( g_Objects[i].get() );
}
}
void key( unsigned char key, int x, int y )
{
switch( key )
{
case '\033':
release();
exit( 0 );
break;
case 'z':
CreateBox();
break;
default:
break;
}
}
void InitOpenGL()
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(
45.0f,
640.0f/480.0f,
1.0f,
10000.0f );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(
0.0f, 20.0f, 30.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f );
glEnable( GL_DEPTH_TEST );
}
void main( int argc, char* argv[] )
{
//メモリリークチェック
#if defined( DEBUG ) | defined( _DEBUG )
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutInitWindowPosition( 0, 0 );
glutCreateWindow( "Bullet Introduction" );
InitOpenGL();
InitPhysics();
glClearColor( 0.0f ,0.0f, 1.0f, 1.0f );
glutDisplayFunc( display );
glutIdleFunc( idle );
glutKeyboardFunc( key );
glutMainLoop();
}
void CreateBox()
{
//形状の生成
ShapePtr shape( new btBoxShape( btVector3( 1.0f, 1.0f, 1.0f ) ) );
//初期位置の設定
btMatrix3x3 mat;
mat.setIdentity();
MotionStatePtr motion( new btDefaultMotionState( btTransform( mat, btVector3( 0.0f, 9.0f, 0.0f ) ) ) );
float mass = 1.0f;
//質量から慣性の計算
btVector3 localInertia;
shape->calculateLocalInertia( mass, localInertia );
btRigidBody::btRigidBodyConstructionInfo info( mass, motion.get(), shape.get(), localInertia );
//剛体の生成
btRigidBody* body( new btRigidBody( info ) );
//登録
g_World->addRigidBody( body );
//コンテナに追加
g_Objects.push_back( ObjectPtr( body ) );
g_MotionStates.push_back( motion );
g_Shapes.push_back( shape );
}
//平地面の生成
void CreatePlaneGround()
{
//形状の生成
ShapePtr shape( new btBoxShape( btVector3( 20.0f, 0.0f, 20.0f ) ) );
//ワールド行列の仲介者
MotionStatePtr motion( new btDefaultMotionState() );
static const float mass = 0.0f; //質量を0に指定すると静的オブジェクトと判定される
btRigidBody::btRigidBodyConstructionInfo info( mass, motion.get(), shape.get() );
//剛体の生成
btRigidBody* body( new btRigidBody( info ) );
//登録
g_World->addRigidBody( body );
//コンテナに追加
g_Objects.push_back( ObjectPtr( body ) );
g_MotionStates.push_back( motion );
g_Shapes.push_back( shape );
}
btIDebugDraw* CreateDebugDraw() {
class DebugDraw : public btIDebugDraw
{
private:
//デバッグモード
int m_DebugMode;
public:
//コストラクタ
DebugDraw()
{}
//ラインの描画
void drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
{
glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
glColor4f( color.x(), color.y(), color.z(), 1.0f );
glBegin( GL_LINES );
glVertex3fv( from );
glVertex3fv( to );
glEnd();
glPopAttrib();
}
//ラインの描画
void drawLine(const btVector3& from,const btVector3& to, const btVector3& fromColor, const btVector3& toColor)
{
glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
glBegin( GL_LINES );
glColor4f( fromColor.x(), fromColor.y(), fromColor.z(), 1.0f );
glVertex3fv( from );
glColor4f( toColor.x(), toColor.y(), toColor.z(), 1.0f );
glVertex3fv( to );
glEnd();
glPopAttrib();
}
//接触点の描画
void drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color)
{
glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
btVector3 to=PointOnB+normalOnB*distance;
const btVector3&from = PointOnB;
glColor4f(color.getX(), color.getY(), color.getZ(),1.f);
glBegin(GL_LINES);
glVertex3d(from.getX(), from.getY(), from.getZ());
glVertex3d(to.getX(), to.getY(), to.getZ());
glEnd();
glPopAttrib();
}
//エラーの表示
void reportErrorWarning(const char* warningString)
{
std::cout << warningString << std::endl;
}
//3Dテキストの描画
void draw3dText(const btVector3& location,const char* textString)
{}
//デバッグモードの設定
void setDebugMode(int debugMode)
{
m_DebugMode = debugMode;
}
//デバッグモードの取得
int getDebugMode() const
{
return m_DebugMode;
}
};
return new DebugDraw;
}
最終更新:2010年06月07日 13:50