From c95fe654ad0a03e32383e5c6378a051ee5764d2d Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Thu, 30 Apr 2015 14:03:50 -0700 Subject: [PATCH] add example to import and export a binary .bullet file --- examples/ExampleBrowser/CMakeLists.txt | 10 +- examples/ExampleBrowser/ExampleEntries.cpp | 3 + examples/ExampleBrowser/premake4.lua | 2 + .../Importers/ImportBullet/SerializeSetup.cpp | 95 +++++++++++++++++++ .../Importers/ImportBullet/SerializeSetup.h | 7 ++ 5 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 examples/Importers/ImportBullet/SerializeSetup.cpp create mode 100644 examples/Importers/ImportBullet/SerializeSetup.h diff --git a/examples/ExampleBrowser/CMakeLists.txt b/examples/ExampleBrowser/CMakeLists.txt index c7e0553be..66048cd51 100644 --- a/examples/ExampleBrowser/CMakeLists.txt +++ b/examples/ExampleBrowser/CMakeLists.txt @@ -34,7 +34,15 @@ SET(App_ExampleBrowser_SRCS ../Benchmarks/landscapeData.h ../Benchmarks/TaruData ../Importers/ImportBsp/BspConverter.h - ../Importers/ImportBsp/BspLoader.h + ../Importers/ImportBullet/SerializeSetup.cpp + ../Importers/ImportBullet/SerializeSetup.h + + ../../Extras/Serialize/BulletWorldImporter/btWorldImporter.cpp + ../../Extras/Serialize/BulletWorldImporter/btBulletWorldImporter.cpp +../../Extras/Serialize/BulletFileLoader/bChunk.cpp ../../Extras/Serialize/BulletFileLoader/bFile.cpp +../../Extras/Serialize/BulletFileLoader/bDNA.cpp ../../Extras/Serialize/BulletFileLoader/btBulletFile.cpp + + ../Importers/ImportBsp/BspLoader.h ../Importers/ImportBsp/ImportBspExample.h ../Importers/ImportColladaDemo/btMatrix4x4.h ../Importers/ImportColladaDemo/ColladaGraphicsInstance.h diff --git a/examples/ExampleBrowser/ExampleEntries.cpp b/examples/ExampleBrowser/ExampleEntries.cpp index 050ac92ab..e2859589a 100644 --- a/examples/ExampleBrowser/ExampleEntries.cpp +++ b/examples/ExampleBrowser/ExampleEntries.cpp @@ -26,6 +26,7 @@ #include "../Constraints/ConstraintDemo.h" #include "../Vehicles/Hinge2Vehicle.h" #include "../Experiments/ImplicitCloth/ImplicitClothExample.h" +#include "../Importers/ImportBullet/SerializeSetup.h" struct ExampleEntry @@ -128,6 +129,8 @@ static ExampleEntry gDefaultExamples[]= ExampleEntry(0,"Importers"), + ExampleEntry(1,"Import .bullet", "Load a binary .bullet file. The serialization mechanism can deal with versioning, differences in endianess, 32 and 64bit, double/single precision. It is easy to save a .bullet file, see the examples/Importers/ImportBullet/SerializeDemo.cpp for a code example how to export a .bullet file.", SerializeBulletCreateFunc), + ExampleEntry(1,"Wavefront Obj", "Import a Wavefront .obj file", ImportObjCreateFunc, 0), ExampleEntry(1,"Quake BSP", "Import a Quake .bsp file", ImportBspCreateFunc, 0), diff --git a/examples/ExampleBrowser/premake4.lua b/examples/ExampleBrowser/premake4.lua index 769527caf..bab36760f 100644 --- a/examples/ExampleBrowser/premake4.lua +++ b/examples/ExampleBrowser/premake4.lua @@ -26,6 +26,8 @@ "../CommonInterfaces/*", "../ForkLift/ForkLiftDemo.*", "../Importers/**", + "../../Extras/Serialize/BulletWorldImporter/*", + "../../Extras/Serialize/BulletFileLoader/*", "../Planar2D/Planar2D.*", "../RenderingExamples/*", "../VoronoiFracture/*", diff --git a/examples/Importers/ImportBullet/SerializeSetup.cpp b/examples/Importers/ImportBullet/SerializeSetup.cpp new file mode 100644 index 000000000..7351b0cbd --- /dev/null +++ b/examples/Importers/ImportBullet/SerializeSetup.cpp @@ -0,0 +1,95 @@ +#include "SerializeSetup.h" +#include "../Extras/Serialize/BulletWorldImporter/btBulletWorldImporter.h" + + +#include "../CommonInterfaces/CommonRigidBodyBase.h" + +class SerializeSetup : public CommonRigidBodyBase +{ +public: + SerializeSetup(struct GUIHelperInterface* helper); + virtual ~SerializeSetup(); + + virtual void initPhysics(); + virtual void stepSimulation(float deltaTime); +}; + + +SerializeSetup::SerializeSetup(struct GUIHelperInterface* helper) +:CommonRigidBodyBase(helper) +{ + +} +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); + const char* someFileName="spider.bullet"; + + const char* prefix[]={"./","./data/","../data/","../../data/","../../../data/","../../../../data/"}; + int numPrefixes = sizeof(prefix)/sizeof(const char*); + char relativeFileName[1024]; + FILE* f=0; + bool fileFound = false; + int result = 0; + + for (int i=0;!f && iloadFile(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); + + FILE* file = fopen("testFile.bullet","wb"); + fwrite(serializer->getBufferPointer(),serializer->getCurrentBufferSize(),1, file); + fclose(file); + + + + + m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld); +} + +void SerializeSetup::stepSimulation(float deltaTime) +{ + CommonRigidBodyBase::stepSimulation(deltaTime); +} + +class CommonExampleInterface* SerializeBulletCreateFunc(struct PhysicsInterface* pint, struct GUIHelperInterface* helper, int option) +{ + return new SerializeSetup(helper); +} diff --git a/examples/Importers/ImportBullet/SerializeSetup.h b/examples/Importers/ImportBullet/SerializeSetup.h new file mode 100644 index 000000000..81a0500e6 --- /dev/null +++ b/examples/Importers/ImportBullet/SerializeSetup.h @@ -0,0 +1,7 @@ +#ifndef SERIALIZE_SETUP_H +#define SERIALIZE_SETUP_H + +class CommonExampleInterface* SerializeBulletCreateFunc(struct PhysicsInterface* pint, struct GUIHelperInterface* helper, int option); + + +#endif //SERIALIZE_SETUP_H