btStridingMeshInterface.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 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 "btStridingMeshInterface.h"
  14. #include "bullet/LinearMath/btSerializer.h"
  15. btStridingMeshInterface::~btStridingMeshInterface()
  16. {
  17. }
  18. void btStridingMeshInterface::InternalProcessAllTriangles(btInternalTriangleIndexCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
  19. {
  20. (void)aabbMin;
  21. (void)aabbMax;
  22. int numtotalphysicsverts = 0;
  23. int part,graphicssubparts = getNumSubParts();
  24. const unsigned char * vertexbase;
  25. const unsigned char * indexbase;
  26. int indexstride;
  27. PHY_ScalarType type;
  28. PHY_ScalarType gfxindextype;
  29. int stride,numverts,numtriangles;
  30. int gfxindex;
  31. btVector3 triangle[3];
  32. btVector3 meshScaling = getScaling();
  33. ///if the number of parts is big, the performance might drop due to the innerloop switch on indextype
  34. for (part=0;part<graphicssubparts ;part++)
  35. {
  36. getLockedReadOnlyVertexIndexBase(&vertexbase,numverts,type,stride,&indexbase,indexstride,numtriangles,gfxindextype,part);
  37. numtotalphysicsverts+=numtriangles*3; //upper bound
  38. ///unlike that developers want to pass in double-precision meshes in single-precision Bullet build
  39. ///so disable this feature by default
  40. ///see patch http://code.google.com/p/bullet/issues/detail?id=213
  41. switch (type)
  42. {
  43. case PHY_FLOAT:
  44. {
  45. float* graphicsbase;
  46. switch (gfxindextype)
  47. {
  48. case PHY_INTEGER:
  49. {
  50. for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
  51. {
  52. unsigned int* tri_indices= (unsigned int*)(indexbase+gfxindex*indexstride);
  53. graphicsbase = (float*)(vertexbase+tri_indices[0]*stride);
  54. triangle[0].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ());
  55. graphicsbase = (float*)(vertexbase+tri_indices[1]*stride);
  56. triangle[1].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
  57. graphicsbase = (float*)(vertexbase+tri_indices[2]*stride);
  58. triangle[2].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
  59. callback->internalProcessTriangleIndex(triangle,part,gfxindex);
  60. }
  61. break;
  62. }
  63. case PHY_SHORT:
  64. {
  65. for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
  66. {
  67. unsigned short int* tri_indices= (unsigned short int*)(indexbase+gfxindex*indexstride);
  68. graphicsbase = (float*)(vertexbase+tri_indices[0]*stride);
  69. triangle[0].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ());
  70. graphicsbase = (float*)(vertexbase+tri_indices[1]*stride);
  71. triangle[1].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
  72. graphicsbase = (float*)(vertexbase+tri_indices[2]*stride);
  73. triangle[2].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
  74. callback->internalProcessTriangleIndex(triangle,part,gfxindex);
  75. }
  76. break;
  77. }
  78. case PHY_UCHAR:
  79. {
  80. for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
  81. {
  82. unsigned char* tri_indices= (unsigned char*)(indexbase+gfxindex*indexstride);
  83. graphicsbase = (float*)(vertexbase+tri_indices[0]*stride);
  84. triangle[0].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ());
  85. graphicsbase = (float*)(vertexbase+tri_indices[1]*stride);
  86. triangle[1].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
  87. graphicsbase = (float*)(vertexbase+tri_indices[2]*stride);
  88. triangle[2].setValue(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(), graphicsbase[2]*meshScaling.getZ());
  89. callback->internalProcessTriangleIndex(triangle,part,gfxindex);
  90. }
  91. break;
  92. }
  93. default:
  94. btAssert((gfxindextype == PHY_INTEGER) || (gfxindextype == PHY_SHORT));
  95. }
  96. break;
  97. }
  98. case PHY_DOUBLE:
  99. {
  100. double* graphicsbase;
  101. switch (gfxindextype)
  102. {
  103. case PHY_INTEGER:
  104. {
  105. for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
  106. {
  107. unsigned int* tri_indices= (unsigned int*)(indexbase+gfxindex*indexstride);
  108. graphicsbase = (double*)(vertexbase+tri_indices[0]*stride);
  109. triangle[0].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(),(btScalar)graphicsbase[2]*meshScaling.getZ());
  110. graphicsbase = (double*)(vertexbase+tri_indices[1]*stride);
  111. triangle[1].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
  112. graphicsbase = (double*)(vertexbase+tri_indices[2]*stride);
  113. triangle[2].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
  114. callback->internalProcessTriangleIndex(triangle,part,gfxindex);
  115. }
  116. break;
  117. }
  118. case PHY_SHORT:
  119. {
  120. for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
  121. {
  122. unsigned short int* tri_indices= (unsigned short int*)(indexbase+gfxindex*indexstride);
  123. graphicsbase = (double*)(vertexbase+tri_indices[0]*stride);
  124. triangle[0].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(),(btScalar)graphicsbase[2]*meshScaling.getZ());
  125. graphicsbase = (double*)(vertexbase+tri_indices[1]*stride);
  126. triangle[1].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
  127. graphicsbase = (double*)(vertexbase+tri_indices[2]*stride);
  128. triangle[2].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
  129. callback->internalProcessTriangleIndex(triangle,part,gfxindex);
  130. }
  131. break;
  132. }
  133. case PHY_UCHAR:
  134. {
  135. for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
  136. {
  137. unsigned char* tri_indices= (unsigned char*)(indexbase+gfxindex*indexstride);
  138. graphicsbase = (double*)(vertexbase+tri_indices[0]*stride);
  139. triangle[0].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(),(btScalar)graphicsbase[2]*meshScaling.getZ());
  140. graphicsbase = (double*)(vertexbase+tri_indices[1]*stride);
  141. triangle[1].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
  142. graphicsbase = (double*)(vertexbase+tri_indices[2]*stride);
  143. triangle[2].setValue((btScalar)graphicsbase[0]*meshScaling.getX(),(btScalar)graphicsbase[1]*meshScaling.getY(), (btScalar)graphicsbase[2]*meshScaling.getZ());
  144. callback->internalProcessTriangleIndex(triangle,part,gfxindex);
  145. }
  146. break;
  147. }
  148. default:
  149. btAssert((gfxindextype == PHY_INTEGER) || (gfxindextype == PHY_SHORT));
  150. }
  151. break;
  152. }
  153. default:
  154. btAssert((type == PHY_FLOAT) || (type == PHY_DOUBLE));
  155. }
  156. unLockReadOnlyVertexBase(part);
  157. }
  158. }
  159. void btStridingMeshInterface::calculateAabbBruteForce(btVector3& aabbMin,btVector3& aabbMax)
  160. {
  161. struct AabbCalculationCallback : public btInternalTriangleIndexCallback
  162. {
  163. btVector3 m_aabbMin;
  164. btVector3 m_aabbMax;
  165. AabbCalculationCallback()
  166. {
  167. m_aabbMin.setValue(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT));
  168. m_aabbMax.setValue(btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT));
  169. }
  170. virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int triangleIndex)
  171. {
  172. (void)partId;
  173. (void)triangleIndex;
  174. m_aabbMin.setMin(triangle[0]);
  175. m_aabbMax.setMax(triangle[0]);
  176. m_aabbMin.setMin(triangle[1]);
  177. m_aabbMax.setMax(triangle[1]);
  178. m_aabbMin.setMin(triangle[2]);
  179. m_aabbMax.setMax(triangle[2]);
  180. }
  181. };
  182. //first calculate the total aabb for all triangles
  183. AabbCalculationCallback aabbCallback;
  184. aabbMin.setValue(btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT));
  185. aabbMax.setValue(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT));
  186. InternalProcessAllTriangles(&aabbCallback,aabbMin,aabbMax);
  187. aabbMin = aabbCallback.m_aabbMin;
  188. aabbMax = aabbCallback.m_aabbMax;
  189. }
  190. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  191. const char* btStridingMeshInterface::serialize(void* dataBuffer, btSerializer* serializer) const
  192. {
  193. btStridingMeshInterfaceData* trimeshData = (btStridingMeshInterfaceData*) dataBuffer;
  194. trimeshData->m_numMeshParts = getNumSubParts();
  195. //void* uniquePtr = 0;
  196. trimeshData->m_meshPartsPtr = 0;
  197. if (trimeshData->m_numMeshParts)
  198. {
  199. btChunk* chunk = serializer->allocate(sizeof(btMeshPartData),trimeshData->m_numMeshParts);
  200. btMeshPartData* memPtr = (btMeshPartData*)chunk->m_oldPtr;
  201. trimeshData->m_meshPartsPtr = (btMeshPartData *)serializer->getUniquePointer(memPtr);
  202. // int numtotalphysicsverts = 0;
  203. int part,graphicssubparts = getNumSubParts();
  204. const unsigned char * vertexbase;
  205. const unsigned char * indexbase;
  206. int indexstride;
  207. PHY_ScalarType type;
  208. PHY_ScalarType gfxindextype;
  209. int stride,numverts,numtriangles;
  210. int gfxindex;
  211. // btVector3 triangle[3];
  212. // btVector3 meshScaling = getScaling();
  213. ///if the number of parts is big, the performance might drop due to the innerloop switch on indextype
  214. for (part=0;part<graphicssubparts ;part++,memPtr++)
  215. {
  216. getLockedReadOnlyVertexIndexBase(&vertexbase,numverts,type,stride,&indexbase,indexstride,numtriangles,gfxindextype,part);
  217. memPtr->m_numTriangles = numtriangles;//indices = 3*numtriangles
  218. memPtr->m_numVertices = numverts;
  219. memPtr->m_indices16 = 0;
  220. memPtr->m_indices32 = 0;
  221. memPtr->m_3indices16 = 0;
  222. memPtr->m_3indices8 = 0;
  223. memPtr->m_vertices3f = 0;
  224. memPtr->m_vertices3d = 0;
  225. switch (gfxindextype)
  226. {
  227. case PHY_INTEGER:
  228. {
  229. int numindices = numtriangles*3;
  230. if (numindices)
  231. {
  232. btChunk* chunk = serializer->allocate(sizeof(btIntIndexData),numindices);
  233. btIntIndexData* tmpIndices = (btIntIndexData*)chunk->m_oldPtr;
  234. memPtr->m_indices32 = (btIntIndexData*)serializer->getUniquePointer(tmpIndices);
  235. for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
  236. {
  237. unsigned int* tri_indices= (unsigned int*)(indexbase+gfxindex*indexstride);
  238. tmpIndices[gfxindex*3].m_value = tri_indices[0];
  239. tmpIndices[gfxindex*3+1].m_value = tri_indices[1];
  240. tmpIndices[gfxindex*3+2].m_value = tri_indices[2];
  241. }
  242. serializer->finalizeChunk(chunk,"btIntIndexData",BT_ARRAY_CODE,(void*)chunk->m_oldPtr);
  243. }
  244. break;
  245. }
  246. case PHY_SHORT:
  247. {
  248. if (numtriangles)
  249. {
  250. btChunk* chunk = serializer->allocate(sizeof(btShortIntIndexTripletData),numtriangles);
  251. btShortIntIndexTripletData* tmpIndices = (btShortIntIndexTripletData*)chunk->m_oldPtr;
  252. memPtr->m_3indices16 = (btShortIntIndexTripletData*) serializer->getUniquePointer(tmpIndices);
  253. for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
  254. {
  255. unsigned short int* tri_indices= (unsigned short int*)(indexbase+gfxindex*indexstride);
  256. tmpIndices[gfxindex].m_values[0] = tri_indices[0];
  257. tmpIndices[gfxindex].m_values[1] = tri_indices[1];
  258. tmpIndices[gfxindex].m_values[2] = tri_indices[2];
  259. }
  260. serializer->finalizeChunk(chunk,"btShortIntIndexTripletData",BT_ARRAY_CODE,(void*)chunk->m_oldPtr);
  261. }
  262. break;
  263. }
  264. case PHY_UCHAR:
  265. {
  266. if (numtriangles)
  267. {
  268. btChunk* chunk = serializer->allocate(sizeof(btCharIndexTripletData),numtriangles);
  269. btCharIndexTripletData* tmpIndices = (btCharIndexTripletData*)chunk->m_oldPtr;
  270. memPtr->m_3indices8 = (btCharIndexTripletData*) serializer->getUniquePointer(tmpIndices);
  271. for (gfxindex=0;gfxindex<numtriangles;gfxindex++)
  272. {
  273. unsigned char* tri_indices= (unsigned char*)(indexbase+gfxindex*indexstride);
  274. tmpIndices[gfxindex].m_values[0] = tri_indices[0];
  275. tmpIndices[gfxindex].m_values[1] = tri_indices[1];
  276. tmpIndices[gfxindex].m_values[2] = tri_indices[2];
  277. }
  278. serializer->finalizeChunk(chunk,"btCharIndexTripletData",BT_ARRAY_CODE,(void*)chunk->m_oldPtr);
  279. }
  280. break;
  281. }
  282. default:
  283. {
  284. btAssert(0);
  285. //unknown index type
  286. }
  287. }
  288. switch (type)
  289. {
  290. case PHY_FLOAT:
  291. {
  292. float* graphicsbase;
  293. if (numverts)
  294. {
  295. btChunk* chunk = serializer->allocate(sizeof(btVector3FloatData),numverts);
  296. btVector3FloatData* tmpVertices = (btVector3FloatData*) chunk->m_oldPtr;
  297. memPtr->m_vertices3f = (btVector3FloatData *)serializer->getUniquePointer(tmpVertices);
  298. for (int i=0;i<numverts;i++)
  299. {
  300. graphicsbase = (float*)(vertexbase+i*stride);
  301. tmpVertices[i].m_floats[0] = graphicsbase[0];
  302. tmpVertices[i].m_floats[1] = graphicsbase[1];
  303. tmpVertices[i].m_floats[2] = graphicsbase[2];
  304. }
  305. serializer->finalizeChunk(chunk,"btVector3FloatData",BT_ARRAY_CODE,(void*)chunk->m_oldPtr);
  306. }
  307. break;
  308. }
  309. case PHY_DOUBLE:
  310. {
  311. if (numverts)
  312. {
  313. btChunk* chunk = serializer->allocate(sizeof(btVector3DoubleData),numverts);
  314. btVector3DoubleData* tmpVertices = (btVector3DoubleData*) chunk->m_oldPtr;
  315. memPtr->m_vertices3d = (btVector3DoubleData *) serializer->getUniquePointer(tmpVertices);
  316. for (int i=0;i<numverts;i++)
  317. {
  318. double* graphicsbase = (double*)(vertexbase+i*stride);//for now convert to float, might leave it at double
  319. tmpVertices[i].m_floats[0] = graphicsbase[0];
  320. tmpVertices[i].m_floats[1] = graphicsbase[1];
  321. tmpVertices[i].m_floats[2] = graphicsbase[2];
  322. }
  323. serializer->finalizeChunk(chunk,"btVector3DoubleData",BT_ARRAY_CODE,(void*)chunk->m_oldPtr);
  324. }
  325. break;
  326. }
  327. default:
  328. btAssert((type == PHY_FLOAT) || (type == PHY_DOUBLE));
  329. }
  330. unLockReadOnlyVertexBase(part);
  331. }
  332. serializer->finalizeChunk(chunk,"btMeshPartData",BT_ARRAY_CODE,chunk->m_oldPtr);
  333. }
  334. m_scaling.serializeFloat(trimeshData->m_scaling);
  335. return "btStridingMeshInterfaceData";
  336. }