btBulletWorldImporter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2012 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "btBulletWorldImporter.h"
  14. #include "../BulletFileLoader/btBulletFile.h"
  15. #include "btBulletDynamicsCommon.h"
  16. #ifndef USE_GIMPACT
  17. #include "BulletCollision/Gimpact/btGImpactShape.h"
  18. #endif
  19. //#define USE_INTERNAL_EDGE_UTILITY
  20. #ifdef USE_INTERNAL_EDGE_UTILITY
  21. #include "BulletCollision/CollisionDispatch/btInternalEdgeUtility.h"
  22. #endif //USE_INTERNAL_EDGE_UTILITY
  23. btBulletWorldImporter::btBulletWorldImporter(btDynamicsWorld* world)
  24. :btWorldImporter(world)
  25. {
  26. }
  27. btBulletWorldImporter::~btBulletWorldImporter()
  28. {
  29. }
  30. bool btBulletWorldImporter::loadFile( const char* fileName, const char* preSwapFilenameOut)
  31. {
  32. bParse::btBulletFile* bulletFile2 = new bParse::btBulletFile(fileName);
  33. bool result = loadFileFromMemory(bulletFile2);
  34. //now you could save the file in 'native' format using
  35. //bulletFile2->writeFile("native.bullet");
  36. if (result)
  37. {
  38. if (preSwapFilenameOut)
  39. {
  40. bulletFile2->preSwap();
  41. bulletFile2->writeFile(preSwapFilenameOut);
  42. }
  43. }
  44. delete bulletFile2;
  45. return result;
  46. }
  47. bool btBulletWorldImporter::loadFileFromMemory( char* memoryBuffer, int len)
  48. {
  49. bParse::btBulletFile* bulletFile2 = new bParse::btBulletFile(memoryBuffer,len);
  50. bool result = loadFileFromMemory(bulletFile2);
  51. delete bulletFile2;
  52. return result;
  53. }
  54. bool btBulletWorldImporter::loadFileFromMemory( bParse::btBulletFile* bulletFile2)
  55. {
  56. bool ok = (bulletFile2->getFlags()& bParse::FD_OK)!=0;
  57. if (ok)
  58. bulletFile2->parse(m_verboseMode);
  59. else
  60. return false;
  61. if (m_verboseMode & bParse::FD_VERBOSE_DUMP_CHUNKS)
  62. {
  63. bulletFile2->dumpChunks(bulletFile2->getFileDNA());
  64. }
  65. return convertAllObjects(bulletFile2);
  66. }
  67. bool btBulletWorldImporter::convertAllObjects( bParse::btBulletFile* bulletFile2)
  68. {
  69. m_shapeMap.clear();
  70. m_bodyMap.clear();
  71. int i;
  72. for (i=0;i<bulletFile2->m_bvhs.size();i++)
  73. {
  74. btOptimizedBvh* bvh = createOptimizedBvh();
  75. if (bulletFile2->getFlags() & bParse::FD_DOUBLE_PRECISION)
  76. {
  77. btQuantizedBvhDoubleData* bvhData = (btQuantizedBvhDoubleData*)bulletFile2->m_bvhs[i];
  78. bvh->deSerializeDouble(*bvhData);
  79. } else
  80. {
  81. btQuantizedBvhFloatData* bvhData = (btQuantizedBvhFloatData*)bulletFile2->m_bvhs[i];
  82. bvh->deSerializeFloat(*bvhData);
  83. }
  84. m_bvhMap.insert(bulletFile2->m_bvhs[i],bvh);
  85. }
  86. for (i=0;i<bulletFile2->m_collisionShapes.size();i++)
  87. {
  88. btCollisionShapeData* shapeData = (btCollisionShapeData*)bulletFile2->m_collisionShapes[i];
  89. btCollisionShape* shape = convertCollisionShape(shapeData);
  90. if (shape)
  91. {
  92. // printf("shapeMap.insert(%x,%x)\n",shapeData,shape);
  93. m_shapeMap.insert(shapeData,shape);
  94. }
  95. if (shape&& shapeData->m_name)
  96. {
  97. char* newname = duplicateName(shapeData->m_name);
  98. m_objectNameMap.insert(shape,newname);
  99. m_nameShapeMap.insert(newname,shape);
  100. }
  101. }
  102. for (int i=0;i<bulletFile2->m_dynamicsWorldInfo.size();i++)
  103. {
  104. if (bulletFile2->getFlags() & bParse::FD_DOUBLE_PRECISION)
  105. {
  106. btDynamicsWorldDoubleData* solverInfoData = (btDynamicsWorldDoubleData*)bulletFile2->m_dynamicsWorldInfo[i];
  107. btContactSolverInfo solverInfo;
  108. btVector3 gravity;
  109. gravity.deSerializeDouble(solverInfoData->m_gravity);
  110. solverInfo.m_tau = btScalar(solverInfoData->m_solverInfo.m_tau);
  111. solverInfo.m_damping = btScalar(solverInfoData->m_solverInfo.m_damping);
  112. solverInfo.m_friction = btScalar(solverInfoData->m_solverInfo.m_friction);
  113. solverInfo.m_timeStep = btScalar(solverInfoData->m_solverInfo.m_timeStep);
  114. solverInfo.m_restitution = btScalar(solverInfoData->m_solverInfo.m_restitution);
  115. solverInfo.m_maxErrorReduction = btScalar(solverInfoData->m_solverInfo.m_maxErrorReduction);
  116. solverInfo.m_sor = btScalar(solverInfoData->m_solverInfo.m_sor);
  117. solverInfo.m_erp = btScalar(solverInfoData->m_solverInfo.m_erp);
  118. solverInfo.m_erp2 = btScalar(solverInfoData->m_solverInfo.m_erp2);
  119. solverInfo.m_globalCfm = btScalar(solverInfoData->m_solverInfo.m_globalCfm);
  120. solverInfo.m_splitImpulsePenetrationThreshold = btScalar(solverInfoData->m_solverInfo.m_splitImpulsePenetrationThreshold);
  121. solverInfo.m_splitImpulseTurnErp = btScalar(solverInfoData->m_solverInfo.m_splitImpulseTurnErp);
  122. solverInfo.m_linearSlop = btScalar(solverInfoData->m_solverInfo.m_linearSlop);
  123. solverInfo.m_warmstartingFactor = btScalar(solverInfoData->m_solverInfo.m_warmstartingFactor);
  124. solverInfo.m_maxGyroscopicForce = btScalar(solverInfoData->m_solverInfo.m_maxGyroscopicForce);
  125. solverInfo.m_singleAxisRollingFrictionThreshold = btScalar(solverInfoData->m_solverInfo.m_singleAxisRollingFrictionThreshold);
  126. solverInfo.m_numIterations = solverInfoData->m_solverInfo.m_numIterations;
  127. solverInfo.m_solverMode = solverInfoData->m_solverInfo.m_solverMode;
  128. solverInfo.m_restingContactRestitutionThreshold = solverInfoData->m_solverInfo.m_restingContactRestitutionThreshold;
  129. solverInfo.m_minimumSolverBatchSize = solverInfoData->m_solverInfo.m_minimumSolverBatchSize;
  130. solverInfo.m_splitImpulse = solverInfoData->m_solverInfo.m_splitImpulse;
  131. setDynamicsWorldInfo(gravity,solverInfo);
  132. } else
  133. {
  134. btDynamicsWorldFloatData* solverInfoData = (btDynamicsWorldFloatData*)bulletFile2->m_dynamicsWorldInfo[i];
  135. btContactSolverInfo solverInfo;
  136. btVector3 gravity;
  137. gravity.deSerializeFloat(solverInfoData->m_gravity);
  138. solverInfo.m_tau = solverInfoData->m_solverInfo.m_tau;
  139. solverInfo.m_damping = solverInfoData->m_solverInfo.m_damping;
  140. solverInfo.m_friction = solverInfoData->m_solverInfo.m_friction;
  141. solverInfo.m_timeStep = solverInfoData->m_solverInfo.m_timeStep;
  142. solverInfo.m_restitution = solverInfoData->m_solverInfo.m_restitution;
  143. solverInfo.m_maxErrorReduction = solverInfoData->m_solverInfo.m_maxErrorReduction;
  144. solverInfo.m_sor = solverInfoData->m_solverInfo.m_sor;
  145. solverInfo.m_erp = solverInfoData->m_solverInfo.m_erp;
  146. solverInfo.m_erp2 = solverInfoData->m_solverInfo.m_erp2;
  147. solverInfo.m_globalCfm = solverInfoData->m_solverInfo.m_globalCfm;
  148. solverInfo.m_splitImpulsePenetrationThreshold = solverInfoData->m_solverInfo.m_splitImpulsePenetrationThreshold;
  149. solverInfo.m_splitImpulseTurnErp = solverInfoData->m_solverInfo.m_splitImpulseTurnErp;
  150. solverInfo.m_linearSlop = solverInfoData->m_solverInfo.m_linearSlop;
  151. solverInfo.m_warmstartingFactor = solverInfoData->m_solverInfo.m_warmstartingFactor;
  152. solverInfo.m_maxGyroscopicForce = solverInfoData->m_solverInfo.m_maxGyroscopicForce;
  153. solverInfo.m_singleAxisRollingFrictionThreshold = solverInfoData->m_solverInfo.m_singleAxisRollingFrictionThreshold;
  154. solverInfo.m_numIterations = solverInfoData->m_solverInfo.m_numIterations;
  155. solverInfo.m_solverMode = solverInfoData->m_solverInfo.m_solverMode;
  156. solverInfo.m_restingContactRestitutionThreshold = solverInfoData->m_solverInfo.m_restingContactRestitutionThreshold;
  157. solverInfo.m_minimumSolverBatchSize = solverInfoData->m_solverInfo.m_minimumSolverBatchSize;
  158. solverInfo.m_splitImpulse = solverInfoData->m_solverInfo.m_splitImpulse;
  159. setDynamicsWorldInfo(gravity,solverInfo);
  160. }
  161. }
  162. for (i=0;i<bulletFile2->m_rigidBodies.size();i++)
  163. {
  164. if (bulletFile2->getFlags() & bParse::FD_DOUBLE_PRECISION)
  165. {
  166. btRigidBodyDoubleData* colObjData = (btRigidBodyDoubleData*)bulletFile2->m_rigidBodies[i];
  167. convertRigidBodyDouble(colObjData);
  168. } else
  169. {
  170. btRigidBodyFloatData* colObjData = (btRigidBodyFloatData*)bulletFile2->m_rigidBodies[i];
  171. convertRigidBodyFloat(colObjData);
  172. }
  173. }
  174. for (i=0;i<bulletFile2->m_collisionObjects.size();i++)
  175. {
  176. if (bulletFile2->getFlags() & bParse::FD_DOUBLE_PRECISION)
  177. {
  178. btCollisionObjectDoubleData* colObjData = (btCollisionObjectDoubleData*)bulletFile2->m_collisionObjects[i];
  179. btCollisionShape** shapePtr = m_shapeMap.find(colObjData->m_collisionShape);
  180. if (shapePtr && *shapePtr)
  181. {
  182. btTransform startTransform;
  183. colObjData->m_worldTransform.m_origin.m_floats[3] = 0.f;
  184. startTransform.deSerializeDouble(colObjData->m_worldTransform);
  185. btCollisionShape* shape = (btCollisionShape*)*shapePtr;
  186. btCollisionObject* body = createCollisionObject(startTransform,shape,colObjData->m_name);
  187. body->setFriction(btScalar(colObjData->m_friction));
  188. body->setRestitution(btScalar(colObjData->m_restitution));
  189. #ifdef USE_INTERNAL_EDGE_UTILITY
  190. if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
  191. {
  192. btBvhTriangleMeshShape* trimesh = (btBvhTriangleMeshShape*)shape;
  193. if (trimesh->getTriangleInfoMap())
  194. {
  195. body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
  196. }
  197. }
  198. #endif //USE_INTERNAL_EDGE_UTILITY
  199. m_bodyMap.insert(colObjData,body);
  200. } else
  201. {
  202. printf("error: no shape found\n");
  203. }
  204. } else
  205. {
  206. btCollisionObjectFloatData* colObjData = (btCollisionObjectFloatData*)bulletFile2->m_collisionObjects[i];
  207. btCollisionShape** shapePtr = m_shapeMap.find(colObjData->m_collisionShape);
  208. if (shapePtr && *shapePtr)
  209. {
  210. btTransform startTransform;
  211. colObjData->m_worldTransform.m_origin.m_floats[3] = 0.f;
  212. startTransform.deSerializeFloat(colObjData->m_worldTransform);
  213. btCollisionShape* shape = (btCollisionShape*)*shapePtr;
  214. btCollisionObject* body = createCollisionObject(startTransform,shape,colObjData->m_name);
  215. #ifdef USE_INTERNAL_EDGE_UTILITY
  216. if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
  217. {
  218. btBvhTriangleMeshShape* trimesh = (btBvhTriangleMeshShape*)shape;
  219. if (trimesh->getTriangleInfoMap())
  220. {
  221. body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
  222. }
  223. }
  224. #endif //USE_INTERNAL_EDGE_UTILITY
  225. m_bodyMap.insert(colObjData,body);
  226. } else
  227. {
  228. printf("error: no shape found\n");
  229. }
  230. }
  231. }
  232. for (i=0;i<bulletFile2->m_constraints.size();i++)
  233. {
  234. btTypedConstraintData2* constraintData = (btTypedConstraintData2*)bulletFile2->m_constraints[i];
  235. btCollisionObject** colAptr = m_bodyMap.find(constraintData->m_rbA);
  236. btCollisionObject** colBptr = m_bodyMap.find(constraintData->m_rbB);
  237. btRigidBody* rbA = 0;
  238. btRigidBody* rbB = 0;
  239. if (colAptr)
  240. {
  241. rbA = btRigidBody::upcast(*colAptr);
  242. if (!rbA)
  243. rbA = &getFixedBody();
  244. }
  245. if (colBptr)
  246. {
  247. rbB = btRigidBody::upcast(*colBptr);
  248. if (!rbB)
  249. rbB = &getFixedBody();
  250. }
  251. if (!rbA && !rbB)
  252. continue;
  253. bool isDoublePrecisionData = (bulletFile2->getFlags() & bParse::FD_DOUBLE_PRECISION)!=0;
  254. if (isDoublePrecisionData)
  255. {
  256. if (bulletFile2->getVersion()>=282)
  257. {
  258. btTypedConstraintDoubleData* dc = (btTypedConstraintDoubleData*)constraintData;
  259. convertConstraintDouble(dc, rbA,rbB, bulletFile2->getVersion());
  260. } else
  261. {
  262. //double-precision constraints were messed up until 2.82, try to recover data...
  263. btTypedConstraintData* oldData = (btTypedConstraintData*)constraintData;
  264. convertConstraintBackwardsCompatible281(oldData, rbA,rbB, bulletFile2->getVersion());
  265. }
  266. }
  267. else
  268. {
  269. btTypedConstraintFloatData* dc = (btTypedConstraintFloatData*)constraintData;
  270. convertConstraintFloat(dc, rbA,rbB, bulletFile2->getVersion());
  271. }
  272. }
  273. return true;
  274. }