satConcave.cl 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 int findClippingFaces(const float4 separatingNormal,
  466. const ConvexPolyhedronCL* hullA,
  467. __global const ConvexPolyhedronCL* hullB,
  468. const float4 posA, const Quaternion ornA,const float4 posB, const Quaternion ornB,
  469. __global float4* worldVertsA1,
  470. __global float4* worldNormalsA1,
  471. __global float4* worldVertsB1,
  472. int capacityWorldVerts,
  473. const float minDist, float maxDist,
  474. const float4* verticesA,
  475. const btGpuFace* facesA,
  476. const int* indicesA,
  477. __global const float4* verticesB,
  478. __global const btGpuFace* facesB,
  479. __global const int* indicesB,
  480. __global int4* clippingFaces, int pairIndex)
  481. {
  482. int numContactsOut = 0;
  483. int numWorldVertsB1= 0;
  484. int closestFaceB=0;
  485. float dmax = -FLT_MAX;
  486. {
  487. for(int face=0;face<hullB->m_numFaces;face++)
  488. {
  489. const float4 Normal = make_float4(facesB[hullB->m_faceOffset+face].m_plane.x,
  490. facesB[hullB->m_faceOffset+face].m_plane.y, facesB[hullB->m_faceOffset+face].m_plane.z,0.f);
  491. const float4 WorldNormal = qtRotate(ornB, Normal);
  492. float d = dot3F4(WorldNormal,separatingNormal);
  493. if (d > dmax)
  494. {
  495. dmax = d;
  496. closestFaceB = face;
  497. }
  498. }
  499. }
  500. {
  501. const btGpuFace polyB = facesB[hullB->m_faceOffset+closestFaceB];
  502. int numVertices = polyB.m_numIndices;
  503. if (numVertices>capacityWorldVerts)
  504. numVertices = capacityWorldVerts;
  505. if (numVertices<0)
  506. numVertices = 0;
  507. for(int e0=0;e0<numVertices;e0++)
  508. {
  509. if (e0<capacityWorldVerts)
  510. {
  511. const float4 b = verticesB[hullB->m_vertexOffset+indicesB[polyB.m_indexOffset+e0]];
  512. worldVertsB1[pairIndex*capacityWorldVerts+numWorldVertsB1++] = transform(&b,&posB,&ornB);
  513. }
  514. }
  515. }
  516. int closestFaceA=0;
  517. {
  518. float dmin = FLT_MAX;
  519. for(int face=0;face<hullA->m_numFaces;face++)
  520. {
  521. const float4 Normal = make_float4(
  522. facesA[hullA->m_faceOffset+face].m_plane.x,
  523. facesA[hullA->m_faceOffset+face].m_plane.y,
  524. facesA[hullA->m_faceOffset+face].m_plane.z,
  525. 0.f);
  526. const float4 faceANormalWS = qtRotate(ornA,Normal);
  527. float d = dot3F4(faceANormalWS,separatingNormal);
  528. if (d < dmin)
  529. {
  530. dmin = d;
  531. closestFaceA = face;
  532. worldNormalsA1[pairIndex] = faceANormalWS;
  533. }
  534. }
  535. }
  536. int numVerticesA = facesA[hullA->m_faceOffset+closestFaceA].m_numIndices;
  537. if (numVerticesA>capacityWorldVerts)
  538. numVerticesA = capacityWorldVerts;
  539. if (numVerticesA<0)
  540. numVerticesA=0;
  541. for(int e0=0;e0<numVerticesA;e0++)
  542. {
  543. if (e0<capacityWorldVerts)
  544. {
  545. const float4 a = verticesA[hullA->m_vertexOffset+indicesA[facesA[hullA->m_faceOffset+closestFaceA].m_indexOffset+e0]];
  546. worldVertsA1[pairIndex*capacityWorldVerts+e0] = transform(&a, &posA,&ornA);
  547. }
  548. }
  549. clippingFaces[pairIndex].x = closestFaceA;
  550. clippingFaces[pairIndex].y = closestFaceB;
  551. clippingFaces[pairIndex].z = numVerticesA;
  552. clippingFaces[pairIndex].w = numWorldVertsB1;
  553. return numContactsOut;
  554. }
  555. // work-in-progress
  556. __kernel void findConcaveSeparatingAxisVertexFaceKernel( __global int4* concavePairs,
  557. __global const BodyData* rigidBodies,
  558. __global const btCollidableGpu* collidables,
  559. __global const ConvexPolyhedronCL* convexShapes,
  560. __global const float4* vertices,
  561. __global const float4* uniqueEdges,
  562. __global const btGpuFace* faces,
  563. __global const int* indices,
  564. __global const btGpuChildShape* gpuChildShapes,
  565. __global btAabbCL* aabbs,
  566. __global float4* concaveSeparatingNormalsOut,
  567. __global int* concaveHasSeparatingNormals,
  568. __global int4* clippingFacesOut,
  569. __global float4* worldVertsA1GPU,
  570. __global float4* worldNormalsAGPU,
  571. __global float4* worldVertsB1GPU,
  572. __global float* dmins,
  573. int vertexFaceCapacity,
  574. int numConcavePairs
  575. )
  576. {
  577. int i = get_global_id(0);
  578. if (i>=numConcavePairs)
  579. return;
  580. concaveHasSeparatingNormals[i] = 0;
  581. int pairIdx = i;
  582. int bodyIndexA = concavePairs[i].x;
  583. int bodyIndexB = concavePairs[i].y;
  584. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  585. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  586. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  587. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  588. if (collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL&&
  589. collidables[collidableIndexB].m_shapeType!=SHAPE_COMPOUND_OF_CONVEX_HULLS)
  590. {
  591. concavePairs[pairIdx].w = -1;
  592. return;
  593. }
  594. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  595. int numActualConcaveConvexTests = 0;
  596. int f = concavePairs[i].z;
  597. bool overlap = false;
  598. ConvexPolyhedronCL convexPolyhedronA;
  599. //add 3 vertices of the triangle
  600. convexPolyhedronA.m_numVertices = 3;
  601. convexPolyhedronA.m_vertexOffset = 0;
  602. float4 localCenter = make_float4(0.f,0.f,0.f,0.f);
  603. btGpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset+f];
  604. float4 triMinAabb, triMaxAabb;
  605. btAabbCL triAabb;
  606. triAabb.m_min = make_float4(1e30f,1e30f,1e30f,0.f);
  607. triAabb.m_max = make_float4(-1e30f,-1e30f,-1e30f,0.f);
  608. float4 verticesA[3];
  609. for (int i=0;i<3;i++)
  610. {
  611. int index = indices[face.m_indexOffset+i];
  612. float4 vert = vertices[convexShapes[shapeIndexA].m_vertexOffset+index];
  613. verticesA[i] = vert;
  614. localCenter += vert;
  615. triAabb.m_min = min(triAabb.m_min,vert);
  616. triAabb.m_max = max(triAabb.m_max,vert);
  617. }
  618. overlap = true;
  619. overlap = (triAabb.m_min.x > aabbs[bodyIndexB].m_max.x || triAabb.m_max.x < aabbs[bodyIndexB].m_min.x) ? false : overlap;
  620. overlap = (triAabb.m_min.z > aabbs[bodyIndexB].m_max.z || triAabb.m_max.z < aabbs[bodyIndexB].m_min.z) ? false : overlap;
  621. overlap = (triAabb.m_min.y > aabbs[bodyIndexB].m_max.y || triAabb.m_max.y < aabbs[bodyIndexB].m_min.y) ? false : overlap;
  622. if (overlap)
  623. {
  624. float dmin = FLT_MAX;
  625. int hasSeparatingAxis=5;
  626. float4 sepAxis=make_float4(1,2,3,4);
  627. int localCC=0;
  628. numActualConcaveConvexTests++;
  629. //a triangle has 3 unique edges
  630. convexPolyhedronA.m_numUniqueEdges = 3;
  631. convexPolyhedronA.m_uniqueEdgesOffset = 0;
  632. float4 uniqueEdgesA[3];
  633. uniqueEdgesA[0] = (verticesA[1]-verticesA[0]);
  634. uniqueEdgesA[1] = (verticesA[2]-verticesA[1]);
  635. uniqueEdgesA[2] = (verticesA[0]-verticesA[2]);
  636. convexPolyhedronA.m_faceOffset = 0;
  637. float4 normal = make_float4(face.m_plane.x,face.m_plane.y,face.m_plane.z,0.f);
  638. btGpuFace facesA[TRIANGLE_NUM_CONVEX_FACES];
  639. int indicesA[3+3+2+2+2];
  640. int curUsedIndices=0;
  641. int fidx=0;
  642. //front size of triangle
  643. {
  644. facesA[fidx].m_indexOffset=curUsedIndices;
  645. indicesA[0] = 0;
  646. indicesA[1] = 1;
  647. indicesA[2] = 2;
  648. curUsedIndices+=3;
  649. float c = face.m_plane.w;
  650. facesA[fidx].m_plane.x = normal.x;
  651. facesA[fidx].m_plane.y = normal.y;
  652. facesA[fidx].m_plane.z = normal.z;
  653. facesA[fidx].m_plane.w = c;
  654. facesA[fidx].m_numIndices=3;
  655. }
  656. fidx++;
  657. //back size of triangle
  658. {
  659. facesA[fidx].m_indexOffset=curUsedIndices;
  660. indicesA[3]=2;
  661. indicesA[4]=1;
  662. indicesA[5]=0;
  663. curUsedIndices+=3;
  664. float c = dot(normal,verticesA[0]);
  665. float c1 = -face.m_plane.w;
  666. facesA[fidx].m_plane.x = -normal.x;
  667. facesA[fidx].m_plane.y = -normal.y;
  668. facesA[fidx].m_plane.z = -normal.z;
  669. facesA[fidx].m_plane.w = c;
  670. facesA[fidx].m_numIndices=3;
  671. }
  672. fidx++;
  673. bool addEdgePlanes = true;
  674. if (addEdgePlanes)
  675. {
  676. int numVertices=3;
  677. int prevVertex = numVertices-1;
  678. for (int i=0;i<numVertices;i++)
  679. {
  680. float4 v0 = verticesA[i];
  681. float4 v1 = verticesA[prevVertex];
  682. float4 edgeNormal = normalize(cross(normal,v1-v0));
  683. float c = -dot(edgeNormal,v0);
  684. facesA[fidx].m_numIndices = 2;
  685. facesA[fidx].m_indexOffset=curUsedIndices;
  686. indicesA[curUsedIndices++]=i;
  687. indicesA[curUsedIndices++]=prevVertex;
  688. facesA[fidx].m_plane.x = edgeNormal.x;
  689. facesA[fidx].m_plane.y = edgeNormal.y;
  690. facesA[fidx].m_plane.z = edgeNormal.z;
  691. facesA[fidx].m_plane.w = c;
  692. fidx++;
  693. prevVertex = i;
  694. }
  695. }
  696. convexPolyhedronA.m_numFaces = TRIANGLE_NUM_CONVEX_FACES;
  697. convexPolyhedronA.m_localCenter = localCenter*(1.f/3.f);
  698. float4 posA = rigidBodies[bodyIndexA].m_pos;
  699. posA.w = 0.f;
  700. float4 posB = rigidBodies[bodyIndexB].m_pos;
  701. posB.w = 0.f;
  702. float4 ornA = rigidBodies[bodyIndexA].m_quat;
  703. float4 ornB =rigidBodies[bodyIndexB].m_quat;
  704. ///////////////////
  705. ///compound shape support
  706. if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  707. {
  708. int compoundChild = concavePairs[pairIdx].w;
  709. int childShapeIndexB = compoundChild;//collidables[collidableIndexB].m_shapeIndex+compoundChild;
  710. int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
  711. float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
  712. float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
  713. float4 newPosB = transform(&childPosB,&posB,&ornB);
  714. float4 newOrnB = qtMul(ornB,childOrnB);
  715. posB = newPosB;
  716. ornB = newOrnB;
  717. shapeIndexB = collidables[childColIndexB].m_shapeIndex;
  718. }
  719. //////////////////
  720. float4 c0local = convexPolyhedronA.m_localCenter;
  721. float4 c0 = transform(&c0local, &posA, &ornA);
  722. float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  723. float4 c1 = transform(&c1local,&posB,&ornB);
  724. const float4 DeltaC2 = c0 - c1;
  725. bool sepA = findSeparatingAxisLocalA( &convexPolyhedronA, &convexShapes[shapeIndexB],
  726. posA,ornA,
  727. posB,ornB,
  728. DeltaC2,
  729. verticesA,uniqueEdgesA,facesA,indicesA,
  730. vertices,uniqueEdges,faces,indices,
  731. &sepAxis,&dmin);
  732. hasSeparatingAxis = 4;
  733. if (!sepA)
  734. {
  735. hasSeparatingAxis = 0;
  736. } else
  737. {
  738. bool sepB = findSeparatingAxisLocalB( &convexShapes[shapeIndexB],&convexPolyhedronA,
  739. posB,ornB,
  740. posA,ornA,
  741. DeltaC2,
  742. vertices,uniqueEdges,faces,indices,
  743. verticesA,uniqueEdgesA,facesA,indicesA,
  744. &sepAxis,&dmin);
  745. if (!sepB)
  746. {
  747. hasSeparatingAxis = 0;
  748. } else
  749. {
  750. hasSeparatingAxis = 1;
  751. }
  752. }
  753. if (hasSeparatingAxis)
  754. {
  755. dmins[i] = dmin;
  756. concaveSeparatingNormalsOut[pairIdx]=sepAxis;
  757. concaveHasSeparatingNormals[i]=1;
  758. } else
  759. {
  760. //mark this pair as in-active
  761. concavePairs[pairIdx].w = -1;
  762. }
  763. }
  764. else
  765. {
  766. //mark this pair as in-active
  767. concavePairs[pairIdx].w = -1;
  768. }
  769. }
  770. // work-in-progress
  771. __kernel void findConcaveSeparatingAxisEdgeEdgeKernel( __global int4* concavePairs,
  772. __global const BodyData* rigidBodies,
  773. __global const btCollidableGpu* collidables,
  774. __global const ConvexPolyhedronCL* convexShapes,
  775. __global const float4* vertices,
  776. __global const float4* uniqueEdges,
  777. __global const btGpuFace* faces,
  778. __global const int* indices,
  779. __global const btGpuChildShape* gpuChildShapes,
  780. __global btAabbCL* aabbs,
  781. __global float4* concaveSeparatingNormalsOut,
  782. __global int* concaveHasSeparatingNormals,
  783. __global int4* clippingFacesOut,
  784. __global float4* worldVertsA1GPU,
  785. __global float4* worldNormalsAGPU,
  786. __global float4* worldVertsB1GPU,
  787. __global float* dmins,
  788. int vertexFaceCapacity,
  789. int numConcavePairs
  790. )
  791. {
  792. int i = get_global_id(0);
  793. if (i>=numConcavePairs)
  794. return;
  795. if (!concaveHasSeparatingNormals[i])
  796. return;
  797. int pairIdx = i;
  798. int bodyIndexA = concavePairs[i].x;
  799. int bodyIndexB = concavePairs[i].y;
  800. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  801. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  802. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  803. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  804. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  805. int numActualConcaveConvexTests = 0;
  806. int f = concavePairs[i].z;
  807. bool overlap = false;
  808. ConvexPolyhedronCL convexPolyhedronA;
  809. //add 3 vertices of the triangle
  810. convexPolyhedronA.m_numVertices = 3;
  811. convexPolyhedronA.m_vertexOffset = 0;
  812. float4 localCenter = make_float4(0.f,0.f,0.f,0.f);
  813. btGpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset+f];
  814. float4 triMinAabb, triMaxAabb;
  815. btAabbCL triAabb;
  816. triAabb.m_min = make_float4(1e30f,1e30f,1e30f,0.f);
  817. triAabb.m_max = make_float4(-1e30f,-1e30f,-1e30f,0.f);
  818. float4 verticesA[3];
  819. for (int i=0;i<3;i++)
  820. {
  821. int index = indices[face.m_indexOffset+i];
  822. float4 vert = vertices[convexShapes[shapeIndexA].m_vertexOffset+index];
  823. verticesA[i] = vert;
  824. localCenter += vert;
  825. triAabb.m_min = min(triAabb.m_min,vert);
  826. triAabb.m_max = max(triAabb.m_max,vert);
  827. }
  828. overlap = true;
  829. overlap = (triAabb.m_min.x > aabbs[bodyIndexB].m_max.x || triAabb.m_max.x < aabbs[bodyIndexB].m_min.x) ? false : overlap;
  830. overlap = (triAabb.m_min.z > aabbs[bodyIndexB].m_max.z || triAabb.m_max.z < aabbs[bodyIndexB].m_min.z) ? false : overlap;
  831. overlap = (triAabb.m_min.y > aabbs[bodyIndexB].m_max.y || triAabb.m_max.y < aabbs[bodyIndexB].m_min.y) ? false : overlap;
  832. if (overlap)
  833. {
  834. float dmin = dmins[i];
  835. int hasSeparatingAxis=5;
  836. float4 sepAxis=make_float4(1,2,3,4);
  837. sepAxis = concaveSeparatingNormalsOut[pairIdx];
  838. int localCC=0;
  839. numActualConcaveConvexTests++;
  840. //a triangle has 3 unique edges
  841. convexPolyhedronA.m_numUniqueEdges = 3;
  842. convexPolyhedronA.m_uniqueEdgesOffset = 0;
  843. float4 uniqueEdgesA[3];
  844. uniqueEdgesA[0] = (verticesA[1]-verticesA[0]);
  845. uniqueEdgesA[1] = (verticesA[2]-verticesA[1]);
  846. uniqueEdgesA[2] = (verticesA[0]-verticesA[2]);
  847. convexPolyhedronA.m_faceOffset = 0;
  848. float4 normal = make_float4(face.m_plane.x,face.m_plane.y,face.m_plane.z,0.f);
  849. btGpuFace facesA[TRIANGLE_NUM_CONVEX_FACES];
  850. int indicesA[3+3+2+2+2];
  851. int curUsedIndices=0;
  852. int fidx=0;
  853. //front size of triangle
  854. {
  855. facesA[fidx].m_indexOffset=curUsedIndices;
  856. indicesA[0] = 0;
  857. indicesA[1] = 1;
  858. indicesA[2] = 2;
  859. curUsedIndices+=3;
  860. float c = face.m_plane.w;
  861. facesA[fidx].m_plane.x = normal.x;
  862. facesA[fidx].m_plane.y = normal.y;
  863. facesA[fidx].m_plane.z = normal.z;
  864. facesA[fidx].m_plane.w = c;
  865. facesA[fidx].m_numIndices=3;
  866. }
  867. fidx++;
  868. //back size of triangle
  869. {
  870. facesA[fidx].m_indexOffset=curUsedIndices;
  871. indicesA[3]=2;
  872. indicesA[4]=1;
  873. indicesA[5]=0;
  874. curUsedIndices+=3;
  875. float c = dot(normal,verticesA[0]);
  876. float c1 = -face.m_plane.w;
  877. facesA[fidx].m_plane.x = -normal.x;
  878. facesA[fidx].m_plane.y = -normal.y;
  879. facesA[fidx].m_plane.z = -normal.z;
  880. facesA[fidx].m_plane.w = c;
  881. facesA[fidx].m_numIndices=3;
  882. }
  883. fidx++;
  884. bool addEdgePlanes = true;
  885. if (addEdgePlanes)
  886. {
  887. int numVertices=3;
  888. int prevVertex = numVertices-1;
  889. for (int i=0;i<numVertices;i++)
  890. {
  891. float4 v0 = verticesA[i];
  892. float4 v1 = verticesA[prevVertex];
  893. float4 edgeNormal = normalize(cross(normal,v1-v0));
  894. float c = -dot(edgeNormal,v0);
  895. facesA[fidx].m_numIndices = 2;
  896. facesA[fidx].m_indexOffset=curUsedIndices;
  897. indicesA[curUsedIndices++]=i;
  898. indicesA[curUsedIndices++]=prevVertex;
  899. facesA[fidx].m_plane.x = edgeNormal.x;
  900. facesA[fidx].m_plane.y = edgeNormal.y;
  901. facesA[fidx].m_plane.z = edgeNormal.z;
  902. facesA[fidx].m_plane.w = c;
  903. fidx++;
  904. prevVertex = i;
  905. }
  906. }
  907. convexPolyhedronA.m_numFaces = TRIANGLE_NUM_CONVEX_FACES;
  908. convexPolyhedronA.m_localCenter = localCenter*(1.f/3.f);
  909. float4 posA = rigidBodies[bodyIndexA].m_pos;
  910. posA.w = 0.f;
  911. float4 posB = rigidBodies[bodyIndexB].m_pos;
  912. posB.w = 0.f;
  913. float4 ornA = rigidBodies[bodyIndexA].m_quat;
  914. float4 ornB =rigidBodies[bodyIndexB].m_quat;
  915. ///////////////////
  916. ///compound shape support
  917. if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  918. {
  919. int compoundChild = concavePairs[pairIdx].w;
  920. int childShapeIndexB = compoundChild;//collidables[collidableIndexB].m_shapeIndex+compoundChild;
  921. int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
  922. float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
  923. float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
  924. float4 newPosB = transform(&childPosB,&posB,&ornB);
  925. float4 newOrnB = qtMul(ornB,childOrnB);
  926. posB = newPosB;
  927. ornB = newOrnB;
  928. shapeIndexB = collidables[childColIndexB].m_shapeIndex;
  929. }
  930. //////////////////
  931. float4 c0local = convexPolyhedronA.m_localCenter;
  932. float4 c0 = transform(&c0local, &posA, &ornA);
  933. float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  934. float4 c1 = transform(&c1local,&posB,&ornB);
  935. const float4 DeltaC2 = c0 - c1;
  936. {
  937. bool sepEE = findSeparatingAxisEdgeEdgeLocalA( &convexPolyhedronA, &convexShapes[shapeIndexB],
  938. posA,ornA,
  939. posB,ornB,
  940. DeltaC2,
  941. verticesA,uniqueEdgesA,facesA,indicesA,
  942. vertices,uniqueEdges,faces,indices,
  943. &sepAxis,&dmin);
  944. if (!sepEE)
  945. {
  946. hasSeparatingAxis = 0;
  947. } else
  948. {
  949. hasSeparatingAxis = 1;
  950. }
  951. }
  952. if (hasSeparatingAxis)
  953. {
  954. sepAxis.w = dmin;
  955. dmins[i] = dmin;
  956. concaveSeparatingNormalsOut[pairIdx]=sepAxis;
  957. concaveHasSeparatingNormals[i]=1;
  958. float minDist = -1e30f;
  959. float maxDist = 0.02f;
  960. findClippingFaces(sepAxis,
  961. &convexPolyhedronA,
  962. &convexShapes[shapeIndexB],
  963. posA,ornA,
  964. posB,ornB,
  965. worldVertsA1GPU,
  966. worldNormalsAGPU,
  967. worldVertsB1GPU,
  968. vertexFaceCapacity,
  969. minDist, maxDist,
  970. verticesA,
  971. facesA,
  972. indicesA,
  973. vertices,
  974. faces,
  975. indices,
  976. clippingFacesOut, pairIdx);
  977. } else
  978. {
  979. //mark this pair as in-active
  980. concavePairs[pairIdx].w = -1;
  981. }
  982. }
  983. else
  984. {
  985. //mark this pair as in-active
  986. concavePairs[pairIdx].w = -1;
  987. }
  988. concavePairs[i].z = -1;//for the next stage, z is used to determine existing contact points
  989. }