2015-04-30 21:03:50 +00:00
|
|
|
#include "SerializeSetup.h"
|
|
|
|
#include "../Extras/Serialize/BulletWorldImporter/btBulletWorldImporter.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "../CommonInterfaces/CommonRigidBodyBase.h"
|
|
|
|
|
|
|
|
class SerializeSetup : public CommonRigidBodyBase
|
|
|
|
{
|
2015-05-01 23:27:09 +00:00
|
|
|
|
|
|
|
char m_fileName[1024];
|
|
|
|
|
2015-04-30 21:03:50 +00:00
|
|
|
public:
|
2015-05-01 23:27:09 +00:00
|
|
|
SerializeSetup(struct GUIHelperInterface* helper, const char* fileName);
|
2015-04-30 21:03:50 +00:00
|
|
|
virtual ~SerializeSetup();
|
|
|
|
|
|
|
|
virtual void initPhysics();
|
|
|
|
virtual void stepSimulation(float deltaTime);
|
2015-05-01 23:27:09 +00:00
|
|
|
virtual void setFileName(const char* fileName)
|
|
|
|
{
|
|
|
|
memcpy(m_fileName,fileName,strlen(fileName)+1);
|
|
|
|
}
|
2015-05-01 18:42:14 +00:00
|
|
|
virtual void resetCamera()
|
|
|
|
{
|
|
|
|
float dist = 9.5;
|
|
|
|
float pitch = -2.8;
|
|
|
|
float yaw = 20;
|
|
|
|
float targetPos[3]={-0.2,-1.4,3.5};
|
|
|
|
m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
|
|
|
|
}
|
|
|
|
|
2015-04-30 21:03:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-05-01 23:27:09 +00:00
|
|
|
SerializeSetup::SerializeSetup(struct GUIHelperInterface* helper, const char* fileName)
|
2015-04-30 21:03:50 +00:00
|
|
|
:CommonRigidBodyBase(helper)
|
|
|
|
{
|
2015-05-01 23:27:09 +00:00
|
|
|
if (fileName)
|
|
|
|
{
|
|
|
|
setFileName(fileName);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
setFileName("spider.bullet");
|
|
|
|
}
|
2015-04-30 21:03:50 +00:00
|
|
|
}
|
|
|
|
SerializeSetup::~SerializeSetup()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerializeSetup::initPhysics()
|
|
|
|
{
|
|
|
|
this->createEmptyDynamicsWorld();
|
|
|
|
m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
|
|
|
|
m_dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe+btIDebugDraw::DBG_DrawContactPoints);
|
|
|
|
btBulletWorldImporter* importer = new btBulletWorldImporter(m_dynamicsWorld);
|
2015-05-01 23:27:09 +00:00
|
|
|
|
2015-04-30 21:03:50 +00:00
|
|
|
|
2015-05-01 23:27:09 +00:00
|
|
|
const char* prefix[]={"","./","./data/","../data/","../../data/","../../../data/","../../../../data/"};
|
2015-04-30 21:03:50 +00:00
|
|
|
int numPrefixes = sizeof(prefix)/sizeof(const char*);
|
|
|
|
char relativeFileName[1024];
|
|
|
|
FILE* f=0;
|
|
|
|
|
|
|
|
for (int i=0;!f && i<numPrefixes;i++)
|
|
|
|
{
|
2015-05-01 23:27:09 +00:00
|
|
|
sprintf(relativeFileName,"%s%s",prefix[i],m_fileName);
|
2015-04-30 21:03:50 +00:00
|
|
|
f = fopen(relativeFileName,"rb");
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
importer->loadFile(relativeFileName);
|
|
|
|
|
|
|
|
//for now, guess the up axis from gravity
|
|
|
|
if (m_dynamicsWorld->getGravity()[1] == 0.f)
|
|
|
|
{
|
|
|
|
m_guiHelper->setUpAxis(2);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
m_guiHelper->setUpAxis(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//example code to export the dynamics world to a .bullet file
|
|
|
|
|
|
|
|
|
|
|
|
btDefaultSerializer* serializer = new btDefaultSerializer();
|
|
|
|
m_dynamicsWorld->serialize(serializer);
|
|
|
|
|
2015-05-01 23:27:09 +00:00
|
|
|
FILE* file = fopen("SerializeSetupTestFile.bullet","wb");
|
2015-04-30 21:03:50 +00:00
|
|
|
fwrite(serializer->getBufferPointer(),serializer->getCurrentBufferSize(),1, file);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerializeSetup::stepSimulation(float deltaTime)
|
|
|
|
{
|
|
|
|
CommonRigidBodyBase::stepSimulation(deltaTime);
|
|
|
|
}
|
|
|
|
|
2015-05-01 18:42:14 +00:00
|
|
|
class CommonExampleInterface* SerializeBulletCreateFunc(struct CommonExampleOptions& options)
|
2015-04-30 21:03:50 +00:00
|
|
|
{
|
2015-05-01 23:27:09 +00:00
|
|
|
return new SerializeSetup(options.m_guiHelper, options.m_fileName);
|
2015-04-30 21:03:50 +00:00
|
|
|
}
|