1 #ifndef B3_CLIP_FACES_H
2 #define B3_CLIP_FACES_H
4 #include "Bullet3Common/shared/b3Int4.h"
5 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
6 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Collidable.h"
7 #include "Bullet3Collision/BroadPhaseCollision/shared/b3Aabb.h"
8 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3BvhSubtreeInfoData.h"
9 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3QuantizedBvhNodeData.h"
10 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3ConvexPolyhedronData.h"
12 inline b3Float4 b3Lerp3(b3Float4ConstArg a, b3Float4ConstArg b, float t)
14 return b3MakeFloat4(a.x + (b.x - a.x) * t,
15 a.y + (b.y - a.y) * t,
16 a.z + (b.z - a.z) * t,
20 // Clips a face to the back of a plane, return the number of vertices out, stored in ppVtxOut
21 int clipFaceGlobal(__global const b3Float4* pVtxIn, int numVertsIn, b3Float4ConstArg planeNormalWS, float planeEqWS, __global b3Float4* ppVtxOut)
26 //double-check next test
27 // if (numVertsIn < 2)
30 b3Float4 firstVertex = pVtxIn[numVertsIn - 1];
31 b3Float4 endVertex = pVtxIn[0];
33 ds = b3Dot(planeNormalWS, firstVertex) + planeEqWS;
35 for (ve = 0; ve < numVertsIn; ve++)
37 endVertex = pVtxIn[ve];
38 de = b3Dot(planeNormalWS, endVertex) + planeEqWS;
43 // Start < 0, end < 0, so output endVertex
44 ppVtxOut[numVertsOut++] = endVertex;
48 // Start < 0, end >= 0, so output intersection
49 ppVtxOut[numVertsOut++] = b3Lerp3(firstVertex, endVertex, (ds * 1.f / (ds - de)));
56 // Start >= 0, end < 0 so output intersection and end
57 ppVtxOut[numVertsOut++] = b3Lerp3(firstVertex, endVertex, (ds * 1.f / (ds - de)));
58 ppVtxOut[numVertsOut++] = endVertex;
61 firstVertex = endVertex;
67 __kernel void clipFacesAndFindContactsKernel(__global const b3Float4* separatingNormals,
68 __global const int* hasSeparatingAxis,
69 __global b3Int4* clippingFacesOut,
70 __global b3Float4* worldVertsA1,
71 __global b3Float4* worldNormalsA1,
72 __global b3Float4* worldVertsB1,
73 __global b3Float4* worldVertsB2,
74 int vertexFaceCapacity,
77 // int i = get_global_id(0);
81 float minDist = -1e30f;
82 float maxDist = 0.02f;
86 if (hasSeparatingAxis[i])
88 // int bodyIndexA = pairs[i].x;
89 // int bodyIndexB = pairs[i].y;
91 int numLocalContactsOut = 0;
93 int capacityWorldVertsB2 = vertexFaceCapacity;
95 __global b3Float4* pVtxIn = &worldVertsB1[pairIndex * capacityWorldVertsB2];
96 __global b3Float4* pVtxOut = &worldVertsB2[pairIndex * capacityWorldVertsB2];
99 __global b3Int4* clippingFaces = clippingFacesOut;
101 int closestFaceA = clippingFaces[pairIndex].x;
102 // int closestFaceB = clippingFaces[pairIndex].y;
103 int numVertsInA = clippingFaces[pairIndex].z;
104 int numVertsInB = clippingFaces[pairIndex].w;
108 if (closestFaceA >= 0)
110 // clip polygon to back of planes of all faces of hull A that are adjacent to witness face
112 for (int e0 = 0; e0 < numVertsInA; e0++)
114 const b3Float4 aw = worldVertsA1[pairIndex * capacityWorldVertsB2 + e0];
115 const b3Float4 bw = worldVertsA1[pairIndex * capacityWorldVertsB2 + ((e0 + 1) % numVertsInA)];
116 const b3Float4 WorldEdge0 = aw - bw;
117 b3Float4 worldPlaneAnormal1 = worldNormalsA1[pairIndex];
118 b3Float4 planeNormalWS1 = -b3Cross(WorldEdge0, worldPlaneAnormal1);
119 b3Float4 worldA1 = aw;
120 float planeEqWS1 = -b3Dot(worldA1, planeNormalWS1);
121 b3Float4 planeNormalWS = planeNormalWS1;
122 float planeEqWS = planeEqWS1;
123 numVertsOut = clipFaceGlobal(pVtxIn, numVertsInB, planeNormalWS, planeEqWS, pVtxOut);
124 __global b3Float4* tmp = pVtxOut;
127 numVertsInB = numVertsOut;
131 b3Float4 planeNormalWS = worldNormalsA1[pairIndex];
132 float planeEqWS = -b3Dot(planeNormalWS, worldVertsA1[pairIndex * capacityWorldVertsB2]);
134 for (int i = 0; i < numVertsInB; i++)
136 float depth = b3Dot(planeNormalWS, pVtxIn[i]) + planeEqWS;
137 if (depth <= minDist)
142 static float maxDepth = 0.f;
143 if (depth < maxDepth)
148 printf("error at framecount %d?\n",myframecount);
150 printf("maxDepth = %f\n", maxDepth);
154 if (depth <= maxDist)
156 b3Float4 pointInWorld = pVtxIn[i];
157 pVtxOut[numLocalContactsOut++] = b3MakeFloat4(pointInWorld.x, pointInWorld.y, pointInWorld.z, depth);
161 clippingFaces[pairIndex].w = numLocalContactsOut;
164 for (int i = 0; i < numLocalContactsOut; i++)
165 pVtxIn[i] = pVtxOut[i];
167 } // if (hasSeparatingAxis[i])
171 #endif //B3_CLIP_FACES_H