btCollisionWorldImporter.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2014 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 "btCollisionWorldImporter.h"
  14. #include "btBulletCollisionCommon.h"
  15. #include "LinearMath/btSerializer.h" //for btBulletSerializedArrays definition
  16. #ifdef SUPPORT_GIMPACT_SHAPE_IMPORT
  17. #include "BulletCollision/Gimpact/btGImpactShape.h"
  18. #endif //SUPPORT_GIMPACT_SHAPE_IMPORT
  19. btCollisionWorldImporter::btCollisionWorldImporter(btCollisionWorld* world)
  20. : m_collisionWorld(world),
  21. m_verboseMode(0)
  22. {
  23. }
  24. btCollisionWorldImporter::~btCollisionWorldImporter()
  25. {
  26. }
  27. bool btCollisionWorldImporter::convertAllObjects(btBulletSerializedArrays* arrays)
  28. {
  29. m_shapeMap.clear();
  30. m_bodyMap.clear();
  31. int i;
  32. for (i = 0; i < arrays->m_bvhsDouble.size(); i++)
  33. {
  34. btOptimizedBvh* bvh = createOptimizedBvh();
  35. btQuantizedBvhDoubleData* bvhData = arrays->m_bvhsDouble[i];
  36. bvh->deSerializeDouble(*bvhData);
  37. m_bvhMap.insert(arrays->m_bvhsDouble[i], bvh);
  38. }
  39. for (i = 0; i < arrays->m_bvhsFloat.size(); i++)
  40. {
  41. btOptimizedBvh* bvh = createOptimizedBvh();
  42. btQuantizedBvhFloatData* bvhData = arrays->m_bvhsFloat[i];
  43. bvh->deSerializeFloat(*bvhData);
  44. m_bvhMap.insert(arrays->m_bvhsFloat[i], bvh);
  45. }
  46. for (i = 0; i < arrays->m_colShapeData.size(); i++)
  47. {
  48. btCollisionShapeData* shapeData = arrays->m_colShapeData[i];
  49. btCollisionShape* shape = convertCollisionShape(shapeData);
  50. if (shape)
  51. {
  52. // printf("shapeMap.insert(%x,%x)\n",shapeData,shape);
  53. m_shapeMap.insert(shapeData, shape);
  54. }
  55. if (shape && shapeData->m_name)
  56. {
  57. char* newname = duplicateName(shapeData->m_name);
  58. m_objectNameMap.insert(shape, newname);
  59. m_nameShapeMap.insert(newname, shape);
  60. }
  61. }
  62. for (i = 0; i < arrays->m_collisionObjectDataDouble.size(); i++)
  63. {
  64. btCollisionObjectDoubleData* colObjData = arrays->m_collisionObjectDataDouble[i];
  65. btCollisionShape** shapePtr = m_shapeMap.find(colObjData->m_collisionShape);
  66. if (shapePtr && *shapePtr)
  67. {
  68. btTransform startTransform;
  69. colObjData->m_worldTransform.m_origin.m_floats[3] = 0.f;
  70. startTransform.deSerializeDouble(colObjData->m_worldTransform);
  71. btCollisionShape* shape = (btCollisionShape*)*shapePtr;
  72. btCollisionObject* body = createCollisionObject(startTransform, shape, colObjData->m_name);
  73. body->setFriction(btScalar(colObjData->m_friction));
  74. body->setRestitution(btScalar(colObjData->m_restitution));
  75. #ifdef USE_INTERNAL_EDGE_UTILITY
  76. if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
  77. {
  78. btBvhTriangleMeshShape* trimesh = (btBvhTriangleMeshShape*)shape;
  79. if (trimesh->getTriangleInfoMap())
  80. {
  81. body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
  82. }
  83. }
  84. #endif //USE_INTERNAL_EDGE_UTILITY
  85. m_bodyMap.insert(colObjData, body);
  86. }
  87. else
  88. {
  89. printf("error: no shape found\n");
  90. }
  91. }
  92. for (i = 0; i < arrays->m_collisionObjectDataFloat.size(); i++)
  93. {
  94. btCollisionObjectFloatData* colObjData = arrays->m_collisionObjectDataFloat[i];
  95. btCollisionShape** shapePtr = m_shapeMap.find(colObjData->m_collisionShape);
  96. if (shapePtr && *shapePtr)
  97. {
  98. btTransform startTransform;
  99. colObjData->m_worldTransform.m_origin.m_floats[3] = 0.f;
  100. startTransform.deSerializeFloat(colObjData->m_worldTransform);
  101. btCollisionShape* shape = (btCollisionShape*)*shapePtr;
  102. btCollisionObject* body = createCollisionObject(startTransform, shape, colObjData->m_name);
  103. #ifdef USE_INTERNAL_EDGE_UTILITY
  104. if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
  105. {
  106. btBvhTriangleMeshShape* trimesh = (btBvhTriangleMeshShape*)shape;
  107. if (trimesh->getTriangleInfoMap())
  108. {
  109. body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
  110. }
  111. }
  112. #endif //USE_INTERNAL_EDGE_UTILITY
  113. m_bodyMap.insert(colObjData, body);
  114. }
  115. else
  116. {
  117. printf("error: no shape found\n");
  118. }
  119. }
  120. return true;
  121. }
  122. void btCollisionWorldImporter::deleteAllData()
  123. {
  124. int i;
  125. for (i = 0; i < m_allocatedCollisionObjects.size(); i++)
  126. {
  127. if (m_collisionWorld)
  128. m_collisionWorld->removeCollisionObject(m_allocatedCollisionObjects[i]);
  129. delete m_allocatedCollisionObjects[i];
  130. }
  131. m_allocatedCollisionObjects.clear();
  132. for (i = 0; i < m_allocatedCollisionShapes.size(); i++)
  133. {
  134. delete m_allocatedCollisionShapes[i];
  135. }
  136. m_allocatedCollisionShapes.clear();
  137. for (i = 0; i < m_allocatedBvhs.size(); i++)
  138. {
  139. delete m_allocatedBvhs[i];
  140. }
  141. m_allocatedBvhs.clear();
  142. for (i = 0; i < m_allocatedTriangleInfoMaps.size(); i++)
  143. {
  144. delete m_allocatedTriangleInfoMaps[i];
  145. }
  146. m_allocatedTriangleInfoMaps.clear();
  147. for (i = 0; i < m_allocatedTriangleIndexArrays.size(); i++)
  148. {
  149. delete m_allocatedTriangleIndexArrays[i];
  150. }
  151. m_allocatedTriangleIndexArrays.clear();
  152. for (i = 0; i < m_allocatedNames.size(); i++)
  153. {
  154. delete[] m_allocatedNames[i];
  155. }
  156. m_allocatedNames.clear();
  157. for (i = 0; i < m_allocatedbtStridingMeshInterfaceDatas.size(); i++)
  158. {
  159. btStridingMeshInterfaceData* curData = m_allocatedbtStridingMeshInterfaceDatas[i];
  160. for (int a = 0; a < curData->m_numMeshParts; a++)
  161. {
  162. btMeshPartData* curPart = &curData->m_meshPartsPtr[a];
  163. if (curPart->m_vertices3f)
  164. delete[] curPart->m_vertices3f;
  165. if (curPart->m_vertices3d)
  166. delete[] curPart->m_vertices3d;
  167. if (curPart->m_indices32)
  168. delete[] curPart->m_indices32;
  169. if (curPart->m_3indices16)
  170. delete[] curPart->m_3indices16;
  171. if (curPart->m_indices16)
  172. delete[] curPart->m_indices16;
  173. if (curPart->m_3indices8)
  174. delete[] curPart->m_3indices8;
  175. }
  176. delete[] curData->m_meshPartsPtr;
  177. delete curData;
  178. }
  179. m_allocatedbtStridingMeshInterfaceDatas.clear();
  180. for (i = 0; i < m_indexArrays.size(); i++)
  181. {
  182. btAlignedFree(m_indexArrays[i]);
  183. }
  184. m_indexArrays.clear();
  185. for (i = 0; i < m_shortIndexArrays.size(); i++)
  186. {
  187. btAlignedFree(m_shortIndexArrays[i]);
  188. }
  189. m_shortIndexArrays.clear();
  190. for (i = 0; i < m_charIndexArrays.size(); i++)
  191. {
  192. btAlignedFree(m_charIndexArrays[i]);
  193. }
  194. m_charIndexArrays.clear();
  195. for (i = 0; i < m_floatVertexArrays.size(); i++)
  196. {
  197. btAlignedFree(m_floatVertexArrays[i]);
  198. }
  199. m_floatVertexArrays.clear();
  200. for (i = 0; i < m_doubleVertexArrays.size(); i++)
  201. {
  202. btAlignedFree(m_doubleVertexArrays[i]);
  203. }
  204. m_doubleVertexArrays.clear();
  205. }
  206. btCollisionShape* btCollisionWorldImporter::convertCollisionShape(btCollisionShapeData* shapeData)
  207. {
  208. btCollisionShape* shape = 0;
  209. switch (shapeData->m_shapeType)
  210. {
  211. case STATIC_PLANE_PROXYTYPE:
  212. {
  213. btStaticPlaneShapeData* planeData = (btStaticPlaneShapeData*)shapeData;
  214. btVector3 planeNormal, localScaling;
  215. planeNormal.deSerializeFloat(planeData->m_planeNormal);
  216. localScaling.deSerializeFloat(planeData->m_localScaling);
  217. shape = createPlaneShape(planeNormal, planeData->m_planeConstant);
  218. shape->setLocalScaling(localScaling);
  219. break;
  220. }
  221. case SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE:
  222. {
  223. btScaledTriangleMeshShapeData* scaledMesh = (btScaledTriangleMeshShapeData*)shapeData;
  224. btCollisionShapeData* colShapeData = (btCollisionShapeData*)&scaledMesh->m_trimeshShapeData;
  225. colShapeData->m_shapeType = TRIANGLE_MESH_SHAPE_PROXYTYPE;
  226. btCollisionShape* childShape = convertCollisionShape(colShapeData);
  227. btBvhTriangleMeshShape* meshShape = (btBvhTriangleMeshShape*)childShape;
  228. btVector3 localScaling;
  229. localScaling.deSerializeFloat(scaledMesh->m_localScaling);
  230. shape = createScaledTrangleMeshShape(meshShape, localScaling);
  231. break;
  232. }
  233. #ifdef SUPPORT_GIMPACT_SHAPE_IMPORT
  234. case GIMPACT_SHAPE_PROXYTYPE:
  235. {
  236. btGImpactMeshShapeData* gimpactData = (btGImpactMeshShapeData*)shapeData;
  237. if (gimpactData->m_gimpactSubType == CONST_GIMPACT_TRIMESH_SHAPE)
  238. {
  239. btStridingMeshInterfaceData* interfaceData = createStridingMeshInterfaceData(&gimpactData->m_meshInterface);
  240. btTriangleIndexVertexArray* meshInterface = createMeshInterface(*interfaceData);
  241. btGImpactMeshShape* gimpactShape = createGimpactShape(meshInterface);
  242. btVector3 localScaling;
  243. localScaling.deSerializeFloat(gimpactData->m_localScaling);
  244. gimpactShape->setLocalScaling(localScaling);
  245. gimpactShape->setMargin(btScalar(gimpactData->m_collisionMargin));
  246. gimpactShape->updateBound();
  247. shape = gimpactShape;
  248. }
  249. else
  250. {
  251. printf("unsupported gimpact sub type\n");
  252. }
  253. break;
  254. }
  255. #endif //SUPPORT_GIMPACT_SHAPE_IMPORT
  256. //The btCapsuleShape* API has issue passing the margin/scaling/halfextents unmodified through the API
  257. //so deal with this
  258. case CAPSULE_SHAPE_PROXYTYPE:
  259. {
  260. btCapsuleShapeData* capData = (btCapsuleShapeData*)shapeData;
  261. switch (capData->m_upAxis)
  262. {
  263. case 0:
  264. {
  265. shape = createCapsuleShapeX(1, 1);
  266. break;
  267. }
  268. case 1:
  269. {
  270. shape = createCapsuleShapeY(1, 1);
  271. break;
  272. }
  273. case 2:
  274. {
  275. shape = createCapsuleShapeZ(1, 1);
  276. break;
  277. }
  278. default:
  279. {
  280. printf("error: wrong up axis for btCapsuleShape\n");
  281. }
  282. };
  283. if (shape)
  284. {
  285. btCapsuleShape* cap = (btCapsuleShape*)shape;
  286. cap->deSerializeFloat(capData);
  287. }
  288. break;
  289. }
  290. case CYLINDER_SHAPE_PROXYTYPE:
  291. case CONE_SHAPE_PROXYTYPE:
  292. case BOX_SHAPE_PROXYTYPE:
  293. case SPHERE_SHAPE_PROXYTYPE:
  294. case MULTI_SPHERE_SHAPE_PROXYTYPE:
  295. case CONVEX_HULL_SHAPE_PROXYTYPE:
  296. {
  297. btConvexInternalShapeData* bsd = (btConvexInternalShapeData*)shapeData;
  298. btVector3 implicitShapeDimensions;
  299. implicitShapeDimensions.deSerializeFloat(bsd->m_implicitShapeDimensions);
  300. btVector3 localScaling;
  301. localScaling.deSerializeFloat(bsd->m_localScaling);
  302. btVector3 margin(bsd->m_collisionMargin, bsd->m_collisionMargin, bsd->m_collisionMargin);
  303. switch (shapeData->m_shapeType)
  304. {
  305. case BOX_SHAPE_PROXYTYPE:
  306. {
  307. btBoxShape* box = (btBoxShape*)createBoxShape(implicitShapeDimensions / localScaling + margin);
  308. //box->initializePolyhedralFeatures();
  309. shape = box;
  310. break;
  311. }
  312. case SPHERE_SHAPE_PROXYTYPE:
  313. {
  314. shape = createSphereShape(implicitShapeDimensions.getX());
  315. break;
  316. }
  317. case CYLINDER_SHAPE_PROXYTYPE:
  318. {
  319. btCylinderShapeData* cylData = (btCylinderShapeData*)shapeData;
  320. btVector3 halfExtents = implicitShapeDimensions + margin;
  321. switch (cylData->m_upAxis)
  322. {
  323. case 0:
  324. {
  325. shape = createCylinderShapeX(halfExtents.getY(), halfExtents.getX());
  326. break;
  327. }
  328. case 1:
  329. {
  330. shape = createCylinderShapeY(halfExtents.getX(), halfExtents.getY());
  331. break;
  332. }
  333. case 2:
  334. {
  335. shape = createCylinderShapeZ(halfExtents.getX(), halfExtents.getZ());
  336. break;
  337. }
  338. default:
  339. {
  340. printf("unknown Cylinder up axis\n");
  341. }
  342. };
  343. break;
  344. }
  345. case CONE_SHAPE_PROXYTYPE:
  346. {
  347. btConeShapeData* conData = (btConeShapeData*)shapeData;
  348. btVector3 halfExtents = implicitShapeDimensions; //+margin;
  349. switch (conData->m_upIndex)
  350. {
  351. case 0:
  352. {
  353. shape = createConeShapeX(halfExtents.getY(), halfExtents.getX());
  354. break;
  355. }
  356. case 1:
  357. {
  358. shape = createConeShapeY(halfExtents.getX(), halfExtents.getY());
  359. break;
  360. }
  361. case 2:
  362. {
  363. shape = createConeShapeZ(halfExtents.getX(), halfExtents.getZ());
  364. break;
  365. }
  366. default:
  367. {
  368. printf("unknown Cone up axis\n");
  369. }
  370. };
  371. break;
  372. }
  373. case MULTI_SPHERE_SHAPE_PROXYTYPE:
  374. {
  375. btMultiSphereShapeData* mss = (btMultiSphereShapeData*)bsd;
  376. int numSpheres = mss->m_localPositionArraySize;
  377. btAlignedObjectArray<btVector3> tmpPos;
  378. btAlignedObjectArray<btScalar> radii;
  379. radii.resize(numSpheres);
  380. tmpPos.resize(numSpheres);
  381. int i;
  382. for (i = 0; i < numSpheres; i++)
  383. {
  384. tmpPos[i].deSerializeFloat(mss->m_localPositionArrayPtr[i].m_pos);
  385. radii[i] = mss->m_localPositionArrayPtr[i].m_radius;
  386. }
  387. shape = createMultiSphereShape(&tmpPos[0], &radii[0], numSpheres);
  388. break;
  389. }
  390. case CONVEX_HULL_SHAPE_PROXYTYPE:
  391. {
  392. // int sz = sizeof(btConvexHullShapeData);
  393. // int sz2 = sizeof(btConvexInternalShapeData);
  394. // int sz3 = sizeof(btCollisionShapeData);
  395. btConvexHullShapeData* convexData = (btConvexHullShapeData*)bsd;
  396. int numPoints = convexData->m_numUnscaledPoints;
  397. btAlignedObjectArray<btVector3> tmpPoints;
  398. tmpPoints.resize(numPoints);
  399. int i;
  400. for (i = 0; i < numPoints; i++)
  401. {
  402. #ifdef BT_USE_DOUBLE_PRECISION
  403. if (convexData->m_unscaledPointsDoublePtr)
  404. tmpPoints[i].deSerialize(convexData->m_unscaledPointsDoublePtr[i]);
  405. if (convexData->m_unscaledPointsFloatPtr)
  406. tmpPoints[i].deSerializeFloat(convexData->m_unscaledPointsFloatPtr[i]);
  407. #else
  408. if (convexData->m_unscaledPointsFloatPtr)
  409. tmpPoints[i].deSerialize(convexData->m_unscaledPointsFloatPtr[i]);
  410. if (convexData->m_unscaledPointsDoublePtr)
  411. tmpPoints[i].deSerializeDouble(convexData->m_unscaledPointsDoublePtr[i]);
  412. #endif //BT_USE_DOUBLE_PRECISION
  413. }
  414. btConvexHullShape* hullShape = createConvexHullShape();
  415. for (i = 0; i < numPoints; i++)
  416. {
  417. hullShape->addPoint(tmpPoints[i]);
  418. }
  419. hullShape->setMargin(bsd->m_collisionMargin);
  420. //hullShape->initializePolyhedralFeatures();
  421. shape = hullShape;
  422. break;
  423. }
  424. default:
  425. {
  426. printf("error: cannot create shape type (%d)\n", shapeData->m_shapeType);
  427. }
  428. }
  429. if (shape)
  430. {
  431. shape->setMargin(bsd->m_collisionMargin);
  432. btVector3 localScaling;
  433. localScaling.deSerializeFloat(bsd->m_localScaling);
  434. shape->setLocalScaling(localScaling);
  435. }
  436. break;
  437. }
  438. case TRIANGLE_MESH_SHAPE_PROXYTYPE:
  439. {
  440. btTriangleMeshShapeData* trimesh = (btTriangleMeshShapeData*)shapeData;
  441. btStridingMeshInterfaceData* interfaceData = createStridingMeshInterfaceData(&trimesh->m_meshInterface);
  442. btTriangleIndexVertexArray* meshInterface = createMeshInterface(*interfaceData);
  443. if (!meshInterface->getNumSubParts())
  444. {
  445. return 0;
  446. }
  447. btVector3 scaling;
  448. scaling.deSerializeFloat(trimesh->m_meshInterface.m_scaling);
  449. meshInterface->setScaling(scaling);
  450. btOptimizedBvh* bvh = 0;
  451. #if 1
  452. if (trimesh->m_quantizedFloatBvh)
  453. {
  454. btOptimizedBvh** bvhPtr = m_bvhMap.find(trimesh->m_quantizedFloatBvh);
  455. if (bvhPtr && *bvhPtr)
  456. {
  457. bvh = *bvhPtr;
  458. }
  459. else
  460. {
  461. bvh = createOptimizedBvh();
  462. bvh->deSerializeFloat(*trimesh->m_quantizedFloatBvh);
  463. }
  464. }
  465. if (trimesh->m_quantizedDoubleBvh)
  466. {
  467. btOptimizedBvh** bvhPtr = m_bvhMap.find(trimesh->m_quantizedDoubleBvh);
  468. if (bvhPtr && *bvhPtr)
  469. {
  470. bvh = *bvhPtr;
  471. }
  472. else
  473. {
  474. bvh = createOptimizedBvh();
  475. bvh->deSerializeDouble(*trimesh->m_quantizedDoubleBvh);
  476. }
  477. }
  478. #endif
  479. btBvhTriangleMeshShape* trimeshShape = createBvhTriangleMeshShape(meshInterface, bvh);
  480. trimeshShape->setMargin(trimesh->m_collisionMargin);
  481. shape = trimeshShape;
  482. if (trimesh->m_triangleInfoMap)
  483. {
  484. btTriangleInfoMap* map = createTriangleInfoMap();
  485. map->deSerialize(*trimesh->m_triangleInfoMap);
  486. trimeshShape->setTriangleInfoMap(map);
  487. #ifdef USE_INTERNAL_EDGE_UTILITY
  488. gContactAddedCallback = btAdjustInternalEdgeContactsCallback;
  489. #endif //USE_INTERNAL_EDGE_UTILITY
  490. }
  491. //printf("trimesh->m_collisionMargin=%f\n",trimesh->m_collisionMargin);
  492. break;
  493. }
  494. case COMPOUND_SHAPE_PROXYTYPE:
  495. {
  496. btCompoundShapeData* compoundData = (btCompoundShapeData*)shapeData;
  497. btCompoundShape* compoundShape = createCompoundShape();
  498. //btCompoundShapeChildData* childShapeDataArray = &compoundData->m_childShapePtr[0];
  499. btAlignedObjectArray<btCollisionShape*> childShapes;
  500. for (int i = 0; i < compoundData->m_numChildShapes; i++)
  501. {
  502. //btCompoundShapeChildData* ptr = &compoundData->m_childShapePtr[i];
  503. btCollisionShapeData* cd = compoundData->m_childShapePtr[i].m_childShape;
  504. btCollisionShape* childShape = convertCollisionShape(cd);
  505. if (childShape)
  506. {
  507. btTransform localTransform;
  508. localTransform.deSerializeFloat(compoundData->m_childShapePtr[i].m_transform);
  509. compoundShape->addChildShape(localTransform, childShape);
  510. }
  511. else
  512. {
  513. #ifdef _DEBUG
  514. printf("error: couldn't create childShape for compoundShape\n");
  515. #endif
  516. }
  517. }
  518. shape = compoundShape;
  519. break;
  520. }
  521. case SOFTBODY_SHAPE_PROXYTYPE:
  522. {
  523. return 0;
  524. }
  525. default:
  526. {
  527. #ifdef _DEBUG
  528. printf("unsupported shape type (%d)\n", shapeData->m_shapeType);
  529. #endif
  530. }
  531. }
  532. return shape;
  533. }
  534. char* btCollisionWorldImporter::duplicateName(const char* name)
  535. {
  536. if (name)
  537. {
  538. int l = (int)strlen(name);
  539. char* newName = new char[l + 1];
  540. memcpy(newName, name, l);
  541. newName[l] = 0;
  542. m_allocatedNames.push_back(newName);
  543. return newName;
  544. }
  545. return 0;
  546. }
  547. btTriangleIndexVertexArray* btCollisionWorldImporter::createMeshInterface(btStridingMeshInterfaceData& meshData)
  548. {
  549. btTriangleIndexVertexArray* meshInterface = createTriangleMeshContainer();
  550. for (int i = 0; i < meshData.m_numMeshParts; i++)
  551. {
  552. btIndexedMesh meshPart;
  553. meshPart.m_numTriangles = meshData.m_meshPartsPtr[i].m_numTriangles;
  554. meshPart.m_numVertices = meshData.m_meshPartsPtr[i].m_numVertices;
  555. if (meshData.m_meshPartsPtr[i].m_indices32)
  556. {
  557. meshPart.m_indexType = PHY_INTEGER;
  558. meshPart.m_triangleIndexStride = 3 * sizeof(int);
  559. int* indexArray = (int*)btAlignedAlloc(sizeof(int) * 3 * meshPart.m_numTriangles, 16);
  560. m_indexArrays.push_back(indexArray);
  561. for (int j = 0; j < 3 * meshPart.m_numTriangles; j++)
  562. {
  563. indexArray[j] = meshData.m_meshPartsPtr[i].m_indices32[j].m_value;
  564. }
  565. meshPart.m_triangleIndexBase = (const unsigned char*)indexArray;
  566. }
  567. else
  568. {
  569. if (meshData.m_meshPartsPtr[i].m_3indices16)
  570. {
  571. meshPart.m_indexType = PHY_SHORT;
  572. meshPart.m_triangleIndexStride = sizeof(short int) * 3; //sizeof(btShortIntIndexTripletData);
  573. short int* indexArray = (short int*)btAlignedAlloc(sizeof(short int) * 3 * meshPart.m_numTriangles, 16);
  574. m_shortIndexArrays.push_back(indexArray);
  575. for (int j = 0; j < meshPart.m_numTriangles; j++)
  576. {
  577. indexArray[3 * j] = meshData.m_meshPartsPtr[i].m_3indices16[j].m_values[0];
  578. indexArray[3 * j + 1] = meshData.m_meshPartsPtr[i].m_3indices16[j].m_values[1];
  579. indexArray[3 * j + 2] = meshData.m_meshPartsPtr[i].m_3indices16[j].m_values[2];
  580. }
  581. meshPart.m_triangleIndexBase = (const unsigned char*)indexArray;
  582. }
  583. if (meshData.m_meshPartsPtr[i].m_indices16)
  584. {
  585. meshPart.m_indexType = PHY_SHORT;
  586. meshPart.m_triangleIndexStride = 3 * sizeof(short int);
  587. short int* indexArray = (short int*)btAlignedAlloc(sizeof(short int) * 3 * meshPart.m_numTriangles, 16);
  588. m_shortIndexArrays.push_back(indexArray);
  589. for (int j = 0; j < 3 * meshPart.m_numTriangles; j++)
  590. {
  591. indexArray[j] = meshData.m_meshPartsPtr[i].m_indices16[j].m_value;
  592. }
  593. meshPart.m_triangleIndexBase = (const unsigned char*)indexArray;
  594. }
  595. if (meshData.m_meshPartsPtr[i].m_3indices8)
  596. {
  597. meshPart.m_indexType = PHY_UCHAR;
  598. meshPart.m_triangleIndexStride = sizeof(unsigned char) * 3;
  599. unsigned char* indexArray = (unsigned char*)btAlignedAlloc(sizeof(unsigned char) * 3 * meshPart.m_numTriangles, 16);
  600. m_charIndexArrays.push_back(indexArray);
  601. for (int j = 0; j < meshPart.m_numTriangles; j++)
  602. {
  603. indexArray[3 * j] = meshData.m_meshPartsPtr[i].m_3indices8[j].m_values[0];
  604. indexArray[3 * j + 1] = meshData.m_meshPartsPtr[i].m_3indices8[j].m_values[1];
  605. indexArray[3 * j + 2] = meshData.m_meshPartsPtr[i].m_3indices8[j].m_values[2];
  606. }
  607. meshPart.m_triangleIndexBase = (const unsigned char*)indexArray;
  608. }
  609. }
  610. if (meshData.m_meshPartsPtr[i].m_vertices3f)
  611. {
  612. meshPart.m_vertexType = PHY_FLOAT;
  613. meshPart.m_vertexStride = sizeof(btVector3FloatData);
  614. btVector3FloatData* vertices = (btVector3FloatData*)btAlignedAlloc(sizeof(btVector3FloatData) * meshPart.m_numVertices, 16);
  615. m_floatVertexArrays.push_back(vertices);
  616. for (int j = 0; j < meshPart.m_numVertices; j++)
  617. {
  618. vertices[j].m_floats[0] = meshData.m_meshPartsPtr[i].m_vertices3f[j].m_floats[0];
  619. vertices[j].m_floats[1] = meshData.m_meshPartsPtr[i].m_vertices3f[j].m_floats[1];
  620. vertices[j].m_floats[2] = meshData.m_meshPartsPtr[i].m_vertices3f[j].m_floats[2];
  621. vertices[j].m_floats[3] = meshData.m_meshPartsPtr[i].m_vertices3f[j].m_floats[3];
  622. }
  623. meshPart.m_vertexBase = (const unsigned char*)vertices;
  624. }
  625. else
  626. {
  627. meshPart.m_vertexType = PHY_DOUBLE;
  628. meshPart.m_vertexStride = sizeof(btVector3DoubleData);
  629. btVector3DoubleData* vertices = (btVector3DoubleData*)btAlignedAlloc(sizeof(btVector3DoubleData) * meshPart.m_numVertices, 16);
  630. m_doubleVertexArrays.push_back(vertices);
  631. for (int j = 0; j < meshPart.m_numVertices; j++)
  632. {
  633. vertices[j].m_floats[0] = meshData.m_meshPartsPtr[i].m_vertices3d[j].m_floats[0];
  634. vertices[j].m_floats[1] = meshData.m_meshPartsPtr[i].m_vertices3d[j].m_floats[1];
  635. vertices[j].m_floats[2] = meshData.m_meshPartsPtr[i].m_vertices3d[j].m_floats[2];
  636. vertices[j].m_floats[3] = meshData.m_meshPartsPtr[i].m_vertices3d[j].m_floats[3];
  637. }
  638. meshPart.m_vertexBase = (const unsigned char*)vertices;
  639. }
  640. if (meshPart.m_triangleIndexBase && meshPart.m_vertexBase)
  641. {
  642. meshInterface->addIndexedMesh(meshPart, meshPart.m_indexType);
  643. }
  644. }
  645. return meshInterface;
  646. }
  647. btStridingMeshInterfaceData* btCollisionWorldImporter::createStridingMeshInterfaceData(btStridingMeshInterfaceData* interfaceData)
  648. {
  649. //create a new btStridingMeshInterfaceData that is an exact copy of shapedata and store it in the WorldImporter
  650. btStridingMeshInterfaceData* newData = new btStridingMeshInterfaceData;
  651. newData->m_scaling = interfaceData->m_scaling;
  652. newData->m_numMeshParts = interfaceData->m_numMeshParts;
  653. newData->m_meshPartsPtr = new btMeshPartData[newData->m_numMeshParts];
  654. for (int i = 0; i < newData->m_numMeshParts; i++)
  655. {
  656. btMeshPartData* curPart = &interfaceData->m_meshPartsPtr[i];
  657. btMeshPartData* curNewPart = &newData->m_meshPartsPtr[i];
  658. curNewPart->m_numTriangles = curPart->m_numTriangles;
  659. curNewPart->m_numVertices = curPart->m_numVertices;
  660. if (curPart->m_vertices3f)
  661. {
  662. curNewPart->m_vertices3f = new btVector3FloatData[curNewPart->m_numVertices];
  663. memcpy(curNewPart->m_vertices3f, curPart->m_vertices3f, sizeof(btVector3FloatData) * curNewPart->m_numVertices);
  664. }
  665. else
  666. curNewPart->m_vertices3f = NULL;
  667. if (curPart->m_vertices3d)
  668. {
  669. curNewPart->m_vertices3d = new btVector3DoubleData[curNewPart->m_numVertices];
  670. memcpy(curNewPart->m_vertices3d, curPart->m_vertices3d, sizeof(btVector3DoubleData) * curNewPart->m_numVertices);
  671. }
  672. else
  673. curNewPart->m_vertices3d = NULL;
  674. int numIndices = curNewPart->m_numTriangles * 3;
  675. ///the m_3indices8 was not initialized in some Bullet versions, this can cause crashes at loading time
  676. ///we catch it by only dealing with m_3indices8 if none of the other indices are initialized
  677. bool uninitialized3indices8Workaround = false;
  678. if (curPart->m_indices32)
  679. {
  680. uninitialized3indices8Workaround = true;
  681. curNewPart->m_indices32 = new btIntIndexData[numIndices];
  682. memcpy(curNewPart->m_indices32, curPart->m_indices32, sizeof(btIntIndexData) * numIndices);
  683. }
  684. else
  685. curNewPart->m_indices32 = NULL;
  686. if (curPart->m_3indices16)
  687. {
  688. uninitialized3indices8Workaround = true;
  689. curNewPart->m_3indices16 = new btShortIntIndexTripletData[curNewPart->m_numTriangles];
  690. memcpy(curNewPart->m_3indices16, curPart->m_3indices16, sizeof(btShortIntIndexTripletData) * curNewPart->m_numTriangles);
  691. }
  692. else
  693. curNewPart->m_3indices16 = NULL;
  694. if (curPart->m_indices16)
  695. {
  696. uninitialized3indices8Workaround = true;
  697. curNewPart->m_indices16 = new btShortIntIndexData[numIndices];
  698. memcpy(curNewPart->m_indices16, curPart->m_indices16, sizeof(btShortIntIndexData) * numIndices);
  699. }
  700. else
  701. curNewPart->m_indices16 = NULL;
  702. if (!uninitialized3indices8Workaround && curPart->m_3indices8)
  703. {
  704. curNewPart->m_3indices8 = new btCharIndexTripletData[curNewPart->m_numTriangles];
  705. memcpy(curNewPart->m_3indices8, curPart->m_3indices8, sizeof(btCharIndexTripletData) * curNewPart->m_numTriangles);
  706. }
  707. else
  708. curNewPart->m_3indices8 = NULL;
  709. }
  710. m_allocatedbtStridingMeshInterfaceDatas.push_back(newData);
  711. return (newData);
  712. }
  713. #ifdef USE_INTERNAL_EDGE_UTILITY
  714. extern ContactAddedCallback gContactAddedCallback;
  715. static bool btAdjustInternalEdgeContactsCallback(btManifoldPoint& cp, const btCollisionObject* colObj0, int partId0, int index0, const btCollisionObject* colObj1, int partId1, int index1)
  716. {
  717. btAdjustInternalEdgeContacts(cp, colObj1, colObj0, partId1, index1);
  718. //btAdjustInternalEdgeContacts(cp,colObj1,colObj0, partId1,index1, BT_TRIANGLE_CONVEX_BACKFACE_MODE);
  719. //btAdjustInternalEdgeContacts(cp,colObj1,colObj0, partId1,index1, BT_TRIANGLE_CONVEX_DOUBLE_SIDED+BT_TRIANGLE_CONCAVE_DOUBLE_SIDED);
  720. return true;
  721. }
  722. #endif //USE_INTERNAL_EDGE_UTILITY
  723. /*
  724. btRigidBody* btWorldImporter::createRigidBody(bool isDynamic, btScalar mass, const btTransform& startTransform,btCollisionShape* shape,const char* bodyName)
  725. {
  726. btVector3 localInertia;
  727. localInertia.setZero();
  728. if (mass)
  729. shape->calculateLocalInertia(mass,localInertia);
  730. btRigidBody* body = new btRigidBody(mass,0,shape,localInertia);
  731. body->setWorldTransform(startTransform);
  732. if (m_dynamicsWorld)
  733. m_dynamicsWorld->addRigidBody(body);
  734. if (bodyName)
  735. {
  736. char* newname = duplicateName(bodyName);
  737. m_objectNameMap.insert(body,newname);
  738. m_nameBodyMap.insert(newname,body);
  739. }
  740. m_allocatedRigidBodies.push_back(body);
  741. return body;
  742. }
  743. */
  744. btCollisionObject* btCollisionWorldImporter::getCollisionObjectByName(const char* name)
  745. {
  746. btCollisionObject** bodyPtr = m_nameColObjMap.find(name);
  747. if (bodyPtr && *bodyPtr)
  748. {
  749. return *bodyPtr;
  750. }
  751. return 0;
  752. }
  753. btCollisionObject* btCollisionWorldImporter::createCollisionObject(const btTransform& startTransform, btCollisionShape* shape, const char* bodyName)
  754. {
  755. btCollisionObject* colObj = new btCollisionObject();
  756. colObj->setWorldTransform(startTransform);
  757. colObj->setCollisionShape(shape);
  758. m_collisionWorld->addCollisionObject(colObj); //todo: flags etc
  759. if (bodyName)
  760. {
  761. char* newname = duplicateName(bodyName);
  762. m_objectNameMap.insert(colObj, newname);
  763. m_nameColObjMap.insert(newname, colObj);
  764. }
  765. m_allocatedCollisionObjects.push_back(colObj);
  766. return colObj;
  767. }
  768. btCollisionShape* btCollisionWorldImporter::createPlaneShape(const btVector3& planeNormal, btScalar planeConstant)
  769. {
  770. btStaticPlaneShape* shape = new btStaticPlaneShape(planeNormal, planeConstant);
  771. m_allocatedCollisionShapes.push_back(shape);
  772. return shape;
  773. }
  774. btCollisionShape* btCollisionWorldImporter::createBoxShape(const btVector3& halfExtents)
  775. {
  776. btBoxShape* shape = new btBoxShape(halfExtents);
  777. m_allocatedCollisionShapes.push_back(shape);
  778. return shape;
  779. }
  780. btCollisionShape* btCollisionWorldImporter::createSphereShape(btScalar radius)
  781. {
  782. btSphereShape* shape = new btSphereShape(radius);
  783. m_allocatedCollisionShapes.push_back(shape);
  784. return shape;
  785. }
  786. btCollisionShape* btCollisionWorldImporter::createCapsuleShapeX(btScalar radius, btScalar height)
  787. {
  788. btCapsuleShapeX* shape = new btCapsuleShapeX(radius, height);
  789. m_allocatedCollisionShapes.push_back(shape);
  790. return shape;
  791. }
  792. btCollisionShape* btCollisionWorldImporter::createCapsuleShapeY(btScalar radius, btScalar height)
  793. {
  794. btCapsuleShape* shape = new btCapsuleShape(radius, height);
  795. m_allocatedCollisionShapes.push_back(shape);
  796. return shape;
  797. }
  798. btCollisionShape* btCollisionWorldImporter::createCapsuleShapeZ(btScalar radius, btScalar height)
  799. {
  800. btCapsuleShapeZ* shape = new btCapsuleShapeZ(radius, height);
  801. m_allocatedCollisionShapes.push_back(shape);
  802. return shape;
  803. }
  804. btCollisionShape* btCollisionWorldImporter::createCylinderShapeX(btScalar radius, btScalar height)
  805. {
  806. btCylinderShapeX* shape = new btCylinderShapeX(btVector3(height, radius, radius));
  807. m_allocatedCollisionShapes.push_back(shape);
  808. return shape;
  809. }
  810. btCollisionShape* btCollisionWorldImporter::createCylinderShapeY(btScalar radius, btScalar height)
  811. {
  812. btCylinderShape* shape = new btCylinderShape(btVector3(radius, height, radius));
  813. m_allocatedCollisionShapes.push_back(shape);
  814. return shape;
  815. }
  816. btCollisionShape* btCollisionWorldImporter::createCylinderShapeZ(btScalar radius, btScalar height)
  817. {
  818. btCylinderShapeZ* shape = new btCylinderShapeZ(btVector3(radius, radius, height));
  819. m_allocatedCollisionShapes.push_back(shape);
  820. return shape;
  821. }
  822. btCollisionShape* btCollisionWorldImporter::createConeShapeX(btScalar radius, btScalar height)
  823. {
  824. btConeShapeX* shape = new btConeShapeX(radius, height);
  825. m_allocatedCollisionShapes.push_back(shape);
  826. return shape;
  827. }
  828. btCollisionShape* btCollisionWorldImporter::createConeShapeY(btScalar radius, btScalar height)
  829. {
  830. btConeShape* shape = new btConeShape(radius, height);
  831. m_allocatedCollisionShapes.push_back(shape);
  832. return shape;
  833. }
  834. btCollisionShape* btCollisionWorldImporter::createConeShapeZ(btScalar radius, btScalar height)
  835. {
  836. btConeShapeZ* shape = new btConeShapeZ(radius, height);
  837. m_allocatedCollisionShapes.push_back(shape);
  838. return shape;
  839. }
  840. btTriangleIndexVertexArray* btCollisionWorldImporter::createTriangleMeshContainer()
  841. {
  842. btTriangleIndexVertexArray* in = new btTriangleIndexVertexArray();
  843. m_allocatedTriangleIndexArrays.push_back(in);
  844. return in;
  845. }
  846. btOptimizedBvh* btCollisionWorldImporter::createOptimizedBvh()
  847. {
  848. btOptimizedBvh* bvh = new btOptimizedBvh();
  849. m_allocatedBvhs.push_back(bvh);
  850. return bvh;
  851. }
  852. btTriangleInfoMap* btCollisionWorldImporter::createTriangleInfoMap()
  853. {
  854. btTriangleInfoMap* tim = new btTriangleInfoMap();
  855. m_allocatedTriangleInfoMaps.push_back(tim);
  856. return tim;
  857. }
  858. btBvhTriangleMeshShape* btCollisionWorldImporter::createBvhTriangleMeshShape(btStridingMeshInterface* trimesh, btOptimizedBvh* bvh)
  859. {
  860. if (bvh)
  861. {
  862. btBvhTriangleMeshShape* bvhTriMesh = new btBvhTriangleMeshShape(trimesh, bvh->isQuantized(), false);
  863. bvhTriMesh->setOptimizedBvh(bvh);
  864. m_allocatedCollisionShapes.push_back(bvhTriMesh);
  865. return bvhTriMesh;
  866. }
  867. btBvhTriangleMeshShape* ts = new btBvhTriangleMeshShape(trimesh, true);
  868. m_allocatedCollisionShapes.push_back(ts);
  869. return ts;
  870. }
  871. btCollisionShape* btCollisionWorldImporter::createConvexTriangleMeshShape(btStridingMeshInterface* trimesh)
  872. {
  873. return 0;
  874. }
  875. #ifdef SUPPORT_GIMPACT_SHAPE_IMPORT
  876. btGImpactMeshShape* btCollisionWorldImporter::createGimpactShape(btStridingMeshInterface* trimesh)
  877. {
  878. btGImpactMeshShape* shape = new btGImpactMeshShape(trimesh);
  879. m_allocatedCollisionShapes.push_back(shape);
  880. return shape;
  881. }
  882. #endif //SUPPORT_GIMPACT_SHAPE_IMPORT
  883. btConvexHullShape* btCollisionWorldImporter::createConvexHullShape()
  884. {
  885. btConvexHullShape* shape = new btConvexHullShape();
  886. m_allocatedCollisionShapes.push_back(shape);
  887. return shape;
  888. }
  889. btCompoundShape* btCollisionWorldImporter::createCompoundShape()
  890. {
  891. btCompoundShape* shape = new btCompoundShape();
  892. m_allocatedCollisionShapes.push_back(shape);
  893. return shape;
  894. }
  895. btScaledBvhTriangleMeshShape* btCollisionWorldImporter::createScaledTrangleMeshShape(btBvhTriangleMeshShape* meshShape, const btVector3& localScaling)
  896. {
  897. btScaledBvhTriangleMeshShape* shape = new btScaledBvhTriangleMeshShape(meshShape, localScaling);
  898. m_allocatedCollisionShapes.push_back(shape);
  899. return shape;
  900. }
  901. btMultiSphereShape* btCollisionWorldImporter::createMultiSphereShape(const btVector3* positions, const btScalar* radi, int numSpheres)
  902. {
  903. btMultiSphereShape* shape = new btMultiSphereShape(positions, radi, numSpheres);
  904. m_allocatedCollisionShapes.push_back(shape);
  905. return shape;
  906. }
  907. // query for data
  908. int btCollisionWorldImporter::getNumCollisionShapes() const
  909. {
  910. return m_allocatedCollisionShapes.size();
  911. }
  912. btCollisionShape* btCollisionWorldImporter::getCollisionShapeByIndex(int index)
  913. {
  914. return m_allocatedCollisionShapes[index];
  915. }
  916. btCollisionShape* btCollisionWorldImporter::getCollisionShapeByName(const char* name)
  917. {
  918. btCollisionShape** shapePtr = m_nameShapeMap.find(name);
  919. if (shapePtr && *shapePtr)
  920. {
  921. return *shapePtr;
  922. }
  923. return 0;
  924. }
  925. const char* btCollisionWorldImporter::getNameForPointer(const void* ptr) const
  926. {
  927. const char* const* namePtr = m_objectNameMap.find(ptr);
  928. if (namePtr && *namePtr)
  929. return *namePtr;
  930. return 0;
  931. }
  932. int btCollisionWorldImporter::getNumRigidBodies() const
  933. {
  934. return m_allocatedRigidBodies.size();
  935. }
  936. btCollisionObject* btCollisionWorldImporter::getRigidBodyByIndex(int index) const
  937. {
  938. return m_allocatedRigidBodies[index];
  939. }
  940. int btCollisionWorldImporter::getNumBvhs() const
  941. {
  942. return m_allocatedBvhs.size();
  943. }
  944. btOptimizedBvh* btCollisionWorldImporter::getBvhByIndex(int index) const
  945. {
  946. return m_allocatedBvhs[index];
  947. }
  948. int btCollisionWorldImporter::getNumTriangleInfoMaps() const
  949. {
  950. return m_allocatedTriangleInfoMaps.size();
  951. }
  952. btTriangleInfoMap* btCollisionWorldImporter::getTriangleInfoMapByIndex(int index) const
  953. {
  954. return m_allocatedTriangleInfoMaps[index];
  955. }