sat.cl 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  1. //keep this enum in sync with the CPU version (in btCollidable.h)
  2. //written by Erwin Coumans
  3. #define SHAPE_CONVEX_HULL 3
  4. #define SHAPE_CONCAVE_TRIMESH 5
  5. #define TRIANGLE_NUM_CONVEX_FACES 5
  6. #define SHAPE_COMPOUND_OF_CONVEX_HULLS 6
  7. #define B3_MAX_STACK_DEPTH 256
  8. typedef unsigned int u32;
  9. ///keep this in sync with btCollidable.h
  10. typedef struct
  11. {
  12. union {
  13. int m_numChildShapes;
  14. int m_bvhIndex;
  15. };
  16. union
  17. {
  18. float m_radius;
  19. int m_compoundBvhIndex;
  20. };
  21. int m_shapeType;
  22. int m_shapeIndex;
  23. } btCollidableGpu;
  24. #define MAX_NUM_PARTS_IN_BITS 10
  25. ///b3QuantizedBvhNode is a compressed aabb node, 16 bytes.
  26. ///Node can be used for leafnode or internal node. Leafnodes can point to 32-bit triangle index (non-negative range).
  27. typedef struct
  28. {
  29. //12 bytes
  30. unsigned short int m_quantizedAabbMin[3];
  31. unsigned short int m_quantizedAabbMax[3];
  32. //4 bytes
  33. int m_escapeIndexOrTriangleIndex;
  34. } b3QuantizedBvhNode;
  35. typedef struct
  36. {
  37. float4 m_aabbMin;
  38. float4 m_aabbMax;
  39. float4 m_quantization;
  40. int m_numNodes;
  41. int m_numSubTrees;
  42. int m_nodeOffset;
  43. int m_subTreeOffset;
  44. } b3BvhInfo;
  45. int getTriangleIndex(const b3QuantizedBvhNode* rootNode)
  46. {
  47. unsigned int x=0;
  48. unsigned int y = (~(x&0))<<(31-MAX_NUM_PARTS_IN_BITS);
  49. // Get only the lower bits where the triangle index is stored
  50. return (rootNode->m_escapeIndexOrTriangleIndex&~(y));
  51. }
  52. int getTriangleIndexGlobal(__global const b3QuantizedBvhNode* rootNode)
  53. {
  54. unsigned int x=0;
  55. unsigned int y = (~(x&0))<<(31-MAX_NUM_PARTS_IN_BITS);
  56. // Get only the lower bits where the triangle index is stored
  57. return (rootNode->m_escapeIndexOrTriangleIndex&~(y));
  58. }
  59. int isLeafNode(const b3QuantizedBvhNode* rootNode)
  60. {
  61. //skipindex is negative (internal node), triangleindex >=0 (leafnode)
  62. return (rootNode->m_escapeIndexOrTriangleIndex >= 0)? 1 : 0;
  63. }
  64. int isLeafNodeGlobal(__global const b3QuantizedBvhNode* rootNode)
  65. {
  66. //skipindex is negative (internal node), triangleindex >=0 (leafnode)
  67. return (rootNode->m_escapeIndexOrTriangleIndex >= 0)? 1 : 0;
  68. }
  69. int getEscapeIndex(const b3QuantizedBvhNode* rootNode)
  70. {
  71. return -rootNode->m_escapeIndexOrTriangleIndex;
  72. }
  73. int getEscapeIndexGlobal(__global const b3QuantizedBvhNode* rootNode)
  74. {
  75. return -rootNode->m_escapeIndexOrTriangleIndex;
  76. }
  77. typedef struct
  78. {
  79. //12 bytes
  80. unsigned short int m_quantizedAabbMin[3];
  81. unsigned short int m_quantizedAabbMax[3];
  82. //4 bytes, points to the root of the subtree
  83. int m_rootNodeIndex;
  84. //4 bytes
  85. int m_subtreeSize;
  86. int m_padding[3];
  87. } b3BvhSubtreeInfo;
  88. typedef struct
  89. {
  90. float4 m_childPosition;
  91. float4 m_childOrientation;
  92. int m_shapeIndex;
  93. int m_unused0;
  94. int m_unused1;
  95. int m_unused2;
  96. } btGpuChildShape;
  97. typedef struct
  98. {
  99. float4 m_pos;
  100. float4 m_quat;
  101. float4 m_linVel;
  102. float4 m_angVel;
  103. u32 m_collidableIdx;
  104. float m_invMass;
  105. float m_restituitionCoeff;
  106. float m_frictionCoeff;
  107. } BodyData;
  108. typedef struct
  109. {
  110. float4 m_localCenter;
  111. float4 m_extents;
  112. float4 mC;
  113. float4 mE;
  114. float m_radius;
  115. int m_faceOffset;
  116. int m_numFaces;
  117. int m_numVertices;
  118. int m_vertexOffset;
  119. int m_uniqueEdgesOffset;
  120. int m_numUniqueEdges;
  121. int m_unused;
  122. } ConvexPolyhedronCL;
  123. typedef struct
  124. {
  125. union
  126. {
  127. float4 m_min;
  128. float m_minElems[4];
  129. int m_minIndices[4];
  130. };
  131. union
  132. {
  133. float4 m_max;
  134. float m_maxElems[4];
  135. int m_maxIndices[4];
  136. };
  137. } btAabbCL;
  138. #include "Bullet3Collision/BroadPhaseCollision/shared/b3Aabb.h"
  139. #include "Bullet3Common/shared/b3Int2.h"
  140. typedef struct
  141. {
  142. float4 m_plane;
  143. int m_indexOffset;
  144. int m_numIndices;
  145. } btGpuFace;
  146. #define make_float4 (float4)
  147. __inline
  148. float4 cross3(float4 a, float4 b)
  149. {
  150. return cross(a,b);
  151. // float4 a1 = make_float4(a.xyz,0.f);
  152. // float4 b1 = make_float4(b.xyz,0.f);
  153. // return cross(a1,b1);
  154. //float4 c = make_float4(a.y*b.z - a.z*b.y,a.z*b.x - a.x*b.z,a.x*b.y - a.y*b.x,0.f);
  155. // float4 c = make_float4(a.y*b.z - a.z*b.y,1.f,a.x*b.y - a.y*b.x,0.f);
  156. //return c;
  157. }
  158. __inline
  159. float dot3F4(float4 a, float4 b)
  160. {
  161. float4 a1 = make_float4(a.xyz,0.f);
  162. float4 b1 = make_float4(b.xyz,0.f);
  163. return dot(a1, b1);
  164. }
  165. __inline
  166. float4 fastNormalize4(float4 v)
  167. {
  168. v = make_float4(v.xyz,0.f);
  169. return fast_normalize(v);
  170. }
  171. ///////////////////////////////////////
  172. // Quaternion
  173. ///////////////////////////////////////
  174. typedef float4 Quaternion;
  175. __inline
  176. Quaternion qtMul(Quaternion a, Quaternion b);
  177. __inline
  178. Quaternion qtNormalize(Quaternion in);
  179. __inline
  180. float4 qtRotate(Quaternion q, float4 vec);
  181. __inline
  182. Quaternion qtInvert(Quaternion q);
  183. __inline
  184. Quaternion qtMul(Quaternion a, Quaternion b)
  185. {
  186. Quaternion ans;
  187. ans = cross3( a, b );
  188. ans += a.w*b+b.w*a;
  189. // ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);
  190. ans.w = a.w*b.w - dot3F4(a, b);
  191. return ans;
  192. }
  193. __inline
  194. Quaternion qtNormalize(Quaternion in)
  195. {
  196. return fastNormalize4(in);
  197. // in /= length( in );
  198. // return in;
  199. }
  200. __inline
  201. float4 qtRotate(Quaternion q, float4 vec)
  202. {
  203. Quaternion qInv = qtInvert( q );
  204. float4 vcpy = vec;
  205. vcpy.w = 0.f;
  206. float4 out = qtMul(qtMul(q,vcpy),qInv);
  207. return out;
  208. }
  209. __inline
  210. Quaternion qtInvert(Quaternion q)
  211. {
  212. return (Quaternion)(-q.xyz, q.w);
  213. }
  214. __inline
  215. float4 qtInvRotate(const Quaternion q, float4 vec)
  216. {
  217. return qtRotate( qtInvert( q ), vec );
  218. }
  219. __inline
  220. float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)
  221. {
  222. return qtRotate( *orientation, *p ) + (*translation);
  223. }
  224. __inline
  225. float4 normalize3(const float4 a)
  226. {
  227. float4 n = make_float4(a.x, a.y, a.z, 0.f);
  228. return fastNormalize4( n );
  229. }
  230. inline void projectLocal(const ConvexPolyhedronCL* hull, const float4 pos, const float4 orn,
  231. const float4* dir, const float4* vertices, float* min, float* max)
  232. {
  233. min[0] = FLT_MAX;
  234. max[0] = -FLT_MAX;
  235. int numVerts = hull->m_numVertices;
  236. const float4 localDir = qtInvRotate(orn,*dir);
  237. float offset = dot(pos,*dir);
  238. for(int i=0;i<numVerts;i++)
  239. {
  240. float dp = dot(vertices[hull->m_vertexOffset+i],localDir);
  241. if(dp < min[0])
  242. min[0] = dp;
  243. if(dp > max[0])
  244. max[0] = dp;
  245. }
  246. if(min[0]>max[0])
  247. {
  248. float tmp = min[0];
  249. min[0] = max[0];
  250. max[0] = tmp;
  251. }
  252. min[0] += offset;
  253. max[0] += offset;
  254. }
  255. inline void project(__global const ConvexPolyhedronCL* hull, const float4 pos, const float4 orn,
  256. const float4* dir, __global const float4* vertices, float* min, float* max)
  257. {
  258. min[0] = FLT_MAX;
  259. max[0] = -FLT_MAX;
  260. int numVerts = hull->m_numVertices;
  261. const float4 localDir = qtInvRotate(orn,*dir);
  262. float offset = dot(pos,*dir);
  263. for(int i=0;i<numVerts;i++)
  264. {
  265. float dp = dot(vertices[hull->m_vertexOffset+i],localDir);
  266. if(dp < min[0])
  267. min[0] = dp;
  268. if(dp > max[0])
  269. max[0] = dp;
  270. }
  271. if(min[0]>max[0])
  272. {
  273. float tmp = min[0];
  274. min[0] = max[0];
  275. max[0] = tmp;
  276. }
  277. min[0] += offset;
  278. max[0] += offset;
  279. }
  280. inline bool TestSepAxisLocalA(const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB,
  281. const float4 posA,const float4 ornA,
  282. const float4 posB,const float4 ornB,
  283. float4* sep_axis, const float4* verticesA, __global const float4* verticesB,float* depth)
  284. {
  285. float Min0,Max0;
  286. float Min1,Max1;
  287. projectLocal(hullA,posA,ornA,sep_axis,verticesA, &Min0, &Max0);
  288. project(hullB,posB,ornB, sep_axis,verticesB, &Min1, &Max1);
  289. if(Max0<Min1 || Max1<Min0)
  290. return false;
  291. float d0 = Max0 - Min1;
  292. float d1 = Max1 - Min0;
  293. *depth = d0<d1 ? d0:d1;
  294. return true;
  295. }
  296. inline bool IsAlmostZero(const float4 v)
  297. {
  298. if(fabs(v.x)>1e-6f || fabs(v.y)>1e-6f || fabs(v.z)>1e-6f)
  299. return false;
  300. return true;
  301. }
  302. bool findSeparatingAxisLocalA( const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB,
  303. const float4 posA1,
  304. const float4 ornA,
  305. const float4 posB1,
  306. const float4 ornB,
  307. const float4 DeltaC2,
  308. const float4* verticesA,
  309. const float4* uniqueEdgesA,
  310. const btGpuFace* facesA,
  311. const int* indicesA,
  312. __global const float4* verticesB,
  313. __global const float4* uniqueEdgesB,
  314. __global const btGpuFace* facesB,
  315. __global const int* indicesB,
  316. float4* sep,
  317. float* dmin)
  318. {
  319. float4 posA = posA1;
  320. posA.w = 0.f;
  321. float4 posB = posB1;
  322. posB.w = 0.f;
  323. int curPlaneTests=0;
  324. {
  325. int numFacesA = hullA->m_numFaces;
  326. // Test normals from hullA
  327. for(int i=0;i<numFacesA;i++)
  328. {
  329. const float4 normal = facesA[hullA->m_faceOffset+i].m_plane;
  330. float4 faceANormalWS = qtRotate(ornA,normal);
  331. if (dot3F4(DeltaC2,faceANormalWS)<0)
  332. faceANormalWS*=-1.f;
  333. curPlaneTests++;
  334. float d;
  335. if(!TestSepAxisLocalA( hullA, hullB, posA,ornA,posB,ornB,&faceANormalWS, verticesA, verticesB,&d))
  336. return false;
  337. if(d<*dmin)
  338. {
  339. *dmin = d;
  340. *sep = faceANormalWS;
  341. }
  342. }
  343. }
  344. if((dot3F4(-DeltaC2,*sep))>0.0f)
  345. {
  346. *sep = -(*sep);
  347. }
  348. return true;
  349. }
  350. bool findSeparatingAxisLocalB( __global const ConvexPolyhedronCL* hullA, const ConvexPolyhedronCL* hullB,
  351. const float4 posA1,
  352. const float4 ornA,
  353. const float4 posB1,
  354. const float4 ornB,
  355. const float4 DeltaC2,
  356. __global const float4* verticesA,
  357. __global const float4* uniqueEdgesA,
  358. __global const btGpuFace* facesA,
  359. __global const int* indicesA,
  360. const float4* verticesB,
  361. const float4* uniqueEdgesB,
  362. const btGpuFace* facesB,
  363. const int* indicesB,
  364. float4* sep,
  365. float* dmin)
  366. {
  367. float4 posA = posA1;
  368. posA.w = 0.f;
  369. float4 posB = posB1;
  370. posB.w = 0.f;
  371. int curPlaneTests=0;
  372. {
  373. int numFacesA = hullA->m_numFaces;
  374. // Test normals from hullA
  375. for(int i=0;i<numFacesA;i++)
  376. {
  377. const float4 normal = facesA[hullA->m_faceOffset+i].m_plane;
  378. float4 faceANormalWS = qtRotate(ornA,normal);
  379. if (dot3F4(DeltaC2,faceANormalWS)<0)
  380. faceANormalWS *= -1.f;
  381. curPlaneTests++;
  382. float d;
  383. if(!TestSepAxisLocalA( hullB, hullA, posB,ornB,posA,ornA, &faceANormalWS, verticesB,verticesA, &d))
  384. return false;
  385. if(d<*dmin)
  386. {
  387. *dmin = d;
  388. *sep = faceANormalWS;
  389. }
  390. }
  391. }
  392. if((dot3F4(-DeltaC2,*sep))>0.0f)
  393. {
  394. *sep = -(*sep);
  395. }
  396. return true;
  397. }
  398. bool findSeparatingAxisEdgeEdgeLocalA( const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB,
  399. const float4 posA1,
  400. const float4 ornA,
  401. const float4 posB1,
  402. const float4 ornB,
  403. const float4 DeltaC2,
  404. const float4* verticesA,
  405. const float4* uniqueEdgesA,
  406. const btGpuFace* facesA,
  407. const int* indicesA,
  408. __global const float4* verticesB,
  409. __global const float4* uniqueEdgesB,
  410. __global const btGpuFace* facesB,
  411. __global const int* indicesB,
  412. float4* sep,
  413. float* dmin)
  414. {
  415. float4 posA = posA1;
  416. posA.w = 0.f;
  417. float4 posB = posB1;
  418. posB.w = 0.f;
  419. int curPlaneTests=0;
  420. int curEdgeEdge = 0;
  421. // Test edges
  422. for(int e0=0;e0<hullA->m_numUniqueEdges;e0++)
  423. {
  424. const float4 edge0 = uniqueEdgesA[hullA->m_uniqueEdgesOffset+e0];
  425. float4 edge0World = qtRotate(ornA,edge0);
  426. for(int e1=0;e1<hullB->m_numUniqueEdges;e1++)
  427. {
  428. const float4 edge1 = uniqueEdgesB[hullB->m_uniqueEdgesOffset+e1];
  429. float4 edge1World = qtRotate(ornB,edge1);
  430. float4 crossje = cross3(edge0World,edge1World);
  431. curEdgeEdge++;
  432. if(!IsAlmostZero(crossje))
  433. {
  434. crossje = normalize3(crossje);
  435. if (dot3F4(DeltaC2,crossje)<0)
  436. crossje *= -1.f;
  437. float dist;
  438. bool result = true;
  439. {
  440. float Min0,Max0;
  441. float Min1,Max1;
  442. projectLocal(hullA,posA,ornA,&crossje,verticesA, &Min0, &Max0);
  443. project(hullB,posB,ornB,&crossje,verticesB, &Min1, &Max1);
  444. if(Max0<Min1 || Max1<Min0)
  445. result = false;
  446. float d0 = Max0 - Min1;
  447. float d1 = Max1 - Min0;
  448. dist = d0<d1 ? d0:d1;
  449. result = true;
  450. }
  451. if(dist<*dmin)
  452. {
  453. *dmin = dist;
  454. *sep = crossje;
  455. }
  456. }
  457. }
  458. }
  459. if((dot3F4(-DeltaC2,*sep))>0.0f)
  460. {
  461. *sep = -(*sep);
  462. }
  463. return true;
  464. }
  465. inline bool TestSepAxis(__global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB,
  466. const float4 posA,const float4 ornA,
  467. const float4 posB,const float4 ornB,
  468. float4* sep_axis, __global const float4* vertices,float* depth)
  469. {
  470. float Min0,Max0;
  471. float Min1,Max1;
  472. project(hullA,posA,ornA,sep_axis,vertices, &Min0, &Max0);
  473. project(hullB,posB,ornB, sep_axis,vertices, &Min1, &Max1);
  474. if(Max0<Min1 || Max1<Min0)
  475. return false;
  476. float d0 = Max0 - Min1;
  477. float d1 = Max1 - Min0;
  478. *depth = d0<d1 ? d0:d1;
  479. return true;
  480. }
  481. bool findSeparatingAxis( __global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB,
  482. const float4 posA1,
  483. const float4 ornA,
  484. const float4 posB1,
  485. const float4 ornB,
  486. const float4 DeltaC2,
  487. __global const float4* vertices,
  488. __global const float4* uniqueEdges,
  489. __global const btGpuFace* faces,
  490. __global const int* indices,
  491. float4* sep,
  492. float* dmin)
  493. {
  494. float4 posA = posA1;
  495. posA.w = 0.f;
  496. float4 posB = posB1;
  497. posB.w = 0.f;
  498. int curPlaneTests=0;
  499. {
  500. int numFacesA = hullA->m_numFaces;
  501. // Test normals from hullA
  502. for(int i=0;i<numFacesA;i++)
  503. {
  504. const float4 normal = faces[hullA->m_faceOffset+i].m_plane;
  505. float4 faceANormalWS = qtRotate(ornA,normal);
  506. if (dot3F4(DeltaC2,faceANormalWS)<0)
  507. faceANormalWS*=-1.f;
  508. curPlaneTests++;
  509. float d;
  510. if(!TestSepAxis( hullA, hullB, posA,ornA,posB,ornB,&faceANormalWS, vertices,&d))
  511. return false;
  512. if(d<*dmin)
  513. {
  514. *dmin = d;
  515. *sep = faceANormalWS;
  516. }
  517. }
  518. }
  519. if((dot3F4(-DeltaC2,*sep))>0.0f)
  520. {
  521. *sep = -(*sep);
  522. }
  523. return true;
  524. }
  525. bool findSeparatingAxisUnitSphere( __global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB,
  526. const float4 posA1,
  527. const float4 ornA,
  528. const float4 posB1,
  529. const float4 ornB,
  530. const float4 DeltaC2,
  531. __global const float4* vertices,
  532. __global const float4* unitSphereDirections,
  533. int numUnitSphereDirections,
  534. float4* sep,
  535. float* dmin)
  536. {
  537. float4 posA = posA1;
  538. posA.w = 0.f;
  539. float4 posB = posB1;
  540. posB.w = 0.f;
  541. int curPlaneTests=0;
  542. int curEdgeEdge = 0;
  543. // Test unit sphere directions
  544. for (int i=0;i<numUnitSphereDirections;i++)
  545. {
  546. float4 crossje;
  547. crossje = unitSphereDirections[i];
  548. if (dot3F4(DeltaC2,crossje)>0)
  549. crossje *= -1.f;
  550. {
  551. float dist;
  552. bool result = true;
  553. float Min0,Max0;
  554. float Min1,Max1;
  555. project(hullA,posA,ornA,&crossje,vertices, &Min0, &Max0);
  556. project(hullB,posB,ornB,&crossje,vertices, &Min1, &Max1);
  557. if(Max0<Min1 || Max1<Min0)
  558. return false;
  559. float d0 = Max0 - Min1;
  560. float d1 = Max1 - Min0;
  561. dist = d0<d1 ? d0:d1;
  562. result = true;
  563. if(dist<*dmin)
  564. {
  565. *dmin = dist;
  566. *sep = crossje;
  567. }
  568. }
  569. }
  570. if((dot3F4(-DeltaC2,*sep))>0.0f)
  571. {
  572. *sep = -(*sep);
  573. }
  574. return true;
  575. }
  576. bool findSeparatingAxisEdgeEdge( __global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB,
  577. const float4 posA1,
  578. const float4 ornA,
  579. const float4 posB1,
  580. const float4 ornB,
  581. const float4 DeltaC2,
  582. __global const float4* vertices,
  583. __global const float4* uniqueEdges,
  584. __global const btGpuFace* faces,
  585. __global const int* indices,
  586. float4* sep,
  587. float* dmin)
  588. {
  589. float4 posA = posA1;
  590. posA.w = 0.f;
  591. float4 posB = posB1;
  592. posB.w = 0.f;
  593. int curPlaneTests=0;
  594. int curEdgeEdge = 0;
  595. // Test edges
  596. for(int e0=0;e0<hullA->m_numUniqueEdges;e0++)
  597. {
  598. const float4 edge0 = uniqueEdges[hullA->m_uniqueEdgesOffset+e0];
  599. float4 edge0World = qtRotate(ornA,edge0);
  600. for(int e1=0;e1<hullB->m_numUniqueEdges;e1++)
  601. {
  602. const float4 edge1 = uniqueEdges[hullB->m_uniqueEdgesOffset+e1];
  603. float4 edge1World = qtRotate(ornB,edge1);
  604. float4 crossje = cross3(edge0World,edge1World);
  605. curEdgeEdge++;
  606. if(!IsAlmostZero(crossje))
  607. {
  608. crossje = normalize3(crossje);
  609. if (dot3F4(DeltaC2,crossje)<0)
  610. crossje*=-1.f;
  611. float dist;
  612. bool result = true;
  613. {
  614. float Min0,Max0;
  615. float Min1,Max1;
  616. project(hullA,posA,ornA,&crossje,vertices, &Min0, &Max0);
  617. project(hullB,posB,ornB,&crossje,vertices, &Min1, &Max1);
  618. if(Max0<Min1 || Max1<Min0)
  619. return false;
  620. float d0 = Max0 - Min1;
  621. float d1 = Max1 - Min0;
  622. dist = d0<d1 ? d0:d1;
  623. result = true;
  624. }
  625. if(dist<*dmin)
  626. {
  627. *dmin = dist;
  628. *sep = crossje;
  629. }
  630. }
  631. }
  632. }
  633. if((dot3F4(-DeltaC2,*sep))>0.0f)
  634. {
  635. *sep = -(*sep);
  636. }
  637. return true;
  638. }
  639. // work-in-progress
  640. __kernel void processCompoundPairsKernel( __global const int4* gpuCompoundPairs,
  641. __global const BodyData* rigidBodies,
  642. __global const btCollidableGpu* collidables,
  643. __global const ConvexPolyhedronCL* convexShapes,
  644. __global const float4* vertices,
  645. __global const float4* uniqueEdges,
  646. __global const btGpuFace* faces,
  647. __global const int* indices,
  648. __global btAabbCL* aabbs,
  649. __global const btGpuChildShape* gpuChildShapes,
  650. __global volatile float4* gpuCompoundSepNormalsOut,
  651. __global volatile int* gpuHasCompoundSepNormalsOut,
  652. int numCompoundPairs
  653. )
  654. {
  655. int i = get_global_id(0);
  656. if (i<numCompoundPairs)
  657. {
  658. int bodyIndexA = gpuCompoundPairs[i].x;
  659. int bodyIndexB = gpuCompoundPairs[i].y;
  660. int childShapeIndexA = gpuCompoundPairs[i].z;
  661. int childShapeIndexB = gpuCompoundPairs[i].w;
  662. int collidableIndexA = -1;
  663. int collidableIndexB = -1;
  664. float4 ornA = rigidBodies[bodyIndexA].m_quat;
  665. float4 posA = rigidBodies[bodyIndexA].m_pos;
  666. float4 ornB = rigidBodies[bodyIndexB].m_quat;
  667. float4 posB = rigidBodies[bodyIndexB].m_pos;
  668. if (childShapeIndexA >= 0)
  669. {
  670. collidableIndexA = gpuChildShapes[childShapeIndexA].m_shapeIndex;
  671. float4 childPosA = gpuChildShapes[childShapeIndexA].m_childPosition;
  672. float4 childOrnA = gpuChildShapes[childShapeIndexA].m_childOrientation;
  673. float4 newPosA = qtRotate(ornA,childPosA)+posA;
  674. float4 newOrnA = qtMul(ornA,childOrnA);
  675. posA = newPosA;
  676. ornA = newOrnA;
  677. } else
  678. {
  679. collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  680. }
  681. if (childShapeIndexB>=0)
  682. {
  683. collidableIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
  684. float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
  685. float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
  686. float4 newPosB = transform(&childPosB,&posB,&ornB);
  687. float4 newOrnB = qtMul(ornB,childOrnB);
  688. posB = newPosB;
  689. ornB = newOrnB;
  690. } else
  691. {
  692. collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  693. }
  694. gpuHasCompoundSepNormalsOut[i] = 0;
  695. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  696. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  697. int shapeTypeA = collidables[collidableIndexA].m_shapeType;
  698. int shapeTypeB = collidables[collidableIndexB].m_shapeType;
  699. if ((shapeTypeA != SHAPE_CONVEX_HULL) || (shapeTypeB != SHAPE_CONVEX_HULL))
  700. {
  701. return;
  702. }
  703. int hasSeparatingAxis = 5;
  704. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  705. float dmin = FLT_MAX;
  706. posA.w = 0.f;
  707. posB.w = 0.f;
  708. float4 c0local = convexShapes[shapeIndexA].m_localCenter;
  709. float4 c0 = transform(&c0local, &posA, &ornA);
  710. float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  711. float4 c1 = transform(&c1local,&posB,&ornB);
  712. const float4 DeltaC2 = c0 - c1;
  713. float4 sepNormal = make_float4(1,0,0,0);
  714. bool sepA = findSeparatingAxis( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,posB,ornB,DeltaC2,vertices,uniqueEdges,faces,indices,&sepNormal,&dmin);
  715. hasSeparatingAxis = 4;
  716. if (!sepA)
  717. {
  718. hasSeparatingAxis = 0;
  719. } else
  720. {
  721. bool sepB = findSeparatingAxis( &convexShapes[shapeIndexB],&convexShapes[shapeIndexA],posB,ornB,posA,ornA,DeltaC2,vertices,uniqueEdges,faces,indices,&sepNormal,&dmin);
  722. if (!sepB)
  723. {
  724. hasSeparatingAxis = 0;
  725. } else//(!sepB)
  726. {
  727. bool sepEE = findSeparatingAxisEdgeEdge( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,posB,ornB,DeltaC2,vertices,uniqueEdges,faces,indices,&sepNormal,&dmin);
  728. if (sepEE)
  729. {
  730. gpuCompoundSepNormalsOut[i] = sepNormal;//fastNormalize4(sepNormal);
  731. gpuHasCompoundSepNormalsOut[i] = 1;
  732. }//sepEE
  733. }//(!sepB)
  734. }//(!sepA)
  735. }
  736. }
  737. inline b3Float4 MyUnQuantize(const unsigned short* vecIn, b3Float4 quantization, b3Float4 bvhAabbMin)
  738. {
  739. b3Float4 vecOut;
  740. vecOut = b3MakeFloat4(
  741. (float)(vecIn[0]) / (quantization.x),
  742. (float)(vecIn[1]) / (quantization.y),
  743. (float)(vecIn[2]) / (quantization.z),
  744. 0.f);
  745. vecOut += bvhAabbMin;
  746. return vecOut;
  747. }
  748. inline b3Float4 MyUnQuantizeGlobal(__global const unsigned short* vecIn, b3Float4 quantization, b3Float4 bvhAabbMin)
  749. {
  750. b3Float4 vecOut;
  751. vecOut = b3MakeFloat4(
  752. (float)(vecIn[0]) / (quantization.x),
  753. (float)(vecIn[1]) / (quantization.y),
  754. (float)(vecIn[2]) / (quantization.z),
  755. 0.f);
  756. vecOut += bvhAabbMin;
  757. return vecOut;
  758. }
  759. // work-in-progress
  760. __kernel void findCompoundPairsKernel( __global const int4* pairs,
  761. __global const BodyData* rigidBodies,
  762. __global const btCollidableGpu* collidables,
  763. __global const ConvexPolyhedronCL* convexShapes,
  764. __global const float4* vertices,
  765. __global const float4* uniqueEdges,
  766. __global const btGpuFace* faces,
  767. __global const int* indices,
  768. __global b3Aabb_t* aabbLocalSpace,
  769. __global const btGpuChildShape* gpuChildShapes,
  770. __global volatile int4* gpuCompoundPairsOut,
  771. __global volatile int* numCompoundPairsOut,
  772. __global const b3BvhSubtreeInfo* subtrees,
  773. __global const b3QuantizedBvhNode* quantizedNodes,
  774. __global const b3BvhInfo* bvhInfos,
  775. int numPairs,
  776. int maxNumCompoundPairsCapacity
  777. )
  778. {
  779. int i = get_global_id(0);
  780. if (i<numPairs)
  781. {
  782. int bodyIndexA = pairs[i].x;
  783. int bodyIndexB = pairs[i].y;
  784. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  785. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  786. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  787. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  788. //once the broadphase avoids static-static pairs, we can remove this test
  789. if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))
  790. {
  791. return;
  792. }
  793. if ((collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) &&(collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))
  794. {
  795. int bvhA = collidables[collidableIndexA].m_compoundBvhIndex;
  796. int bvhB = collidables[collidableIndexB].m_compoundBvhIndex;
  797. int numSubTreesA = bvhInfos[bvhA].m_numSubTrees;
  798. int subTreesOffsetA = bvhInfos[bvhA].m_subTreeOffset;
  799. int subTreesOffsetB = bvhInfos[bvhB].m_subTreeOffset;
  800. int numSubTreesB = bvhInfos[bvhB].m_numSubTrees;
  801. float4 posA = rigidBodies[bodyIndexA].m_pos;
  802. b3Quat ornA = rigidBodies[bodyIndexA].m_quat;
  803. b3Quat ornB = rigidBodies[bodyIndexB].m_quat;
  804. float4 posB = rigidBodies[bodyIndexB].m_pos;
  805. for (int p=0;p<numSubTreesA;p++)
  806. {
  807. b3BvhSubtreeInfo subtreeA = subtrees[subTreesOffsetA+p];
  808. //bvhInfos[bvhA].m_quantization
  809. b3Float4 treeAminLocal = MyUnQuantize(subtreeA.m_quantizedAabbMin,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);
  810. b3Float4 treeAmaxLocal = MyUnQuantize(subtreeA.m_quantizedAabbMax,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);
  811. b3Float4 aabbAMinOut,aabbAMaxOut;
  812. float margin=0.f;
  813. b3TransformAabb2(treeAminLocal,treeAmaxLocal, margin,posA,ornA,&aabbAMinOut,&aabbAMaxOut);
  814. for (int q=0;q<numSubTreesB;q++)
  815. {
  816. b3BvhSubtreeInfo subtreeB = subtrees[subTreesOffsetB+q];
  817. b3Float4 treeBminLocal = MyUnQuantize(subtreeB.m_quantizedAabbMin,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);
  818. b3Float4 treeBmaxLocal = MyUnQuantize(subtreeB.m_quantizedAabbMax,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);
  819. b3Float4 aabbBMinOut,aabbBMaxOut;
  820. float margin=0.f;
  821. b3TransformAabb2(treeBminLocal,treeBmaxLocal, margin,posB,ornB,&aabbBMinOut,&aabbBMaxOut);
  822. bool aabbOverlap = b3TestAabbAgainstAabb(aabbAMinOut,aabbAMaxOut,aabbBMinOut,aabbBMaxOut);
  823. if (aabbOverlap)
  824. {
  825. int startNodeIndexA = subtreeA.m_rootNodeIndex+bvhInfos[bvhA].m_nodeOffset;
  826. int endNodeIndexA = startNodeIndexA+subtreeA.m_subtreeSize;
  827. int startNodeIndexB = subtreeB.m_rootNodeIndex+bvhInfos[bvhB].m_nodeOffset;
  828. int endNodeIndexB = startNodeIndexB+subtreeB.m_subtreeSize;
  829. b3Int2 nodeStack[B3_MAX_STACK_DEPTH];
  830. b3Int2 node0;
  831. node0.x = startNodeIndexA;
  832. node0.y = startNodeIndexB;
  833. int maxStackDepth = B3_MAX_STACK_DEPTH;
  834. int depth=0;
  835. nodeStack[depth++]=node0;
  836. do
  837. {
  838. b3Int2 node = nodeStack[--depth];
  839. b3Float4 aMinLocal = MyUnQuantizeGlobal(quantizedNodes[node.x].m_quantizedAabbMin,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);
  840. b3Float4 aMaxLocal = MyUnQuantizeGlobal(quantizedNodes[node.x].m_quantizedAabbMax,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);
  841. b3Float4 bMinLocal = MyUnQuantizeGlobal(quantizedNodes[node.y].m_quantizedAabbMin,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);
  842. b3Float4 bMaxLocal = MyUnQuantizeGlobal(quantizedNodes[node.y].m_quantizedAabbMax,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);
  843. float margin=0.f;
  844. b3Float4 aabbAMinOut,aabbAMaxOut;
  845. b3TransformAabb2(aMinLocal,aMaxLocal, margin,posA,ornA,&aabbAMinOut,&aabbAMaxOut);
  846. b3Float4 aabbBMinOut,aabbBMaxOut;
  847. b3TransformAabb2(bMinLocal,bMaxLocal, margin,posB,ornB,&aabbBMinOut,&aabbBMaxOut);
  848. bool nodeOverlap = b3TestAabbAgainstAabb(aabbAMinOut,aabbAMaxOut,aabbBMinOut,aabbBMaxOut);
  849. if (nodeOverlap)
  850. {
  851. bool isLeafA = isLeafNodeGlobal(&quantizedNodes[node.x]);
  852. bool isLeafB = isLeafNodeGlobal(&quantizedNodes[node.y]);
  853. bool isInternalA = !isLeafA;
  854. bool isInternalB = !isLeafB;
  855. //fail, even though it might hit two leaf nodes
  856. if (depth+4>maxStackDepth && !(isLeafA && isLeafB))
  857. {
  858. //printf("Error: traversal exceeded maxStackDepth");
  859. continue;
  860. }
  861. if(isInternalA)
  862. {
  863. int nodeAleftChild = node.x+1;
  864. bool isNodeALeftChildLeaf = isLeafNodeGlobal(&quantizedNodes[node.x+1]);
  865. int nodeArightChild = isNodeALeftChildLeaf? node.x+2 : node.x+1 + getEscapeIndexGlobal(&quantizedNodes[node.x+1]);
  866. if(isInternalB)
  867. {
  868. int nodeBleftChild = node.y+1;
  869. bool isNodeBLeftChildLeaf = isLeafNodeGlobal(&quantizedNodes[node.y+1]);
  870. int nodeBrightChild = isNodeBLeftChildLeaf? node.y+2 : node.y+1 + getEscapeIndexGlobal(&quantizedNodes[node.y+1]);
  871. nodeStack[depth++] = b3MakeInt2(nodeAleftChild, nodeBleftChild);
  872. nodeStack[depth++] = b3MakeInt2(nodeArightChild, nodeBleftChild);
  873. nodeStack[depth++] = b3MakeInt2(nodeAleftChild, nodeBrightChild);
  874. nodeStack[depth++] = b3MakeInt2(nodeArightChild, nodeBrightChild);
  875. }
  876. else
  877. {
  878. nodeStack[depth++] = b3MakeInt2(nodeAleftChild,node.y);
  879. nodeStack[depth++] = b3MakeInt2(nodeArightChild,node.y);
  880. }
  881. }
  882. else
  883. {
  884. if(isInternalB)
  885. {
  886. int nodeBleftChild = node.y+1;
  887. bool isNodeBLeftChildLeaf = isLeafNodeGlobal(&quantizedNodes[node.y+1]);
  888. int nodeBrightChild = isNodeBLeftChildLeaf? node.y+2 : node.y+1 + getEscapeIndexGlobal(&quantizedNodes[node.y+1]);
  889. nodeStack[depth++] = b3MakeInt2(node.x,nodeBleftChild);
  890. nodeStack[depth++] = b3MakeInt2(node.x,nodeBrightChild);
  891. }
  892. else
  893. {
  894. int compoundPairIdx = atomic_inc(numCompoundPairsOut);
  895. if (compoundPairIdx<maxNumCompoundPairsCapacity)
  896. {
  897. int childShapeIndexA = getTriangleIndexGlobal(&quantizedNodes[node.x]);
  898. int childShapeIndexB = getTriangleIndexGlobal(&quantizedNodes[node.y]);
  899. gpuCompoundPairsOut[compoundPairIdx] = (int4)(bodyIndexA,bodyIndexB,childShapeIndexA,childShapeIndexB);
  900. }
  901. }
  902. }
  903. }
  904. } while (depth);
  905. }
  906. }
  907. }
  908. return;
  909. }
  910. if ((collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) ||(collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))
  911. {
  912. if (collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  913. {
  914. int numChildrenA = collidables[collidableIndexA].m_numChildShapes;
  915. for (int c=0;c<numChildrenA;c++)
  916. {
  917. int childShapeIndexA = collidables[collidableIndexA].m_shapeIndex+c;
  918. int childColIndexA = gpuChildShapes[childShapeIndexA].m_shapeIndex;
  919. float4 posA = rigidBodies[bodyIndexA].m_pos;
  920. float4 ornA = rigidBodies[bodyIndexA].m_quat;
  921. float4 childPosA = gpuChildShapes[childShapeIndexA].m_childPosition;
  922. float4 childOrnA = gpuChildShapes[childShapeIndexA].m_childOrientation;
  923. float4 newPosA = qtRotate(ornA,childPosA)+posA;
  924. float4 newOrnA = qtMul(ornA,childOrnA);
  925. int shapeIndexA = collidables[childColIndexA].m_shapeIndex;
  926. b3Aabb_t aabbAlocal = aabbLocalSpace[shapeIndexA];
  927. float margin = 0.f;
  928. b3Float4 aabbAMinWS;
  929. b3Float4 aabbAMaxWS;
  930. b3TransformAabb2(aabbAlocal.m_minVec,aabbAlocal.m_maxVec,margin,
  931. newPosA,
  932. newOrnA,
  933. &aabbAMinWS,&aabbAMaxWS);
  934. if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  935. {
  936. int numChildrenB = collidables[collidableIndexB].m_numChildShapes;
  937. for (int b=0;b<numChildrenB;b++)
  938. {
  939. int childShapeIndexB = collidables[collidableIndexB].m_shapeIndex+b;
  940. int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
  941. float4 ornB = rigidBodies[bodyIndexB].m_quat;
  942. float4 posB = rigidBodies[bodyIndexB].m_pos;
  943. float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
  944. float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
  945. float4 newPosB = transform(&childPosB,&posB,&ornB);
  946. float4 newOrnB = qtMul(ornB,childOrnB);
  947. int shapeIndexB = collidables[childColIndexB].m_shapeIndex;
  948. b3Aabb_t aabbBlocal = aabbLocalSpace[shapeIndexB];
  949. b3Float4 aabbBMinWS;
  950. b3Float4 aabbBMaxWS;
  951. b3TransformAabb2(aabbBlocal.m_minVec,aabbBlocal.m_maxVec,margin,
  952. newPosB,
  953. newOrnB,
  954. &aabbBMinWS,&aabbBMaxWS);
  955. bool aabbOverlap = b3TestAabbAgainstAabb(aabbAMinWS,aabbAMaxWS,aabbBMinWS,aabbBMaxWS);
  956. if (aabbOverlap)
  957. {
  958. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  959. float dmin = FLT_MAX;
  960. float4 posA = newPosA;
  961. posA.w = 0.f;
  962. float4 posB = newPosB;
  963. posB.w = 0.f;
  964. float4 c0local = convexShapes[shapeIndexA].m_localCenter;
  965. float4 ornA = newOrnA;
  966. float4 c0 = transform(&c0local, &posA, &ornA);
  967. float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  968. float4 ornB =newOrnB;
  969. float4 c1 = transform(&c1local,&posB,&ornB);
  970. const float4 DeltaC2 = c0 - c1;
  971. {//
  972. int compoundPairIdx = atomic_inc(numCompoundPairsOut);
  973. if (compoundPairIdx<maxNumCompoundPairsCapacity)
  974. {
  975. gpuCompoundPairsOut[compoundPairIdx] = (int4)(bodyIndexA,bodyIndexB,childShapeIndexA,childShapeIndexB);
  976. }
  977. }//
  978. }//fi(1)
  979. } //for (int b=0
  980. }//if (collidables[collidableIndexB].
  981. else//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  982. {
  983. if (1)
  984. {
  985. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  986. float dmin = FLT_MAX;
  987. float4 posA = newPosA;
  988. posA.w = 0.f;
  989. float4 posB = rigidBodies[bodyIndexB].m_pos;
  990. posB.w = 0.f;
  991. float4 c0local = convexShapes[shapeIndexA].m_localCenter;
  992. float4 ornA = newOrnA;
  993. float4 c0 = transform(&c0local, &posA, &ornA);
  994. float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  995. float4 ornB = rigidBodies[bodyIndexB].m_quat;
  996. float4 c1 = transform(&c1local,&posB,&ornB);
  997. const float4 DeltaC2 = c0 - c1;
  998. {
  999. int compoundPairIdx = atomic_inc(numCompoundPairsOut);
  1000. if (compoundPairIdx<maxNumCompoundPairsCapacity)
  1001. {
  1002. gpuCompoundPairsOut[compoundPairIdx] = (int4)(bodyIndexA,bodyIndexB,childShapeIndexA,-1);
  1003. }//if (compoundPairIdx<maxNumCompoundPairsCapacity)
  1004. }//
  1005. }//fi (1)
  1006. }//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  1007. }//for (int b=0;b<numChildrenB;b++)
  1008. return;
  1009. }//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  1010. if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONCAVE_TRIMESH)
  1011. && (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))
  1012. {
  1013. int numChildrenB = collidables[collidableIndexB].m_numChildShapes;
  1014. for (int b=0;b<numChildrenB;b++)
  1015. {
  1016. int childShapeIndexB = collidables[collidableIndexB].m_shapeIndex+b;
  1017. int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
  1018. float4 ornB = rigidBodies[bodyIndexB].m_quat;
  1019. float4 posB = rigidBodies[bodyIndexB].m_pos;
  1020. float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
  1021. float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
  1022. float4 newPosB = qtRotate(ornB,childPosB)+posB;
  1023. float4 newOrnB = qtMul(ornB,childOrnB);
  1024. int shapeIndexB = collidables[childColIndexB].m_shapeIndex;
  1025. //////////////////////////////////////
  1026. if (1)
  1027. {
  1028. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  1029. float dmin = FLT_MAX;
  1030. float4 posA = rigidBodies[bodyIndexA].m_pos;
  1031. posA.w = 0.f;
  1032. float4 posB = newPosB;
  1033. posB.w = 0.f;
  1034. float4 c0local = convexShapes[shapeIndexA].m_localCenter;
  1035. float4 ornA = rigidBodies[bodyIndexA].m_quat;
  1036. float4 c0 = transform(&c0local, &posA, &ornA);
  1037. float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  1038. float4 ornB =newOrnB;
  1039. float4 c1 = transform(&c1local,&posB,&ornB);
  1040. const float4 DeltaC2 = c0 - c1;
  1041. {//
  1042. int compoundPairIdx = atomic_inc(numCompoundPairsOut);
  1043. if (compoundPairIdx<maxNumCompoundPairsCapacity)
  1044. {
  1045. gpuCompoundPairsOut[compoundPairIdx] = (int4)(bodyIndexA,bodyIndexB,-1,childShapeIndexB);
  1046. }//fi (compoundPairIdx<maxNumCompoundPairsCapacity)
  1047. }//
  1048. }//fi (1)
  1049. }//for (int b=0;b<numChildrenB;b++)
  1050. return;
  1051. }//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  1052. return;
  1053. }//fi ((collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) ||(collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))
  1054. }//i<numPairs
  1055. }
  1056. // work-in-progress
  1057. __kernel void findSeparatingAxisKernel( __global const int4* pairs,
  1058. __global const BodyData* rigidBodies,
  1059. __global const btCollidableGpu* collidables,
  1060. __global const ConvexPolyhedronCL* convexShapes,
  1061. __global const float4* vertices,
  1062. __global const float4* uniqueEdges,
  1063. __global const btGpuFace* faces,
  1064. __global const int* indices,
  1065. __global btAabbCL* aabbs,
  1066. __global volatile float4* separatingNormals,
  1067. __global volatile int* hasSeparatingAxis,
  1068. int numPairs
  1069. )
  1070. {
  1071. int i = get_global_id(0);
  1072. if (i<numPairs)
  1073. {
  1074. int bodyIndexA = pairs[i].x;
  1075. int bodyIndexB = pairs[i].y;
  1076. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  1077. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  1078. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  1079. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  1080. //once the broadphase avoids static-static pairs, we can remove this test
  1081. if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))
  1082. {
  1083. hasSeparatingAxis[i] = 0;
  1084. return;
  1085. }
  1086. if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONVEX_HULL) ||(collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL))
  1087. {
  1088. hasSeparatingAxis[i] = 0;
  1089. return;
  1090. }
  1091. if ((collidables[collidableIndexA].m_shapeType==SHAPE_CONCAVE_TRIMESH))
  1092. {
  1093. hasSeparatingAxis[i] = 0;
  1094. return;
  1095. }
  1096. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  1097. float dmin = FLT_MAX;
  1098. float4 posA = rigidBodies[bodyIndexA].m_pos;
  1099. posA.w = 0.f;
  1100. float4 posB = rigidBodies[bodyIndexB].m_pos;
  1101. posB.w = 0.f;
  1102. float4 c0local = convexShapes[shapeIndexA].m_localCenter;
  1103. float4 ornA = rigidBodies[bodyIndexA].m_quat;
  1104. float4 c0 = transform(&c0local, &posA, &ornA);
  1105. float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  1106. float4 ornB =rigidBodies[bodyIndexB].m_quat;
  1107. float4 c1 = transform(&c1local,&posB,&ornB);
  1108. const float4 DeltaC2 = c0 - c1;
  1109. float4 sepNormal;
  1110. bool sepA = findSeparatingAxis( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,
  1111. posB,ornB,
  1112. DeltaC2,
  1113. vertices,uniqueEdges,faces,
  1114. indices,&sepNormal,&dmin);
  1115. hasSeparatingAxis[i] = 4;
  1116. if (!sepA)
  1117. {
  1118. hasSeparatingAxis[i] = 0;
  1119. } else
  1120. {
  1121. bool sepB = findSeparatingAxis( &convexShapes[shapeIndexB],&convexShapes[shapeIndexA],posB,ornB,
  1122. posA,ornA,
  1123. DeltaC2,
  1124. vertices,uniqueEdges,faces,
  1125. indices,&sepNormal,&dmin);
  1126. if (!sepB)
  1127. {
  1128. hasSeparatingAxis[i] = 0;
  1129. } else
  1130. {
  1131. bool sepEE = findSeparatingAxisEdgeEdge( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,
  1132. posB,ornB,
  1133. DeltaC2,
  1134. vertices,uniqueEdges,faces,
  1135. indices,&sepNormal,&dmin);
  1136. if (!sepEE)
  1137. {
  1138. hasSeparatingAxis[i] = 0;
  1139. } else
  1140. {
  1141. hasSeparatingAxis[i] = 1;
  1142. separatingNormals[i] = sepNormal;
  1143. }
  1144. }
  1145. }
  1146. }
  1147. }
  1148. __kernel void findSeparatingAxisVertexFaceKernel( __global const int4* pairs,
  1149. __global const BodyData* rigidBodies,
  1150. __global const btCollidableGpu* collidables,
  1151. __global const ConvexPolyhedronCL* convexShapes,
  1152. __global const float4* vertices,
  1153. __global const float4* uniqueEdges,
  1154. __global const btGpuFace* faces,
  1155. __global const int* indices,
  1156. __global btAabbCL* aabbs,
  1157. __global volatile float4* separatingNormals,
  1158. __global volatile int* hasSeparatingAxis,
  1159. __global float* dmins,
  1160. int numPairs
  1161. )
  1162. {
  1163. int i = get_global_id(0);
  1164. if (i<numPairs)
  1165. {
  1166. int bodyIndexA = pairs[i].x;
  1167. int bodyIndexB = pairs[i].y;
  1168. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  1169. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  1170. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  1171. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  1172. hasSeparatingAxis[i] = 0;
  1173. //once the broadphase avoids static-static pairs, we can remove this test
  1174. if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))
  1175. {
  1176. return;
  1177. }
  1178. if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONVEX_HULL) ||(collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL))
  1179. {
  1180. return;
  1181. }
  1182. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  1183. float dmin = FLT_MAX;
  1184. dmins[i] = dmin;
  1185. float4 posA = rigidBodies[bodyIndexA].m_pos;
  1186. posA.w = 0.f;
  1187. float4 posB = rigidBodies[bodyIndexB].m_pos;
  1188. posB.w = 0.f;
  1189. float4 c0local = convexShapes[shapeIndexA].m_localCenter;
  1190. float4 ornA = rigidBodies[bodyIndexA].m_quat;
  1191. float4 c0 = transform(&c0local, &posA, &ornA);
  1192. float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  1193. float4 ornB =rigidBodies[bodyIndexB].m_quat;
  1194. float4 c1 = transform(&c1local,&posB,&ornB);
  1195. const float4 DeltaC2 = c0 - c1;
  1196. float4 sepNormal;
  1197. bool sepA = findSeparatingAxis( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,
  1198. posB,ornB,
  1199. DeltaC2,
  1200. vertices,uniqueEdges,faces,
  1201. indices,&sepNormal,&dmin);
  1202. hasSeparatingAxis[i] = 4;
  1203. if (!sepA)
  1204. {
  1205. hasSeparatingAxis[i] = 0;
  1206. } else
  1207. {
  1208. bool sepB = findSeparatingAxis( &convexShapes[shapeIndexB],&convexShapes[shapeIndexA],posB,ornB,
  1209. posA,ornA,
  1210. DeltaC2,
  1211. vertices,uniqueEdges,faces,
  1212. indices,&sepNormal,&dmin);
  1213. if (sepB)
  1214. {
  1215. dmins[i] = dmin;
  1216. hasSeparatingAxis[i] = 1;
  1217. separatingNormals[i] = sepNormal;
  1218. }
  1219. }
  1220. }
  1221. }
  1222. __kernel void findSeparatingAxisEdgeEdgeKernel( __global const int4* pairs,
  1223. __global const BodyData* rigidBodies,
  1224. __global const btCollidableGpu* collidables,
  1225. __global const ConvexPolyhedronCL* convexShapes,
  1226. __global const float4* vertices,
  1227. __global const float4* uniqueEdges,
  1228. __global const btGpuFace* faces,
  1229. __global const int* indices,
  1230. __global btAabbCL* aabbs,
  1231. __global float4* separatingNormals,
  1232. __global int* hasSeparatingAxis,
  1233. __global float* dmins,
  1234. __global const float4* unitSphereDirections,
  1235. int numUnitSphereDirections,
  1236. int numPairs
  1237. )
  1238. {
  1239. int i = get_global_id(0);
  1240. if (i<numPairs)
  1241. {
  1242. if (hasSeparatingAxis[i])
  1243. {
  1244. int bodyIndexA = pairs[i].x;
  1245. int bodyIndexB = pairs[i].y;
  1246. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  1247. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  1248. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  1249. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  1250. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  1251. float dmin = dmins[i];
  1252. float4 posA = rigidBodies[bodyIndexA].m_pos;
  1253. posA.w = 0.f;
  1254. float4 posB = rigidBodies[bodyIndexB].m_pos;
  1255. posB.w = 0.f;
  1256. float4 c0local = convexShapes[shapeIndexA].m_localCenter;
  1257. float4 ornA = rigidBodies[bodyIndexA].m_quat;
  1258. float4 c0 = transform(&c0local, &posA, &ornA);
  1259. float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  1260. float4 ornB =rigidBodies[bodyIndexB].m_quat;
  1261. float4 c1 = transform(&c1local,&posB,&ornB);
  1262. const float4 DeltaC2 = c0 - c1;
  1263. float4 sepNormal = separatingNormals[i];
  1264. bool sepEE = false;
  1265. int numEdgeEdgeDirections = convexShapes[shapeIndexA].m_numUniqueEdges*convexShapes[shapeIndexB].m_numUniqueEdges;
  1266. if (numEdgeEdgeDirections<=numUnitSphereDirections)
  1267. {
  1268. sepEE = findSeparatingAxisEdgeEdge( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,
  1269. posB,ornB,
  1270. DeltaC2,
  1271. vertices,uniqueEdges,faces,
  1272. indices,&sepNormal,&dmin);
  1273. if (!sepEE)
  1274. {
  1275. hasSeparatingAxis[i] = 0;
  1276. } else
  1277. {
  1278. hasSeparatingAxis[i] = 1;
  1279. separatingNormals[i] = sepNormal;
  1280. }
  1281. }
  1282. /*
  1283. ///else case is a separate kernel, to make Mac OSX OpenCL compiler happy
  1284. else
  1285. {
  1286. sepEE = findSeparatingAxisUnitSphere(&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,
  1287. posB,ornB,
  1288. DeltaC2,
  1289. vertices,unitSphereDirections,numUnitSphereDirections,
  1290. &sepNormal,&dmin);
  1291. if (!sepEE)
  1292. {
  1293. hasSeparatingAxis[i] = 0;
  1294. } else
  1295. {
  1296. hasSeparatingAxis[i] = 1;
  1297. separatingNormals[i] = sepNormal;
  1298. }
  1299. }
  1300. */
  1301. } //if (hasSeparatingAxis[i])
  1302. }//(i<numPairs)
  1303. }
  1304. inline int findClippingFaces(const float4 separatingNormal,
  1305. const ConvexPolyhedronCL* hullA,
  1306. __global const ConvexPolyhedronCL* hullB,
  1307. const float4 posA, const Quaternion ornA,const float4 posB, const Quaternion ornB,
  1308. __global float4* worldVertsA1,
  1309. __global float4* worldNormalsA1,
  1310. __global float4* worldVertsB1,
  1311. int capacityWorldVerts,
  1312. const float minDist, float maxDist,
  1313. const float4* verticesA,
  1314. const btGpuFace* facesA,
  1315. const int* indicesA,
  1316. __global const float4* verticesB,
  1317. __global const btGpuFace* facesB,
  1318. __global const int* indicesB,
  1319. __global int4* clippingFaces, int pairIndex)
  1320. {
  1321. int numContactsOut = 0;
  1322. int numWorldVertsB1= 0;
  1323. int closestFaceB=0;
  1324. float dmax = -FLT_MAX;
  1325. {
  1326. for(int face=0;face<hullB->m_numFaces;face++)
  1327. {
  1328. const float4 Normal = make_float4(facesB[hullB->m_faceOffset+face].m_plane.x,
  1329. facesB[hullB->m_faceOffset+face].m_plane.y, facesB[hullB->m_faceOffset+face].m_plane.z,0.f);
  1330. const float4 WorldNormal = qtRotate(ornB, Normal);
  1331. float d = dot3F4(WorldNormal,separatingNormal);
  1332. if (d > dmax)
  1333. {
  1334. dmax = d;
  1335. closestFaceB = face;
  1336. }
  1337. }
  1338. }
  1339. {
  1340. const btGpuFace polyB = facesB[hullB->m_faceOffset+closestFaceB];
  1341. int numVertices = polyB.m_numIndices;
  1342. if (numVertices>capacityWorldVerts)
  1343. numVertices = capacityWorldVerts;
  1344. for(int e0=0;e0<numVertices;e0++)
  1345. {
  1346. if (e0<capacityWorldVerts)
  1347. {
  1348. const float4 b = verticesB[hullB->m_vertexOffset+indicesB[polyB.m_indexOffset+e0]];
  1349. worldVertsB1[pairIndex*capacityWorldVerts+numWorldVertsB1++] = transform(&b,&posB,&ornB);
  1350. }
  1351. }
  1352. }
  1353. int closestFaceA=0;
  1354. {
  1355. float dmin = FLT_MAX;
  1356. for(int face=0;face<hullA->m_numFaces;face++)
  1357. {
  1358. const float4 Normal = make_float4(
  1359. facesA[hullA->m_faceOffset+face].m_plane.x,
  1360. facesA[hullA->m_faceOffset+face].m_plane.y,
  1361. facesA[hullA->m_faceOffset+face].m_plane.z,
  1362. 0.f);
  1363. const float4 faceANormalWS = qtRotate(ornA,Normal);
  1364. float d = dot3F4(faceANormalWS,separatingNormal);
  1365. if (d < dmin)
  1366. {
  1367. dmin = d;
  1368. closestFaceA = face;
  1369. worldNormalsA1[pairIndex] = faceANormalWS;
  1370. }
  1371. }
  1372. }
  1373. int numVerticesA = facesA[hullA->m_faceOffset+closestFaceA].m_numIndices;
  1374. if (numVerticesA>capacityWorldVerts)
  1375. numVerticesA = capacityWorldVerts;
  1376. for(int e0=0;e0<numVerticesA;e0++)
  1377. {
  1378. if (e0<capacityWorldVerts)
  1379. {
  1380. const float4 a = verticesA[hullA->m_vertexOffset+indicesA[facesA[hullA->m_faceOffset+closestFaceA].m_indexOffset+e0]];
  1381. worldVertsA1[pairIndex*capacityWorldVerts+e0] = transform(&a, &posA,&ornA);
  1382. }
  1383. }
  1384. clippingFaces[pairIndex].x = closestFaceA;
  1385. clippingFaces[pairIndex].y = closestFaceB;
  1386. clippingFaces[pairIndex].z = numVerticesA;
  1387. clippingFaces[pairIndex].w = numWorldVertsB1;
  1388. return numContactsOut;
  1389. }
  1390. // work-in-progress
  1391. __kernel void findConcaveSeparatingAxisKernel( __global int4* concavePairs,
  1392. __global const BodyData* rigidBodies,
  1393. __global const btCollidableGpu* collidables,
  1394. __global const ConvexPolyhedronCL* convexShapes,
  1395. __global const float4* vertices,
  1396. __global const float4* uniqueEdges,
  1397. __global const btGpuFace* faces,
  1398. __global const int* indices,
  1399. __global const btGpuChildShape* gpuChildShapes,
  1400. __global btAabbCL* aabbs,
  1401. __global float4* concaveSeparatingNormalsOut,
  1402. __global int* concaveHasSeparatingNormals,
  1403. __global int4* clippingFacesOut,
  1404. __global float4* worldVertsA1GPU,
  1405. __global float4* worldNormalsAGPU,
  1406. __global float4* worldVertsB1GPU,
  1407. int vertexFaceCapacity,
  1408. int numConcavePairs
  1409. )
  1410. {
  1411. int i = get_global_id(0);
  1412. if (i>=numConcavePairs)
  1413. return;
  1414. concaveHasSeparatingNormals[i] = 0;
  1415. int pairIdx = i;
  1416. int bodyIndexA = concavePairs[i].x;
  1417. int bodyIndexB = concavePairs[i].y;
  1418. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  1419. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  1420. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  1421. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  1422. if (collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL&&
  1423. collidables[collidableIndexB].m_shapeType!=SHAPE_COMPOUND_OF_CONVEX_HULLS)
  1424. {
  1425. concavePairs[pairIdx].w = -1;
  1426. return;
  1427. }
  1428. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  1429. int numActualConcaveConvexTests = 0;
  1430. int f = concavePairs[i].z;
  1431. bool overlap = false;
  1432. ConvexPolyhedronCL convexPolyhedronA;
  1433. //add 3 vertices of the triangle
  1434. convexPolyhedronA.m_numVertices = 3;
  1435. convexPolyhedronA.m_vertexOffset = 0;
  1436. float4 localCenter = make_float4(0.f,0.f,0.f,0.f);
  1437. btGpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset+f];
  1438. float4 triMinAabb, triMaxAabb;
  1439. btAabbCL triAabb;
  1440. triAabb.m_min = make_float4(1e30f,1e30f,1e30f,0.f);
  1441. triAabb.m_max = make_float4(-1e30f,-1e30f,-1e30f,0.f);
  1442. float4 verticesA[3];
  1443. for (int i=0;i<3;i++)
  1444. {
  1445. int index = indices[face.m_indexOffset+i];
  1446. float4 vert = vertices[convexShapes[shapeIndexA].m_vertexOffset+index];
  1447. verticesA[i] = vert;
  1448. localCenter += vert;
  1449. triAabb.m_min = min(triAabb.m_min,vert);
  1450. triAabb.m_max = max(triAabb.m_max,vert);
  1451. }
  1452. overlap = true;
  1453. overlap = (triAabb.m_min.x > aabbs[bodyIndexB].m_max.x || triAabb.m_max.x < aabbs[bodyIndexB].m_min.x) ? false : overlap;
  1454. overlap = (triAabb.m_min.z > aabbs[bodyIndexB].m_max.z || triAabb.m_max.z < aabbs[bodyIndexB].m_min.z) ? false : overlap;
  1455. overlap = (triAabb.m_min.y > aabbs[bodyIndexB].m_max.y || triAabb.m_max.y < aabbs[bodyIndexB].m_min.y) ? false : overlap;
  1456. if (overlap)
  1457. {
  1458. float dmin = FLT_MAX;
  1459. int hasSeparatingAxis=5;
  1460. float4 sepAxis=make_float4(1,2,3,4);
  1461. int localCC=0;
  1462. numActualConcaveConvexTests++;
  1463. //a triangle has 3 unique edges
  1464. convexPolyhedronA.m_numUniqueEdges = 3;
  1465. convexPolyhedronA.m_uniqueEdgesOffset = 0;
  1466. float4 uniqueEdgesA[3];
  1467. uniqueEdgesA[0] = (verticesA[1]-verticesA[0]);
  1468. uniqueEdgesA[1] = (verticesA[2]-verticesA[1]);
  1469. uniqueEdgesA[2] = (verticesA[0]-verticesA[2]);
  1470. convexPolyhedronA.m_faceOffset = 0;
  1471. float4 normal = make_float4(face.m_plane.x,face.m_plane.y,face.m_plane.z,0.f);
  1472. btGpuFace facesA[TRIANGLE_NUM_CONVEX_FACES];
  1473. int indicesA[3+3+2+2+2];
  1474. int curUsedIndices=0;
  1475. int fidx=0;
  1476. //front size of triangle
  1477. {
  1478. facesA[fidx].m_indexOffset=curUsedIndices;
  1479. indicesA[0] = 0;
  1480. indicesA[1] = 1;
  1481. indicesA[2] = 2;
  1482. curUsedIndices+=3;
  1483. float c = face.m_plane.w;
  1484. facesA[fidx].m_plane.x = normal.x;
  1485. facesA[fidx].m_plane.y = normal.y;
  1486. facesA[fidx].m_plane.z = normal.z;
  1487. facesA[fidx].m_plane.w = c;
  1488. facesA[fidx].m_numIndices=3;
  1489. }
  1490. fidx++;
  1491. //back size of triangle
  1492. {
  1493. facesA[fidx].m_indexOffset=curUsedIndices;
  1494. indicesA[3]=2;
  1495. indicesA[4]=1;
  1496. indicesA[5]=0;
  1497. curUsedIndices+=3;
  1498. float c = dot(normal,verticesA[0]);
  1499. float c1 = -face.m_plane.w;
  1500. facesA[fidx].m_plane.x = -normal.x;
  1501. facesA[fidx].m_plane.y = -normal.y;
  1502. facesA[fidx].m_plane.z = -normal.z;
  1503. facesA[fidx].m_plane.w = c;
  1504. facesA[fidx].m_numIndices=3;
  1505. }
  1506. fidx++;
  1507. bool addEdgePlanes = true;
  1508. if (addEdgePlanes)
  1509. {
  1510. int numVertices=3;
  1511. int prevVertex = numVertices-1;
  1512. for (int i=0;i<numVertices;i++)
  1513. {
  1514. float4 v0 = verticesA[i];
  1515. float4 v1 = verticesA[prevVertex];
  1516. float4 edgeNormal = normalize(cross(normal,v1-v0));
  1517. float c = -dot(edgeNormal,v0);
  1518. facesA[fidx].m_numIndices = 2;
  1519. facesA[fidx].m_indexOffset=curUsedIndices;
  1520. indicesA[curUsedIndices++]=i;
  1521. indicesA[curUsedIndices++]=prevVertex;
  1522. facesA[fidx].m_plane.x = edgeNormal.x;
  1523. facesA[fidx].m_plane.y = edgeNormal.y;
  1524. facesA[fidx].m_plane.z = edgeNormal.z;
  1525. facesA[fidx].m_plane.w = c;
  1526. fidx++;
  1527. prevVertex = i;
  1528. }
  1529. }
  1530. convexPolyhedronA.m_numFaces = TRIANGLE_NUM_CONVEX_FACES;
  1531. convexPolyhedronA.m_localCenter = localCenter*(1.f/3.f);
  1532. float4 posA = rigidBodies[bodyIndexA].m_pos;
  1533. posA.w = 0.f;
  1534. float4 posB = rigidBodies[bodyIndexB].m_pos;
  1535. posB.w = 0.f;
  1536. float4 ornA = rigidBodies[bodyIndexA].m_quat;
  1537. float4 ornB =rigidBodies[bodyIndexB].m_quat;
  1538. ///////////////////
  1539. ///compound shape support
  1540. if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  1541. {
  1542. int compoundChild = concavePairs[pairIdx].w;
  1543. int childShapeIndexB = compoundChild;//collidables[collidableIndexB].m_shapeIndex+compoundChild;
  1544. int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
  1545. float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
  1546. float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
  1547. float4 newPosB = transform(&childPosB,&posB,&ornB);
  1548. float4 newOrnB = qtMul(ornB,childOrnB);
  1549. posB = newPosB;
  1550. ornB = newOrnB;
  1551. shapeIndexB = collidables[childColIndexB].m_shapeIndex;
  1552. }
  1553. //////////////////
  1554. float4 c0local = convexPolyhedronA.m_localCenter;
  1555. float4 c0 = transform(&c0local, &posA, &ornA);
  1556. float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  1557. float4 c1 = transform(&c1local,&posB,&ornB);
  1558. const float4 DeltaC2 = c0 - c1;
  1559. bool sepA = findSeparatingAxisLocalA( &convexPolyhedronA, &convexShapes[shapeIndexB],
  1560. posA,ornA,
  1561. posB,ornB,
  1562. DeltaC2,
  1563. verticesA,uniqueEdgesA,facesA,indicesA,
  1564. vertices,uniqueEdges,faces,indices,
  1565. &sepAxis,&dmin);
  1566. hasSeparatingAxis = 4;
  1567. if (!sepA)
  1568. {
  1569. hasSeparatingAxis = 0;
  1570. } else
  1571. {
  1572. bool sepB = findSeparatingAxisLocalB( &convexShapes[shapeIndexB],&convexPolyhedronA,
  1573. posB,ornB,
  1574. posA,ornA,
  1575. DeltaC2,
  1576. vertices,uniqueEdges,faces,indices,
  1577. verticesA,uniqueEdgesA,facesA,indicesA,
  1578. &sepAxis,&dmin);
  1579. if (!sepB)
  1580. {
  1581. hasSeparatingAxis = 0;
  1582. } else
  1583. {
  1584. bool sepEE = findSeparatingAxisEdgeEdgeLocalA( &convexPolyhedronA, &convexShapes[shapeIndexB],
  1585. posA,ornA,
  1586. posB,ornB,
  1587. DeltaC2,
  1588. verticesA,uniqueEdgesA,facesA,indicesA,
  1589. vertices,uniqueEdges,faces,indices,
  1590. &sepAxis,&dmin);
  1591. if (!sepEE)
  1592. {
  1593. hasSeparatingAxis = 0;
  1594. } else
  1595. {
  1596. hasSeparatingAxis = 1;
  1597. }
  1598. }
  1599. }
  1600. if (hasSeparatingAxis)
  1601. {
  1602. sepAxis.w = dmin;
  1603. concaveSeparatingNormalsOut[pairIdx]=sepAxis;
  1604. concaveHasSeparatingNormals[i]=1;
  1605. float minDist = -1e30f;
  1606. float maxDist = 0.02f;
  1607. findClippingFaces(sepAxis,
  1608. &convexPolyhedronA,
  1609. &convexShapes[shapeIndexB],
  1610. posA,ornA,
  1611. posB,ornB,
  1612. worldVertsA1GPU,
  1613. worldNormalsAGPU,
  1614. worldVertsB1GPU,
  1615. vertexFaceCapacity,
  1616. minDist, maxDist,
  1617. verticesA,
  1618. facesA,
  1619. indicesA,
  1620. vertices,
  1621. faces,
  1622. indices,
  1623. clippingFacesOut, pairIdx);
  1624. } else
  1625. {
  1626. //mark this pair as in-active
  1627. concavePairs[pairIdx].w = -1;
  1628. }
  1629. }
  1630. else
  1631. {
  1632. //mark this pair as in-active
  1633. concavePairs[pairIdx].w = -1;
  1634. }
  1635. concavePairs[pairIdx].z = -1;//now z is used for existing/persistent contacts
  1636. }