implemented sphere-convex (supports edge and corner-vertex cases)
This commit is contained in:
@@ -13,6 +13,8 @@ premake4 --file=stringifyKernel.lua --kernelfile="../opencl/gpu_broadphase/kerne
|
|||||||
|
|
||||||
premake4 --file=stringifyKernel.lua --kernelfile="../opencl/gpu_sat/kernels/sat.cl" --headerfile="../opencl/gpu_sat/kernels/satKernels.h" --stringname="satKernelsCL" stringify
|
premake4 --file=stringifyKernel.lua --kernelfile="../opencl/gpu_sat/kernels/sat.cl" --headerfile="../opencl/gpu_sat/kernels/satKernels.h" --stringname="satKernelsCL" stringify
|
||||||
premake4 --file=stringifyKernel.lua --kernelfile="../opencl/gpu_sat/kernels/satClipHullContacts.cl" --headerfile="../opencl/gpu_sat/kernels/satClipHullContacts.h" --stringname="satClipKernelsCL" stringify
|
premake4 --file=stringifyKernel.lua --kernelfile="../opencl/gpu_sat/kernels/satClipHullContacts.cl" --headerfile="../opencl/gpu_sat/kernels/satClipHullContacts.h" --stringname="satClipKernelsCL" stringify
|
||||||
|
premake4 --file=stringifyKernel.lua --kernelfile="../opencl/gpu_sat/kernels/primitiveContacts.cl" --headerfile="../opencl/gpu_sat/kernels/primitiveContacts.h" --stringname="primitiveContactsKernelsCL" stringify
|
||||||
|
|
||||||
premake4 --file=stringifyKernel.lua --kernelfile="../opencl/gpu_sat/kernels/bvhTraversal.cl" --headerfile="../opencl/gpu_sat/kernels/bvhTraversal.h" --stringname="bvhTraversalKernelCL" stringify
|
premake4 --file=stringifyKernel.lua --kernelfile="../opencl/gpu_sat/kernels/bvhTraversal.cl" --headerfile="../opencl/gpu_sat/kernels/bvhTraversal.h" --stringname="bvhTraversalKernelCL" stringify
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -38,9 +38,9 @@ public:
|
|||||||
:useOpenCL(true),
|
:useOpenCL(true),
|
||||||
preferredOpenCLPlatformIndex(-1),
|
preferredOpenCLPlatformIndex(-1),
|
||||||
preferredOpenCLDeviceIndex(-1),
|
preferredOpenCLDeviceIndex(-1),
|
||||||
arraySizeX(1),
|
arraySizeX(25),
|
||||||
arraySizeY(2),
|
arraySizeY(23),
|
||||||
arraySizeZ(1),
|
arraySizeZ(23),
|
||||||
m_useConcaveMesh(false),
|
m_useConcaveMesh(false),
|
||||||
gapX(14.3),
|
gapX(14.3),
|
||||||
gapY(14.0),
|
gapY(14.0),
|
||||||
|
|||||||
@@ -32,18 +32,19 @@ void GpuSphereScene::setupScene(const ConstructionInfo& ci)
|
|||||||
int mask=1;
|
int mask=1;
|
||||||
int index=0;
|
int index=0;
|
||||||
|
|
||||||
if (0)
|
if (1)
|
||||||
{
|
{
|
||||||
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||||
btVector4 scaling(400,0.01,400,1);
|
btVector4 scaling(120,2,120,1);
|
||||||
//int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||||
btVector3 normal(0,1,0);
|
btVector3 normal(0,-1,0);
|
||||||
float constant=0.01;
|
float constant=2;
|
||||||
|
|
||||||
int colIndex = m_data->m_np->registerPlaneShape(normal,constant);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
//int colIndex = m_data->m_np->registerPlaneShape(normal,constant);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||||
btVector4 position(0,0,0,0);
|
btVector4 position(0,50,0,0);
|
||||||
btQuaternion orn(0,0,0,1);
|
//btQuaternion orn(0,0,0,1);
|
||||||
|
btQuaternion orn(btVector3(1,0,0),0.3);
|
||||||
|
|
||||||
btVector4 color(0,0,1,1);
|
btVector4 color(0,0,1,1);
|
||||||
|
|
||||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||||
@@ -67,7 +68,7 @@ void GpuSphereScene::setupScene(const ConstructionInfo& ci)
|
|||||||
prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&detailed_sphere_vertices[0],numVertices,detailed_sphere_indices,numIndices);
|
prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&detailed_sphere_vertices[0],numVertices,detailed_sphere_indices,numIndices);
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
bool usePointSprites = false;
|
bool usePointSprites = true;
|
||||||
if (usePointSprites)
|
if (usePointSprites)
|
||||||
{
|
{
|
||||||
int numVertices = sizeof(point_sphere_vertices)/strideInBytes;
|
int numVertices = sizeof(point_sphere_vertices)/strideInBytes;
|
||||||
@@ -109,7 +110,7 @@ void GpuSphereScene::setupScene(const ConstructionInfo& ci)
|
|||||||
|
|
||||||
|
|
||||||
int curColor = 0;
|
int curColor = 0;
|
||||||
float scaling[4] = {1,1,1,1};
|
|
||||||
//int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
//int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||||
int colIndex = m_data->m_np->registerSphereShape(radius);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
int colIndex = m_data->m_np->registerSphereShape(radius);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||||
for (int i=0;i<ci.arraySizeX;i++)
|
for (int i=0;i<ci.arraySizeX;i++)
|
||||||
@@ -119,18 +120,16 @@ void GpuSphereScene::setupScene(const ConstructionInfo& ci)
|
|||||||
for (int k=0;k<ci.arraySizeZ;k++)
|
for (int k=0;k<ci.arraySizeZ;k++)
|
||||||
{
|
{
|
||||||
float mass = 1.f;
|
float mass = 1.f;
|
||||||
if (j==0)
|
|
||||||
mass=0.f;
|
|
||||||
|
|
||||||
//btVector3 position((j&1)+i*2.2,2+j*2.,(j&1)+k*2.2);
|
//btVector3 position((j&1)+i*2.2,1+j*2.,(j&1)+k*2.2);
|
||||||
btVector3 position(i*2.2,2+j*4.,k*2.2);
|
btVector3 position(i*radius*3,j*radius*3,k*radius*3);
|
||||||
|
|
||||||
btQuaternion orn(0,0,0,1);
|
btQuaternion orn(0,0,0,1);
|
||||||
|
|
||||||
btVector4 color = colors[curColor];
|
btVector4 color = colors[curColor];
|
||||||
curColor++;
|
curColor++;
|
||||||
curColor&=3;
|
curColor&=3;
|
||||||
btVector4 scaling(1,1,1,1);
|
btVector4 scaling(radius,radius,radius,1);
|
||||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(prevGraphicsShapeIndex,position,orn,color,scaling);
|
int id = ci.m_instancingRenderer->registerGraphicsInstance(prevGraphicsShapeIndex,position,orn,color,scaling);
|
||||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index);
|
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index);
|
||||||
|
|
||||||
@@ -142,7 +141,7 @@ void GpuSphereScene::setupScene(const ConstructionInfo& ci)
|
|||||||
float camPos[4]={ci.arraySizeX,ci.arraySizeY/2,ci.arraySizeZ,0};
|
float camPos[4]={ci.arraySizeX,ci.arraySizeY/2,ci.arraySizeZ,0};
|
||||||
//float camPos[4]={1,12.5,1.5,0};
|
//float camPos[4]={1,12.5,1.5,0};
|
||||||
m_instancingRenderer->setCameraTargetPosition(camPos);
|
m_instancingRenderer->setCameraTargetPosition(camPos);
|
||||||
m_instancingRenderer->setCameraDistance(20);
|
m_instancingRenderer->setCameraDistance(150);
|
||||||
|
|
||||||
|
|
||||||
char msg[1024];
|
char msg[1024];
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ typedef btAlignedObjectArray<btVector3> btVertexArray;
|
|||||||
#include "../kernels/satKernels.h"
|
#include "../kernels/satKernels.h"
|
||||||
#include "../kernels/satClipHullContacts.h"
|
#include "../kernels/satClipHullContacts.h"
|
||||||
#include "../kernels/bvhTraversal.h"
|
#include "../kernels/bvhTraversal.h"
|
||||||
|
#include "../kernels/primitiveContacts.h"
|
||||||
|
|
||||||
#include "BulletGeometry/btAabbUtil2.h"
|
#include "BulletGeometry/btAabbUtil2.h"
|
||||||
|
|
||||||
@@ -117,11 +118,20 @@ m_totalContactsOut(m_context, m_queue)
|
|||||||
//cl_program bvhTraversalProg = btOpenCLUtils::compileCLProgramFromString(m_context,m_device,0,&errNum,"","opencl/gpu_sat/kernels/bvhTraversal.cl", true);
|
//cl_program bvhTraversalProg = btOpenCLUtils::compileCLProgramFromString(m_context,m_device,0,&errNum,"","opencl/gpu_sat/kernels/bvhTraversal.cl", true);
|
||||||
btAssert(errNum==CL_SUCCESS);
|
btAssert(errNum==CL_SUCCESS);
|
||||||
|
|
||||||
m_bvhTraversalKernel = btOpenCLUtils::compileCLKernelFromString(m_context, m_device,srcBvh, "bvhTraversalKernel",&errNum,bvhTraversalProg,"-g");
|
m_bvhTraversalKernel = btOpenCLUtils::compileCLKernelFromString(m_context, m_device,srcBvh, "bvhTraversalKernel",&errNum,bvhTraversalProg,"");
|
||||||
btAssert(errNum==CL_SUCCESS);
|
btAssert(errNum==CL_SUCCESS);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const char* primitiveContactsSrc = primitiveContactsKernelsCL;
|
||||||
|
cl_program primitiveContactsProg = btOpenCLUtils::compileCLProgramFromString(m_context,m_device,primitiveContactsSrc,&errNum,"","opencl/gpu_sat/kernels/primitiveContacts.cl");
|
||||||
|
btAssert(errNum==CL_SUCCESS);
|
||||||
|
|
||||||
|
m_primitiveContactsKernel = btOpenCLUtils::compileCLKernelFromString(m_context, m_device,primitiveContactsSrc, "primitiveContactsKernel",&errNum,primitiveContactsProg,"");
|
||||||
|
btAssert(errNum==CL_SUCCESS);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -148,6 +158,8 @@ GpuSatCollision::~GpuSatCollision()
|
|||||||
clReleaseKernel(m_clipFacesAndContactReductionKernel);
|
clReleaseKernel(m_clipFacesAndContactReductionKernel);
|
||||||
if (m_newContactReductionKernel)
|
if (m_newContactReductionKernel)
|
||||||
clReleaseKernel(m_newContactReductionKernel);
|
clReleaseKernel(m_newContactReductionKernel);
|
||||||
|
if (m_primitiveContactsKernel)
|
||||||
|
clReleaseKernel(m_primitiveContactsKernel);
|
||||||
|
|
||||||
if (m_clipHullHullKernel)
|
if (m_clipHullHullKernel)
|
||||||
clReleaseKernel(m_clipHullHullKernel);
|
clReleaseKernel(m_clipHullHullKernel);
|
||||||
@@ -176,6 +188,224 @@ struct MyTriangleCallback : public btNodeOverlapCallback
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define float4 btVector3
|
||||||
|
#define make_float4(x,y,z,w) btVector4(x,y,z,w)
|
||||||
|
|
||||||
|
float signedDistanceFromPointToPlane(const float4& point, const float4& planeEqn, float4* closestPointOnFace)
|
||||||
|
{
|
||||||
|
float4 n = planeEqn;
|
||||||
|
n[3] = 0.f;
|
||||||
|
float dist = dot3F4(n, point) + planeEqn[3];
|
||||||
|
*closestPointOnFace = point - dist * n;
|
||||||
|
return dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
inline bool IsPointInPolygon(const btVector3& p,
|
||||||
|
const btVector3& posConvex,
|
||||||
|
const btQuaternion& ornConvex,
|
||||||
|
const btGpuFace* face,
|
||||||
|
const btVector3* baseVertex,
|
||||||
|
const int* convexIndices,
|
||||||
|
btVector3* out)
|
||||||
|
{
|
||||||
|
btVector3 a;
|
||||||
|
btVector3 b;
|
||||||
|
btVector3 ab;
|
||||||
|
btVector3 ap;
|
||||||
|
btVector3 v;
|
||||||
|
|
||||||
|
btVector3 plane (face->m_plane[0],face->m_plane[1],face->m_plane[2]);
|
||||||
|
|
||||||
|
if (face->m_numIndices<2)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
btTransform tr;
|
||||||
|
tr.setIdentity();
|
||||||
|
tr.setOrigin(posConvex);
|
||||||
|
tr.setRotation(ornConvex);
|
||||||
|
|
||||||
|
float4 v0 = baseVertex[convexIndices[face->m_indexOffset + face->m_numIndices-1]];
|
||||||
|
btVector3 worldV0 = tr(v0);
|
||||||
|
b = worldV0;
|
||||||
|
|
||||||
|
for(unsigned i=0; i != face->m_numIndices; ++i)
|
||||||
|
{
|
||||||
|
a = b;
|
||||||
|
float4 vi = baseVertex[convexIndices[face->m_indexOffset + i]];
|
||||||
|
btVector3 worldVi = tr(vi);
|
||||||
|
b = worldVi;
|
||||||
|
ab = b-a;
|
||||||
|
ap = p-a;
|
||||||
|
v = ab.cross(plane);
|
||||||
|
|
||||||
|
if (btDot(ap, v) > 0.f)
|
||||||
|
{
|
||||||
|
btScalar ab_m2 = btDot(ab, ab);
|
||||||
|
btScalar s = ab_m2 != btScalar(0.0) ? btDot(ab, ap) / ab_m2 : btScalar(0.0);
|
||||||
|
if (s <= btScalar(0.0))
|
||||||
|
{
|
||||||
|
*out = a;
|
||||||
|
}
|
||||||
|
else if (s >= btScalar(1.0))
|
||||||
|
{
|
||||||
|
*out = b;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
out->setInterpolate3(a,b,s);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void computeContactSphereConvex(int pairIndex,
|
||||||
|
int bodyIndexA, int bodyIndexB,
|
||||||
|
int collidableIndexA, int collidableIndexB,
|
||||||
|
const btRigidBodyCL* rigidBodies,
|
||||||
|
const btCollidable* collidables,
|
||||||
|
const btConvexPolyhedronCL* convexShapes,
|
||||||
|
const btVector3* convexVertices,
|
||||||
|
const int* convexIndices,
|
||||||
|
const btGpuFace* faces,
|
||||||
|
btContact4* globalContactsOut,
|
||||||
|
int& nGlobalContactsOut,
|
||||||
|
int maxContactCapacity)
|
||||||
|
{
|
||||||
|
|
||||||
|
float radius = collidables[collidableIndexA].m_radius;
|
||||||
|
float4 spherePos1 = rigidBodies[bodyIndexA].m_pos;
|
||||||
|
btQuaternion sphereOrn = rigidBodies[bodyIndexA].m_quat;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
float4 pos = rigidBodies[bodyIndexB].m_pos;
|
||||||
|
float4 spherePos = spherePos1-pos;
|
||||||
|
btQuaternion quat = rigidBodies[bodyIndexB].m_quat;
|
||||||
|
|
||||||
|
int collidableIndex = rigidBodies[bodyIndexB].m_collidableIdx;
|
||||||
|
int shapeIndex = collidables[collidableIndex].m_shapeIndex;
|
||||||
|
int numFaces = convexShapes[shapeIndex].m_numFaces;
|
||||||
|
float4 closestPnt = make_float4(0, 0, 0, 0);
|
||||||
|
float4 hitNormalWorld = make_float4(0, 0, 0, 0);
|
||||||
|
float minDist = -1000000.f; // TODO: What is the largest/smallest float?
|
||||||
|
bool bCollide = true;
|
||||||
|
int region = -1;
|
||||||
|
for ( int f = 0; f < numFaces; f++ )
|
||||||
|
{
|
||||||
|
btGpuFace face = faces[convexShapes[shapeIndex].m_faceOffset+f];
|
||||||
|
float4 planeEqn;
|
||||||
|
float4 localPlaneNormal = make_float4(face.m_plane.x(),face.m_plane.y(),face.m_plane.z(),0.f);
|
||||||
|
float4 n1 = quatRotate(quat,localPlaneNormal);
|
||||||
|
planeEqn = n1;
|
||||||
|
planeEqn[3] = face.m_plane[3];
|
||||||
|
|
||||||
|
float4 pntReturn;
|
||||||
|
float dist = signedDistanceFromPointToPlane(spherePos, planeEqn, &pntReturn);
|
||||||
|
|
||||||
|
if ( dist > radius)
|
||||||
|
{
|
||||||
|
bCollide = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( dist > 0 )
|
||||||
|
{
|
||||||
|
//might hit an edge or vertex
|
||||||
|
btVector3 out;
|
||||||
|
bool isInPoly = IsPointInPolygon(spherePos,
|
||||||
|
pos,
|
||||||
|
quat,
|
||||||
|
&face,
|
||||||
|
&convexVertices[convexShapes[shapeIndex].m_vertexOffset],
|
||||||
|
convexIndices,
|
||||||
|
&out);
|
||||||
|
if (isInPoly)
|
||||||
|
{
|
||||||
|
if (dist>minDist)
|
||||||
|
{
|
||||||
|
minDist = dist;
|
||||||
|
closestPnt = pntReturn;
|
||||||
|
hitNormalWorld = planeEqn;
|
||||||
|
region=1;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
btVector3 tmp = spherePos-out;
|
||||||
|
btScalar l2 = tmp.length2();
|
||||||
|
if (l2<radius*radius)
|
||||||
|
{
|
||||||
|
dist = btSqrt(l2);
|
||||||
|
if (dist>minDist)
|
||||||
|
{
|
||||||
|
minDist = dist;
|
||||||
|
closestPnt = out;
|
||||||
|
hitNormalWorld = tmp/dist;
|
||||||
|
region=2;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
bCollide = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ( dist > minDist )
|
||||||
|
{
|
||||||
|
minDist = dist;
|
||||||
|
closestPnt = pntReturn;
|
||||||
|
hitNormalWorld = planeEqn;
|
||||||
|
region=3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (bCollide && minDist > -100)
|
||||||
|
{
|
||||||
|
float4 normalOnSurfaceB1 = -hitNormalWorld;
|
||||||
|
float4 pOnB1 = closestPnt+pos;
|
||||||
|
//printf("dist ,%f,",minDist);
|
||||||
|
float actualDepth = minDist-radius;
|
||||||
|
//printf("actualDepth = ,%f,", actualDepth);
|
||||||
|
//printf("normalOnSurfaceB1 = ,%f,%f,%f,", normalOnSurfaceB1.getX(),normalOnSurfaceB1.getY(),normalOnSurfaceB1.getZ());
|
||||||
|
//printf("region=,%d,\n", region);
|
||||||
|
pOnB1[3] = actualDepth;
|
||||||
|
|
||||||
|
int dstIdx;
|
||||||
|
// dstIdx = nGlobalContactsOut++;//AppendInc( nGlobalContactsOut, dstIdx );
|
||||||
|
|
||||||
|
if (nGlobalContactsOut < maxContactCapacity)
|
||||||
|
{
|
||||||
|
dstIdx=nGlobalContactsOut;
|
||||||
|
nGlobalContactsOut++;
|
||||||
|
|
||||||
|
btContact4* c = &globalContactsOut[dstIdx];
|
||||||
|
c->m_worldNormal = normalOnSurfaceB1;
|
||||||
|
c->setFrictionCoeff(0.7);
|
||||||
|
c->setRestituitionCoeff(0.f);
|
||||||
|
|
||||||
|
c->m_batchIdx = pairIndex;
|
||||||
|
c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
|
||||||
|
c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
|
||||||
|
c->m_worldPos[0] = pOnB1;
|
||||||
|
int numPoints = 1;
|
||||||
|
c->m_worldNormal[3] = numPoints;
|
||||||
|
}//if (dstIdx < numPairs)
|
||||||
|
}//if (hasCollision)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void GpuSatCollision::computeConvexConvexContactsGPUSAT( const btOpenCLArray<btInt2>* pairs, int nPairs,
|
void GpuSatCollision::computeConvexConvexContactsGPUSAT( const btOpenCLArray<btInt2>* pairs, int nPairs,
|
||||||
const btOpenCLArray<btRigidBodyCL>* bodyBuf,
|
const btOpenCLArray<btRigidBodyCL>* bodyBuf,
|
||||||
btOpenCLArray<btContact4>* contactOut, int& nContacts,
|
btOpenCLArray<btContact4>* contactOut, int& nContacts,
|
||||||
@@ -206,6 +436,113 @@ void GpuSatCollision::computeConvexConvexContactsGPUSAT( const btOpenCLArray<btI
|
|||||||
if (!nPairs)
|
if (!nPairs)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
|
//#define CHECK_ON_HOST
|
||||||
|
#ifdef CHECK_ON_HOST
|
||||||
|
btAlignedObjectArray<btYetAnotherAabb> hostAabbs;
|
||||||
|
clAabbsWS.copyToHost(hostAabbs);
|
||||||
|
btAlignedObjectArray<btInt2> hostPairs;
|
||||||
|
pairs->copyToHost(hostPairs);
|
||||||
|
|
||||||
|
btAlignedObjectArray<btRigidBodyCL> hostBodyBuf;
|
||||||
|
bodyBuf->copyToHost(hostBodyBuf);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
btAlignedObjectArray<btConvexPolyhedronCL> hostConvexData;
|
||||||
|
convexData.copyToHost(hostConvexData);
|
||||||
|
|
||||||
|
btAlignedObjectArray<btVector3> hostVertices;
|
||||||
|
gpuVertices.copyToHost(hostVertices);
|
||||||
|
|
||||||
|
btAlignedObjectArray<btVector3> hostUniqueEdges;
|
||||||
|
gpuUniqueEdges.copyToHost(hostUniqueEdges);
|
||||||
|
btAlignedObjectArray<btGpuFace> hostFaces;
|
||||||
|
gpuFaces.copyToHost(hostFaces);
|
||||||
|
btAlignedObjectArray<int> hostIndices;
|
||||||
|
gpuIndices.copyToHost(hostIndices);
|
||||||
|
btAlignedObjectArray<btCollidable> hostCollidables;
|
||||||
|
gpuCollidables.copyToHost(hostCollidables);
|
||||||
|
|
||||||
|
btAlignedObjectArray<btGpuChildShape> cpuChildShapes;
|
||||||
|
gpuChildShapes.copyToHost(cpuChildShapes);
|
||||||
|
|
||||||
|
|
||||||
|
btAlignedObjectArray<btInt4> hostTriangleConvexPairs;
|
||||||
|
|
||||||
|
btAlignedObjectArray<btContact4> hostContacts;
|
||||||
|
if (nContacts)
|
||||||
|
{
|
||||||
|
contactOut->copyToHost(hostContacts);
|
||||||
|
}
|
||||||
|
|
||||||
|
hostContacts.resize(nPairs);
|
||||||
|
|
||||||
|
for (int i=0;i<nPairs;i++)
|
||||||
|
{
|
||||||
|
int bodyIndexA = hostPairs[i].x;
|
||||||
|
int bodyIndexB = hostPairs[i].y;
|
||||||
|
int collidableIndexA = hostBodyBuf[bodyIndexA].m_collidableIdx;
|
||||||
|
int collidableIndexB = hostBodyBuf[bodyIndexB].m_collidableIdx;
|
||||||
|
|
||||||
|
if (hostCollidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
|
||||||
|
hostCollidables[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)
|
||||||
|
{
|
||||||
|
printf("sphere-convex\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hostCollidables[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&
|
||||||
|
hostCollidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
|
||||||
|
{
|
||||||
|
computeContactSphereConvex(i,bodyIndexB,bodyIndexA,collidableIndexB,collidableIndexA,&hostBodyBuf[0],
|
||||||
|
&hostCollidables[0],&hostConvexData[0],&hostVertices[0],&hostIndices[0],&hostFaces[0],&hostContacts[0],nContacts,nPairs);
|
||||||
|
//printf("convex-sphere\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nContacts)
|
||||||
|
{
|
||||||
|
hostContacts.resize(nContacts);
|
||||||
|
contactOut->copyFromHost(hostContacts);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
{
|
||||||
|
if (nPairs)
|
||||||
|
{
|
||||||
|
m_totalContactsOut.copyFromHostPointer(&nContacts,1,0,true);
|
||||||
|
|
||||||
|
BT_PROFILE("primitiveContactsKernel");
|
||||||
|
btBufferInfoCL bInfo[] = {
|
||||||
|
btBufferInfoCL( pairs->getBufferCL(), true ),
|
||||||
|
btBufferInfoCL( bodyBuf->getBufferCL(),true),
|
||||||
|
btBufferInfoCL( gpuCollidables.getBufferCL(),true),
|
||||||
|
btBufferInfoCL( convexData.getBufferCL(),true),
|
||||||
|
btBufferInfoCL( gpuVertices.getBufferCL(),true),
|
||||||
|
btBufferInfoCL( gpuUniqueEdges.getBufferCL(),true),
|
||||||
|
btBufferInfoCL( gpuFaces.getBufferCL(),true),
|
||||||
|
btBufferInfoCL( gpuIndices.getBufferCL(),true),
|
||||||
|
btBufferInfoCL( contactOut->getBufferCL()),
|
||||||
|
btBufferInfoCL( m_totalContactsOut.getBufferCL())
|
||||||
|
};
|
||||||
|
|
||||||
|
btLauncherCL launcher(m_queue, m_primitiveContactsKernel);
|
||||||
|
launcher.setBuffers( bInfo, sizeof(bInfo)/sizeof(btBufferInfoCL) );
|
||||||
|
launcher.setConst( nPairs );
|
||||||
|
int num = nPairs;
|
||||||
|
launcher.launch1D( num);
|
||||||
|
clFinish(m_queue);
|
||||||
|
|
||||||
|
nContacts = m_totalContactsOut.at(0);
|
||||||
|
contactOut->resize(nContacts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif//CHECK_ON_HOST
|
||||||
BT_PROFILE("computeConvexConvexContactsGPUSAT");
|
BT_PROFILE("computeConvexConvexContactsGPUSAT");
|
||||||
// printf("nContacts = %d\n",nContacts);
|
// printf("nContacts = %d\n",nContacts);
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ struct GpuSatCollision
|
|||||||
cl_kernel m_newContactReductionKernel;
|
cl_kernel m_newContactReductionKernel;
|
||||||
|
|
||||||
cl_kernel m_bvhTraversalKernel;
|
cl_kernel m_bvhTraversalKernel;
|
||||||
|
cl_kernel m_primitiveContactsKernel;
|
||||||
|
|
||||||
|
|
||||||
btOpenCLArray<int> m_totalContactsOut;
|
btOpenCLArray<int> m_totalContactsOut;
|
||||||
|
|||||||
667
opencl/gpu_sat/kernels/primitiveContacts.cl
Normal file
667
opencl/gpu_sat/kernels/primitiveContacts.cl
Normal file
@@ -0,0 +1,667 @@
|
|||||||
|
#define TRIANGLE_NUM_CONVEX_FACES 5
|
||||||
|
|
||||||
|
#define SHAPE_CONVEX_HULL 3
|
||||||
|
#define SHAPE_PLANE 4
|
||||||
|
#define SHAPE_CONCAVE_TRIMESH 5
|
||||||
|
#define SHAPE_COMPOUND_OF_CONVEX_HULLS 6
|
||||||
|
#define SHAPE_SPHERE 7
|
||||||
|
|
||||||
|
|
||||||
|
#pragma OPENCL EXTENSION cl_amd_printf : enable
|
||||||
|
#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
|
||||||
|
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
|
||||||
|
#pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable
|
||||||
|
#pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable
|
||||||
|
|
||||||
|
#ifdef cl_ext_atomic_counters_32
|
||||||
|
#pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable
|
||||||
|
#else
|
||||||
|
#define counter32_t volatile __global int*
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define GET_GROUP_IDX get_group_id(0)
|
||||||
|
#define GET_LOCAL_IDX get_local_id(0)
|
||||||
|
#define GET_GLOBAL_IDX get_global_id(0)
|
||||||
|
#define GET_GROUP_SIZE get_local_size(0)
|
||||||
|
#define GET_NUM_GROUPS get_num_groups(0)
|
||||||
|
#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)
|
||||||
|
#define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)
|
||||||
|
#define AtomInc(x) atom_inc(&(x))
|
||||||
|
#define AtomInc1(x, out) out = atom_inc(&(x))
|
||||||
|
#define AppendInc(x, out) out = atomic_inc(x)
|
||||||
|
#define AtomAdd(x, value) atom_add(&(x), value)
|
||||||
|
#define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )
|
||||||
|
#define AtomXhg(x, value) atom_xchg ( &(x), value )
|
||||||
|
|
||||||
|
#define max2 max
|
||||||
|
#define min2 min
|
||||||
|
|
||||||
|
typedef unsigned int u32;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
float4 m_worldPos[4];
|
||||||
|
float4 m_worldNormal; // w: m_nPoints
|
||||||
|
u32 m_coeffs;
|
||||||
|
u32 m_batchIdx;
|
||||||
|
|
||||||
|
int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr
|
||||||
|
int m_bodyBPtrAndSignBit;
|
||||||
|
} Contact4;
|
||||||
|
|
||||||
|
|
||||||
|
///keep this in sync with btCollidable.h
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int m_numChildShapes;
|
||||||
|
float m_radius;
|
||||||
|
int m_shapeType;
|
||||||
|
int m_shapeIndex;
|
||||||
|
|
||||||
|
} btCollidableGpu;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
float4 m_childPosition;
|
||||||
|
float4 m_childOrientation;
|
||||||
|
int m_shapeIndex;
|
||||||
|
int m_unused0;
|
||||||
|
int m_unused1;
|
||||||
|
int m_unused2;
|
||||||
|
} btGpuChildShape;
|
||||||
|
|
||||||
|
#define GET_NPOINTS(x) (x).m_worldNormal.w
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
float4 m_pos;
|
||||||
|
float4 m_quat;
|
||||||
|
float4 m_linVel;
|
||||||
|
float4 m_angVel;
|
||||||
|
|
||||||
|
u32 m_collidableIdx;
|
||||||
|
float m_invMass;
|
||||||
|
float m_restituitionCoeff;
|
||||||
|
float m_frictionCoeff;
|
||||||
|
} BodyData;
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
float4 m_localCenter;
|
||||||
|
float4 m_extents;
|
||||||
|
float4 mC;
|
||||||
|
float4 mE;
|
||||||
|
|
||||||
|
float m_radius;
|
||||||
|
int m_faceOffset;
|
||||||
|
int m_numFaces;
|
||||||
|
int m_numVertices;
|
||||||
|
|
||||||
|
int m_vertexOffset;
|
||||||
|
int m_uniqueEdgesOffset;
|
||||||
|
int m_numUniqueEdges;
|
||||||
|
int m_unused;
|
||||||
|
|
||||||
|
} ConvexPolyhedronCL;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
float4 m_plane;
|
||||||
|
int m_indexOffset;
|
||||||
|
int m_numIndices;
|
||||||
|
} btGpuFace;
|
||||||
|
|
||||||
|
#define SELECT_UINT4( b, a, condition ) select( b,a,condition )
|
||||||
|
|
||||||
|
#define make_float4 (float4)
|
||||||
|
#define make_float2 (float2)
|
||||||
|
#define make_uint4 (uint4)
|
||||||
|
#define make_int4 (int4)
|
||||||
|
#define make_uint2 (uint2)
|
||||||
|
#define make_int2 (int2)
|
||||||
|
|
||||||
|
|
||||||
|
__inline
|
||||||
|
float fastDiv(float numerator, float denominator)
|
||||||
|
{
|
||||||
|
return native_divide(numerator, denominator);
|
||||||
|
// return numerator/denominator;
|
||||||
|
}
|
||||||
|
|
||||||
|
__inline
|
||||||
|
float4 fastDiv4(float4 numerator, float4 denominator)
|
||||||
|
{
|
||||||
|
return native_divide(numerator, denominator);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__inline
|
||||||
|
float4 cross3(float4 a, float4 b)
|
||||||
|
{
|
||||||
|
return cross(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
//#define dot3F4 dot
|
||||||
|
|
||||||
|
__inline
|
||||||
|
float dot3F4(float4 a, float4 b)
|
||||||
|
{
|
||||||
|
float4 a1 = make_float4(a.xyz,0.f);
|
||||||
|
float4 b1 = make_float4(b.xyz,0.f);
|
||||||
|
return dot(a1, b1);
|
||||||
|
}
|
||||||
|
|
||||||
|
__inline
|
||||||
|
float4 fastNormalize4(float4 v)
|
||||||
|
{
|
||||||
|
return fast_normalize(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////
|
||||||
|
// Quaternion
|
||||||
|
///////////////////////////////////////
|
||||||
|
|
||||||
|
typedef float4 Quaternion;
|
||||||
|
|
||||||
|
__inline
|
||||||
|
Quaternion qtMul(Quaternion a, Quaternion b);
|
||||||
|
|
||||||
|
__inline
|
||||||
|
Quaternion qtNormalize(Quaternion in);
|
||||||
|
|
||||||
|
__inline
|
||||||
|
float4 qtRotate(Quaternion q, float4 vec);
|
||||||
|
|
||||||
|
__inline
|
||||||
|
Quaternion qtInvert(Quaternion q);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
__inline
|
||||||
|
Quaternion qtMul(Quaternion a, Quaternion b)
|
||||||
|
{
|
||||||
|
Quaternion ans;
|
||||||
|
ans = cross3( a, b );
|
||||||
|
ans += a.w*b+b.w*a;
|
||||||
|
// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);
|
||||||
|
ans.w = a.w*b.w - dot3F4(a, b);
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
__inline
|
||||||
|
Quaternion qtNormalize(Quaternion in)
|
||||||
|
{
|
||||||
|
return fastNormalize4(in);
|
||||||
|
// in /= length( in );
|
||||||
|
// return in;
|
||||||
|
}
|
||||||
|
__inline
|
||||||
|
float4 qtRotate(Quaternion q, float4 vec)
|
||||||
|
{
|
||||||
|
Quaternion qInv = qtInvert( q );
|
||||||
|
float4 vcpy = vec;
|
||||||
|
vcpy.w = 0.f;
|
||||||
|
float4 out = qtMul(qtMul(q,vcpy),qInv);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
__inline
|
||||||
|
Quaternion qtInvert(Quaternion q)
|
||||||
|
{
|
||||||
|
return (Quaternion)(-q.xyz, q.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
__inline
|
||||||
|
float4 qtInvRotate(const Quaternion q, float4 vec)
|
||||||
|
{
|
||||||
|
return qtRotate( qtInvert( q ), vec );
|
||||||
|
}
|
||||||
|
|
||||||
|
__inline
|
||||||
|
float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)
|
||||||
|
{
|
||||||
|
return qtRotate( *orientation, *p ) + (*translation);
|
||||||
|
}
|
||||||
|
|
||||||
|
void trInverse(float4 translationIn, Quaternion orientationIn,
|
||||||
|
float4* translationOut, Quaternion* orientationOut)
|
||||||
|
{
|
||||||
|
*orientationOut = qtInvert(orientationIn);
|
||||||
|
*translationOut = qtRotate(*orientationOut, -translationIn);
|
||||||
|
}
|
||||||
|
|
||||||
|
void trMul(float4 translationA, Quaternion orientationA,
|
||||||
|
float4 translationB, Quaternion orientationB,
|
||||||
|
float4* translationOut, Quaternion* orientationOut)
|
||||||
|
{
|
||||||
|
*orientationOut = qtMul(orientationA,orientationB);
|
||||||
|
*translationOut = transform(&translationB,&translationA,&orientationA);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
__inline
|
||||||
|
float4 normalize3(const float4 a)
|
||||||
|
{
|
||||||
|
float4 n = make_float4(a.x, a.y, a.z, 0.f);
|
||||||
|
return fastNormalize4( n );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__inline float4 lerp3(const float4 a,const float4 b, float t)
|
||||||
|
{
|
||||||
|
return make_float4( a.x + (b.x - a.x) * t,
|
||||||
|
a.y + (b.y - a.y) * t,
|
||||||
|
a.z + (b.z - a.z) * t,
|
||||||
|
0.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float signedDistanceFromPointToPlane(float4 point, float4 planeEqn, float4* closestPointOnFace)
|
||||||
|
{
|
||||||
|
float4 n = (float4)(planeEqn.x, planeEqn.y, planeEqn.z, 0);
|
||||||
|
float dist = dot3F4(n, point) + planeEqn.w;
|
||||||
|
*closestPointOnFace = point - dist * n;
|
||||||
|
return dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
inline bool IsPointInPolygon(float4 p,
|
||||||
|
float4 posConvex,
|
||||||
|
float4 ornConvex,
|
||||||
|
const btGpuFace* face,
|
||||||
|
__global const float4* baseVertex,
|
||||||
|
__global const int* convexIndices,
|
||||||
|
float4* out)
|
||||||
|
{
|
||||||
|
float4 a;
|
||||||
|
float4 b;
|
||||||
|
float4 ab;
|
||||||
|
float4 ap;
|
||||||
|
float4 v;
|
||||||
|
|
||||||
|
float4 plane = make_float4(face->m_plane.x,face->m_plane.y,face->m_plane.z,0.f);
|
||||||
|
|
||||||
|
if (face->m_numIndices<2)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
|
||||||
|
float4 v0 = baseVertex[convexIndices[face->m_indexOffset + face->m_numIndices-1]];
|
||||||
|
float4 worldV0 = transform(&v0, &posConvex, &ornConvex);
|
||||||
|
|
||||||
|
b = worldV0;
|
||||||
|
|
||||||
|
for(unsigned i=0; i != face->m_numIndices; ++i)
|
||||||
|
{
|
||||||
|
a = b;
|
||||||
|
float4 vi = baseVertex[convexIndices[face->m_indexOffset + i]];
|
||||||
|
float4 worldVi = transform(&vi, &posConvex, &ornConvex);
|
||||||
|
b = worldVi;
|
||||||
|
ab = b-a;
|
||||||
|
ap = p-a;
|
||||||
|
v = cross3(ab,plane);
|
||||||
|
|
||||||
|
if (dot(ap, v) > 0.f)
|
||||||
|
{
|
||||||
|
float ab_m2 = dot(ab, ab);
|
||||||
|
float rt = ab_m2 != 0.f ? dot(ab, ap) / ab_m2 : 0.f;
|
||||||
|
if (rt <= 0.f)
|
||||||
|
{
|
||||||
|
*out = a;
|
||||||
|
}
|
||||||
|
else if (rt >= 1.f)
|
||||||
|
{
|
||||||
|
*out = b;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
float s = 1.f - rt;
|
||||||
|
out[0].x = s * a.x + rt * b.x;
|
||||||
|
out[0].y = s * a.y + rt * b.y;
|
||||||
|
out[0].z = s * a.z + rt * b.z;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void computeContactSphereConvex(int pairIndex,
|
||||||
|
int bodyIndexA, int bodyIndexB,
|
||||||
|
int collidableIndexA, int collidableIndexB,
|
||||||
|
__global const BodyData* rigidBodies,
|
||||||
|
__global const btCollidableGpu* collidables,
|
||||||
|
__global const ConvexPolyhedronCL* convexShapes,
|
||||||
|
__global const float4* convexVertices,
|
||||||
|
__global const int* convexIndices,
|
||||||
|
__global const btGpuFace* faces,
|
||||||
|
__global Contact4* restrict globalContactsOut,
|
||||||
|
counter32_t nGlobalContactsOut,
|
||||||
|
int numPairs)
|
||||||
|
{
|
||||||
|
|
||||||
|
float radius = collidables[collidableIndexA].m_radius;
|
||||||
|
float4 spherePos1 = rigidBodies[bodyIndexA].m_pos;
|
||||||
|
float4 sphereOrn = rigidBodies[bodyIndexA].m_quat;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
float4 pos = rigidBodies[bodyIndexB].m_pos;
|
||||||
|
float4 quat = rigidBodies[bodyIndexB].m_quat;
|
||||||
|
|
||||||
|
float4 spherePos = spherePos1 - pos;
|
||||||
|
|
||||||
|
int collidableIndex = rigidBodies[bodyIndexB].m_collidableIdx;
|
||||||
|
int shapeIndex = collidables[collidableIndex].m_shapeIndex;
|
||||||
|
int numFaces = convexShapes[shapeIndex].m_numFaces;
|
||||||
|
float4 closestPnt = (float4)(0, 0, 0, 0);
|
||||||
|
float4 hitNormalWorld = (float4)(0, 0, 0, 0);
|
||||||
|
float minDist = -1000000.f;
|
||||||
|
bool bCollide = true;
|
||||||
|
|
||||||
|
for ( int f = 0; f < numFaces; f++ )
|
||||||
|
{
|
||||||
|
btGpuFace face = faces[convexShapes[shapeIndex].m_faceOffset+f];
|
||||||
|
|
||||||
|
// set up a plane equation
|
||||||
|
float4 planeEqn;
|
||||||
|
float4 n1 = qtRotate(quat, (float4)(face.m_plane.xyz, 0));
|
||||||
|
planeEqn = n1;
|
||||||
|
planeEqn.w = face.m_plane.w;
|
||||||
|
|
||||||
|
|
||||||
|
// compute a signed distance from the vertex in cloth to the face of rigidbody.
|
||||||
|
float4 pntReturn;
|
||||||
|
float dist = signedDistanceFromPointToPlane(spherePos, planeEqn, &pntReturn);
|
||||||
|
|
||||||
|
// If the distance is positive, the plane is a separating plane.
|
||||||
|
if ( dist > radius )
|
||||||
|
{
|
||||||
|
bCollide = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (dist>0)
|
||||||
|
{
|
||||||
|
//might hit an edge or vertex
|
||||||
|
float4 out;
|
||||||
|
bool isInPoly = IsPointInPolygon(spherePos,
|
||||||
|
pos,
|
||||||
|
quat,
|
||||||
|
&face,
|
||||||
|
&convexVertices[convexShapes[shapeIndex].m_vertexOffset],
|
||||||
|
convexIndices,
|
||||||
|
&out);
|
||||||
|
if (isInPoly)
|
||||||
|
{
|
||||||
|
if (dist>minDist)
|
||||||
|
{
|
||||||
|
minDist = dist;
|
||||||
|
closestPnt = pntReturn;
|
||||||
|
hitNormalWorld = planeEqn;
|
||||||
|
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
float4 tmp = spherePos-out;
|
||||||
|
float l2 = dot(tmp,tmp);
|
||||||
|
if (l2<radius*radius)
|
||||||
|
{
|
||||||
|
dist = sqrt(l2);
|
||||||
|
if (dist>minDist)
|
||||||
|
{
|
||||||
|
minDist = dist;
|
||||||
|
closestPnt = out;
|
||||||
|
hitNormalWorld = tmp/dist;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
bCollide = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if ( dist > minDist )
|
||||||
|
{
|
||||||
|
minDist = dist;
|
||||||
|
closestPnt = pntReturn;
|
||||||
|
hitNormalWorld.xyz = planeEqn.xyz;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (bCollide)
|
||||||
|
{
|
||||||
|
float4 normalOnSurfaceB1 = -hitNormalWorld;
|
||||||
|
float4 pOnB1 = closestPnt+pos;
|
||||||
|
float actualDepth = minDist-radius;
|
||||||
|
pOnB1.w = actualDepth;
|
||||||
|
|
||||||
|
int dstIdx;
|
||||||
|
AppendInc( nGlobalContactsOut, dstIdx );
|
||||||
|
|
||||||
|
if (dstIdx < numPairs)
|
||||||
|
{
|
||||||
|
__global Contact4* c = &globalContactsOut[dstIdx];
|
||||||
|
c->m_worldNormal = normalOnSurfaceB1;
|
||||||
|
c->m_coeffs = (u32)(0.f*0xffff) | ((u32)(0.7f*0xffff)<<16);
|
||||||
|
c->m_batchIdx = pairIndex;
|
||||||
|
c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
|
||||||
|
c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
|
||||||
|
c->m_worldPos[0] = pOnB1;
|
||||||
|
GET_NPOINTS(*c) = 1;
|
||||||
|
}//if (dstIdx < numPairs)
|
||||||
|
}//if (hasCollision)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void computeContactPlaneConvex(int pairIndex,
|
||||||
|
int bodyIndexA, int bodyIndexB,
|
||||||
|
int collidableIndexA, int collidableIndexB,
|
||||||
|
__global const BodyData* rigidBodies,
|
||||||
|
__global const btCollidableGpu* collidables,
|
||||||
|
__global const btGpuFace* faces,
|
||||||
|
__global Contact4* restrict globalContactsOut,
|
||||||
|
counter32_t nGlobalContactsOut,
|
||||||
|
int numPairs)
|
||||||
|
{
|
||||||
|
float4 planeEq = faces[collidables[collidableIndexA].m_shapeIndex].m_plane;
|
||||||
|
float radius = collidables[collidableIndexB].m_radius;
|
||||||
|
float4 posA1 = rigidBodies[bodyIndexA].m_pos;
|
||||||
|
float4 ornA1 = rigidBodies[bodyIndexA].m_quat;
|
||||||
|
float4 posB1 = rigidBodies[bodyIndexB].m_pos;
|
||||||
|
float4 ornB1 = rigidBodies[bodyIndexB].m_quat;
|
||||||
|
|
||||||
|
bool hasCollision = false;
|
||||||
|
float4 planeNormal1 = make_float4(planeEq.x,planeEq.y,planeEq.z,0.f);
|
||||||
|
float planeConstant = planeEq.w;
|
||||||
|
float4 convexInPlaneTransPos1; Quaternion convexInPlaneTransOrn1;
|
||||||
|
{
|
||||||
|
float4 invPosA;Quaternion invOrnA;
|
||||||
|
trInverse(posA1,ornA1,&invPosA,&invOrnA);
|
||||||
|
trMul(invPosA,invOrnA,posB1,ornB1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);
|
||||||
|
}
|
||||||
|
float4 planeInConvexPos1; Quaternion planeInConvexOrn1;
|
||||||
|
{
|
||||||
|
float4 invPosB;Quaternion invOrnB;
|
||||||
|
trInverse(posB1,ornB1,&invPosB,&invOrnB);
|
||||||
|
trMul(invPosB,invOrnB,posA1,ornA1,&planeInConvexPos1,&planeInConvexOrn1);
|
||||||
|
}
|
||||||
|
float4 vtx1 = qtRotate(planeInConvexOrn1,-planeNormal1)*radius;
|
||||||
|
float4 vtxInPlane1 = transform(&vtx1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);
|
||||||
|
float distance = dot3F4(planeNormal1,vtxInPlane1) - planeConstant;
|
||||||
|
hasCollision = distance < 0.f;//m_manifoldPtr->getContactBreakingThreshold();
|
||||||
|
if (hasCollision)
|
||||||
|
{
|
||||||
|
float4 vtxInPlaneProjected1 = vtxInPlane1 - distance*planeNormal1;
|
||||||
|
float4 vtxInPlaneWorld1 = transform(&vtxInPlaneProjected1,&posA1,&ornA1);
|
||||||
|
float4 normalOnSurfaceB1 = qtRotate(ornA1,planeNormal1);
|
||||||
|
float4 pOnB1 = vtxInPlaneWorld1+normalOnSurfaceB1*distance;
|
||||||
|
pOnB1.w = distance;
|
||||||
|
|
||||||
|
int dstIdx;
|
||||||
|
AppendInc( nGlobalContactsOut, dstIdx );
|
||||||
|
|
||||||
|
if (dstIdx < numPairs)
|
||||||
|
{
|
||||||
|
__global Contact4* c = &globalContactsOut[dstIdx];
|
||||||
|
c->m_worldNormal = normalOnSurfaceB1;
|
||||||
|
c->m_coeffs = (u32)(0.f*0xffff) | ((u32)(0.7f*0xffff)<<16);
|
||||||
|
c->m_batchIdx = pairIndex;
|
||||||
|
c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
|
||||||
|
c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
|
||||||
|
c->m_worldPos[0] = pOnB1;
|
||||||
|
GET_NPOINTS(*c) = 1;
|
||||||
|
}//if (dstIdx < numPairs)
|
||||||
|
}//if (hasCollision)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
__kernel void primitiveContactsKernel( __global const int2* pairs,
|
||||||
|
__global const BodyData* rigidBodies,
|
||||||
|
__global const btCollidableGpu* collidables,
|
||||||
|
__global const ConvexPolyhedronCL* convexShapes,
|
||||||
|
__global const float4* vertices,
|
||||||
|
__global const float4* uniqueEdges,
|
||||||
|
__global const btGpuFace* faces,
|
||||||
|
__global const int* indices,
|
||||||
|
__global Contact4* restrict globalContactsOut,
|
||||||
|
counter32_t nGlobalContactsOut,
|
||||||
|
int numPairs)
|
||||||
|
{
|
||||||
|
|
||||||
|
int i = get_global_id(0);
|
||||||
|
int pairIndex = i;
|
||||||
|
|
||||||
|
float4 worldVertsB1[64];
|
||||||
|
float4 worldVertsB2[64];
|
||||||
|
int capacityWorldVerts = 64;
|
||||||
|
|
||||||
|
float4 localContactsOut[64];
|
||||||
|
int localContactCapacity=64;
|
||||||
|
|
||||||
|
float minDist = -1e30f;
|
||||||
|
float maxDist = 0.02f;
|
||||||
|
|
||||||
|
if (i<numPairs)
|
||||||
|
{
|
||||||
|
|
||||||
|
int bodyIndexA = pairs[i].x;
|
||||||
|
int bodyIndexB = pairs[i].y;
|
||||||
|
|
||||||
|
int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
|
||||||
|
int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
|
||||||
|
|
||||||
|
|
||||||
|
if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
|
||||||
|
collidables[collidableIndexB].m_shapeType == SHAPE_PLANE)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
computeContactPlaneConvex( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA,
|
||||||
|
rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,numPairs);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (collidables[collidableIndexA].m_shapeType == SHAPE_PLANE &&
|
||||||
|
collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
computeContactPlaneConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
|
||||||
|
rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,numPairs);
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
|
||||||
|
collidables[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)
|
||||||
|
{
|
||||||
|
|
||||||
|
computeContactSphereConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
|
||||||
|
rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,numPairs);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (collidables[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&
|
||||||
|
collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
|
||||||
|
{
|
||||||
|
|
||||||
|
computeContactSphereConvex(pairIndex, bodyIndexB, bodyIndexA, collidableIndexB, collidableIndexA,
|
||||||
|
rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,numPairs);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
|
||||||
|
collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
|
||||||
|
{
|
||||||
|
//sphere-sphere
|
||||||
|
float radiusA = collidables[collidableIndexA].m_radius;
|
||||||
|
float radiusB = collidables[collidableIndexB].m_radius;
|
||||||
|
float4 posA = rigidBodies[bodyIndexA].m_pos;
|
||||||
|
float4 posB = rigidBodies[bodyIndexB].m_pos;
|
||||||
|
|
||||||
|
float4 diff = posA-posB;
|
||||||
|
float len = length(diff);
|
||||||
|
|
||||||
|
///iff distance positive, don't generate a new contact
|
||||||
|
if ( len <= (radiusA+radiusB))
|
||||||
|
{
|
||||||
|
///distance (negative means penetration)
|
||||||
|
float dist = len - (radiusA+radiusB);
|
||||||
|
float4 normalOnSurfaceB = make_float4(1.f,0.f,0.f,0.f);
|
||||||
|
if (len > 0.00001)
|
||||||
|
{
|
||||||
|
normalOnSurfaceB = diff / len;
|
||||||
|
}
|
||||||
|
float4 contactPosB = posB + normalOnSurfaceB*radiusB;
|
||||||
|
contactPosB.w = dist;
|
||||||
|
|
||||||
|
int dstIdx;
|
||||||
|
AppendInc( nGlobalContactsOut, dstIdx );
|
||||||
|
|
||||||
|
if (dstIdx < numPairs)
|
||||||
|
{
|
||||||
|
__global Contact4* c = &globalContactsOut[dstIdx];
|
||||||
|
c->m_worldNormal = -normalOnSurfaceB;
|
||||||
|
c->m_coeffs = (u32)(0.f*0xffff) | ((u32)(0.7f*0xffff)<<16);
|
||||||
|
c->m_batchIdx = pairIndex;
|
||||||
|
int bodyA = pairs[pairIndex].x;
|
||||||
|
int bodyB = pairs[pairIndex].y;
|
||||||
|
c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;
|
||||||
|
c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;
|
||||||
|
c->m_worldPos[0] = contactPosB;
|
||||||
|
GET_NPOINTS(*c) = 1;
|
||||||
|
}//if (dstIdx < numPairs)
|
||||||
|
}//if ( len <= (radiusA+radiusB))
|
||||||
|
|
||||||
|
return;
|
||||||
|
}//SHAPE_SPHERE SHAPE_SPHERE
|
||||||
|
|
||||||
|
}// if (i<numPairs)
|
||||||
|
|
||||||
|
}
|
||||||
671
opencl/gpu_sat/kernels/primitiveContacts.h
Normal file
671
opencl/gpu_sat/kernels/primitiveContacts.h
Normal file
@@ -0,0 +1,671 @@
|
|||||||
|
//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
|
||||||
|
static const char* primitiveContactsKernelsCL= \
|
||||||
|
"#define TRIANGLE_NUM_CONVEX_FACES 5\n"
|
||||||
|
"\n"
|
||||||
|
"#define SHAPE_CONVEX_HULL 3\n"
|
||||||
|
"#define SHAPE_PLANE 4\n"
|
||||||
|
"#define SHAPE_CONCAVE_TRIMESH 5\n"
|
||||||
|
"#define SHAPE_COMPOUND_OF_CONVEX_HULLS 6\n"
|
||||||
|
"#define SHAPE_SPHERE 7\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"#pragma OPENCL EXTENSION cl_amd_printf : enable\n"
|
||||||
|
"#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable\n"
|
||||||
|
"#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n"
|
||||||
|
"#pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable\n"
|
||||||
|
"#pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable\n"
|
||||||
|
"\n"
|
||||||
|
"#ifdef cl_ext_atomic_counters_32\n"
|
||||||
|
"#pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable\n"
|
||||||
|
"#else\n"
|
||||||
|
"#define counter32_t volatile __global int*\n"
|
||||||
|
"#endif\n"
|
||||||
|
"\n"
|
||||||
|
"#define GET_GROUP_IDX get_group_id(0)\n"
|
||||||
|
"#define GET_LOCAL_IDX get_local_id(0)\n"
|
||||||
|
"#define GET_GLOBAL_IDX get_global_id(0)\n"
|
||||||
|
"#define GET_GROUP_SIZE get_local_size(0)\n"
|
||||||
|
"#define GET_NUM_GROUPS get_num_groups(0)\n"
|
||||||
|
"#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)\n"
|
||||||
|
"#define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)\n"
|
||||||
|
"#define AtomInc(x) atom_inc(&(x))\n"
|
||||||
|
"#define AtomInc1(x, out) out = atom_inc(&(x))\n"
|
||||||
|
"#define AppendInc(x, out) out = atomic_inc(x)\n"
|
||||||
|
"#define AtomAdd(x, value) atom_add(&(x), value)\n"
|
||||||
|
"#define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )\n"
|
||||||
|
"#define AtomXhg(x, value) atom_xchg ( &(x), value )\n"
|
||||||
|
"\n"
|
||||||
|
"#define max2 max\n"
|
||||||
|
"#define min2 min\n"
|
||||||
|
"\n"
|
||||||
|
"typedef unsigned int u32;\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"typedef struct\n"
|
||||||
|
"{\n"
|
||||||
|
" float4 m_worldPos[4];\n"
|
||||||
|
" float4 m_worldNormal; // w: m_nPoints\n"
|
||||||
|
" u32 m_coeffs;\n"
|
||||||
|
" u32 m_batchIdx;\n"
|
||||||
|
"\n"
|
||||||
|
" int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr\n"
|
||||||
|
" int m_bodyBPtrAndSignBit;\n"
|
||||||
|
"} Contact4;\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"///keep this in sync with btCollidable.h\n"
|
||||||
|
"typedef struct\n"
|
||||||
|
"{\n"
|
||||||
|
" int m_numChildShapes;\n"
|
||||||
|
" float m_radius;\n"
|
||||||
|
" int m_shapeType;\n"
|
||||||
|
" int m_shapeIndex;\n"
|
||||||
|
" \n"
|
||||||
|
"} btCollidableGpu;\n"
|
||||||
|
"\n"
|
||||||
|
"typedef struct\n"
|
||||||
|
"{\n"
|
||||||
|
" float4 m_childPosition;\n"
|
||||||
|
" float4 m_childOrientation;\n"
|
||||||
|
" int m_shapeIndex;\n"
|
||||||
|
" int m_unused0;\n"
|
||||||
|
" int m_unused1;\n"
|
||||||
|
" int m_unused2;\n"
|
||||||
|
"} btGpuChildShape;\n"
|
||||||
|
"\n"
|
||||||
|
"#define GET_NPOINTS(x) (x).m_worldNormal.w\n"
|
||||||
|
"\n"
|
||||||
|
"typedef struct\n"
|
||||||
|
"{\n"
|
||||||
|
" float4 m_pos;\n"
|
||||||
|
" float4 m_quat;\n"
|
||||||
|
" float4 m_linVel;\n"
|
||||||
|
" float4 m_angVel;\n"
|
||||||
|
"\n"
|
||||||
|
" u32 m_collidableIdx; \n"
|
||||||
|
" float m_invMass;\n"
|
||||||
|
" float m_restituitionCoeff;\n"
|
||||||
|
" float m_frictionCoeff;\n"
|
||||||
|
"} BodyData;\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"typedef struct \n"
|
||||||
|
"{\n"
|
||||||
|
" float4 m_localCenter;\n"
|
||||||
|
" float4 m_extents;\n"
|
||||||
|
" float4 mC;\n"
|
||||||
|
" float4 mE;\n"
|
||||||
|
" \n"
|
||||||
|
" float m_radius;\n"
|
||||||
|
" int m_faceOffset;\n"
|
||||||
|
" int m_numFaces;\n"
|
||||||
|
" int m_numVertices;\n"
|
||||||
|
" \n"
|
||||||
|
" int m_vertexOffset;\n"
|
||||||
|
" int m_uniqueEdgesOffset;\n"
|
||||||
|
" int m_numUniqueEdges;\n"
|
||||||
|
" int m_unused;\n"
|
||||||
|
"\n"
|
||||||
|
"} ConvexPolyhedronCL;\n"
|
||||||
|
"\n"
|
||||||
|
"typedef struct\n"
|
||||||
|
"{\n"
|
||||||
|
" float4 m_plane;\n"
|
||||||
|
" int m_indexOffset;\n"
|
||||||
|
" int m_numIndices;\n"
|
||||||
|
"} btGpuFace;\n"
|
||||||
|
"\n"
|
||||||
|
"#define SELECT_UINT4( b, a, condition ) select( b,a,condition )\n"
|
||||||
|
"\n"
|
||||||
|
"#define make_float4 (float4)\n"
|
||||||
|
"#define make_float2 (float2)\n"
|
||||||
|
"#define make_uint4 (uint4)\n"
|
||||||
|
"#define make_int4 (int4)\n"
|
||||||
|
"#define make_uint2 (uint2)\n"
|
||||||
|
"#define make_int2 (int2)\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"float fastDiv(float numerator, float denominator)\n"
|
||||||
|
"{\n"
|
||||||
|
" return native_divide(numerator, denominator); \n"
|
||||||
|
"// return numerator/denominator; \n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"float4 fastDiv4(float4 numerator, float4 denominator)\n"
|
||||||
|
"{\n"
|
||||||
|
" return native_divide(numerator, denominator); \n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"float4 cross3(float4 a, float4 b)\n"
|
||||||
|
"{\n"
|
||||||
|
" return cross(a,b);\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"//#define dot3F4 dot\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"float dot3F4(float4 a, float4 b)\n"
|
||||||
|
"{\n"
|
||||||
|
" float4 a1 = make_float4(a.xyz,0.f);\n"
|
||||||
|
" float4 b1 = make_float4(b.xyz,0.f);\n"
|
||||||
|
" return dot(a1, b1);\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"float4 fastNormalize4(float4 v)\n"
|
||||||
|
"{\n"
|
||||||
|
" return fast_normalize(v);\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"///////////////////////////////////////\n"
|
||||||
|
"// Quaternion\n"
|
||||||
|
"///////////////////////////////////////\n"
|
||||||
|
"\n"
|
||||||
|
"typedef float4 Quaternion;\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"Quaternion qtMul(Quaternion a, Quaternion b);\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"Quaternion qtNormalize(Quaternion in);\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"float4 qtRotate(Quaternion q, float4 vec);\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"Quaternion qtInvert(Quaternion q);\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"Quaternion qtMul(Quaternion a, Quaternion b)\n"
|
||||||
|
"{\n"
|
||||||
|
" Quaternion ans;\n"
|
||||||
|
" ans = cross3( a, b );\n"
|
||||||
|
" ans += a.w*b+b.w*a;\n"
|
||||||
|
"// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
|
||||||
|
" ans.w = a.w*b.w - dot3F4(a, b);\n"
|
||||||
|
" return ans;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"Quaternion qtNormalize(Quaternion in)\n"
|
||||||
|
"{\n"
|
||||||
|
" return fastNormalize4(in);\n"
|
||||||
|
"// in /= length( in );\n"
|
||||||
|
"// return in;\n"
|
||||||
|
"}\n"
|
||||||
|
"__inline\n"
|
||||||
|
"float4 qtRotate(Quaternion q, float4 vec)\n"
|
||||||
|
"{\n"
|
||||||
|
" Quaternion qInv = qtInvert( q );\n"
|
||||||
|
" float4 vcpy = vec;\n"
|
||||||
|
" vcpy.w = 0.f;\n"
|
||||||
|
" float4 out = qtMul(qtMul(q,vcpy),qInv);\n"
|
||||||
|
" return out;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"Quaternion qtInvert(Quaternion q)\n"
|
||||||
|
"{\n"
|
||||||
|
" return (Quaternion)(-q.xyz, q.w);\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"float4 qtInvRotate(const Quaternion q, float4 vec)\n"
|
||||||
|
"{\n"
|
||||||
|
" return qtRotate( qtInvert( q ), vec );\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)\n"
|
||||||
|
"{\n"
|
||||||
|
" return qtRotate( *orientation, *p ) + (*translation);\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"void trInverse(float4 translationIn, Quaternion orientationIn,\n"
|
||||||
|
" float4* translationOut, Quaternion* orientationOut)\n"
|
||||||
|
"{\n"
|
||||||
|
" *orientationOut = qtInvert(orientationIn);\n"
|
||||||
|
" *translationOut = qtRotate(*orientationOut, -translationIn);\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"void trMul(float4 translationA, Quaternion orientationA,\n"
|
||||||
|
" float4 translationB, Quaternion orientationB,\n"
|
||||||
|
" float4* translationOut, Quaternion* orientationOut)\n"
|
||||||
|
"{\n"
|
||||||
|
" *orientationOut = qtMul(orientationA,orientationB);\n"
|
||||||
|
" *translationOut = transform(&translationB,&translationA,&orientationA);\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"__inline\n"
|
||||||
|
"float4 normalize3(const float4 a)\n"
|
||||||
|
"{\n"
|
||||||
|
" float4 n = make_float4(a.x, a.y, a.z, 0.f);\n"
|
||||||
|
" return fastNormalize4( n );\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"__inline float4 lerp3(const float4 a,const float4 b, float t)\n"
|
||||||
|
"{\n"
|
||||||
|
" return make_float4( a.x + (b.x - a.x) * t,\n"
|
||||||
|
" a.y + (b.y - a.y) * t,\n"
|
||||||
|
" a.z + (b.z - a.z) * t,\n"
|
||||||
|
" 0.f);\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"float signedDistanceFromPointToPlane(float4 point, float4 planeEqn, float4* closestPointOnFace)\n"
|
||||||
|
"{\n"
|
||||||
|
" float4 n = (float4)(planeEqn.x, planeEqn.y, planeEqn.z, 0);\n"
|
||||||
|
" float dist = dot3F4(n, point) + planeEqn.w;\n"
|
||||||
|
" *closestPointOnFace = point - dist * n;\n"
|
||||||
|
" return dist;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"inline bool IsPointInPolygon(float4 p, \n"
|
||||||
|
" float4 posConvex,\n"
|
||||||
|
" float4 ornConvex,\n"
|
||||||
|
" const btGpuFace* face,\n"
|
||||||
|
" __global const float4* baseVertex,\n"
|
||||||
|
" __global const int* convexIndices,\n"
|
||||||
|
" float4* out)\n"
|
||||||
|
"{\n"
|
||||||
|
" float4 a;\n"
|
||||||
|
" float4 b;\n"
|
||||||
|
" float4 ab;\n"
|
||||||
|
" float4 ap;\n"
|
||||||
|
" float4 v;\n"
|
||||||
|
"\n"
|
||||||
|
" float4 plane = make_float4(face->m_plane.x,face->m_plane.y,face->m_plane.z,0.f);\n"
|
||||||
|
" \n"
|
||||||
|
" if (face->m_numIndices<2)\n"
|
||||||
|
" return false;\n"
|
||||||
|
"\n"
|
||||||
|
" \n"
|
||||||
|
" float4 v0 = baseVertex[convexIndices[face->m_indexOffset + face->m_numIndices-1]];\n"
|
||||||
|
" float4 worldV0 = transform(&v0, &posConvex, &ornConvex);\n"
|
||||||
|
" \n"
|
||||||
|
" b = worldV0;\n"
|
||||||
|
"\n"
|
||||||
|
" for(unsigned i=0; i != face->m_numIndices; ++i)\n"
|
||||||
|
" {\n"
|
||||||
|
" a = b;\n"
|
||||||
|
" float4 vi = baseVertex[convexIndices[face->m_indexOffset + i]];\n"
|
||||||
|
" float4 worldVi = transform(&vi, &posConvex, &ornConvex);\n"
|
||||||
|
" b = worldVi;\n"
|
||||||
|
" ab = b-a;\n"
|
||||||
|
" ap = p-a;\n"
|
||||||
|
" v = cross3(ab,plane);\n"
|
||||||
|
"\n"
|
||||||
|
" if (dot(ap, v) > 0.f)\n"
|
||||||
|
" {\n"
|
||||||
|
" float ab_m2 = dot(ab, ab);\n"
|
||||||
|
" float rt = ab_m2 != 0.f ? dot(ab, ap) / ab_m2 : 0.f;\n"
|
||||||
|
" if (rt <= 0.f)\n"
|
||||||
|
" {\n"
|
||||||
|
" *out = a;\n"
|
||||||
|
" }\n"
|
||||||
|
" else if (rt >= 1.f) \n"
|
||||||
|
" {\n"
|
||||||
|
" *out = b;\n"
|
||||||
|
" }\n"
|
||||||
|
" else\n"
|
||||||
|
" {\n"
|
||||||
|
" float s = 1.f - rt;\n"
|
||||||
|
" out[0].x = s * a.x + rt * b.x;\n"
|
||||||
|
" out[0].y = s * a.y + rt * b.y;\n"
|
||||||
|
" out[0].z = s * a.z + rt * b.z;\n"
|
||||||
|
" }\n"
|
||||||
|
" return false;\n"
|
||||||
|
" }\n"
|
||||||
|
" }\n"
|
||||||
|
" return true;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"void computeContactSphereConvex(int pairIndex,\n"
|
||||||
|
" int bodyIndexA, int bodyIndexB, \n"
|
||||||
|
" int collidableIndexA, int collidableIndexB, \n"
|
||||||
|
" __global const BodyData* rigidBodies, \n"
|
||||||
|
" __global const btCollidableGpu* collidables,\n"
|
||||||
|
" __global const ConvexPolyhedronCL* convexShapes,\n"
|
||||||
|
" __global const float4* convexVertices,\n"
|
||||||
|
" __global const int* convexIndices,\n"
|
||||||
|
" __global const btGpuFace* faces,\n"
|
||||||
|
" __global Contact4* restrict globalContactsOut,\n"
|
||||||
|
" counter32_t nGlobalContactsOut,\n"
|
||||||
|
" int numPairs)\n"
|
||||||
|
"{\n"
|
||||||
|
"\n"
|
||||||
|
" float radius = collidables[collidableIndexA].m_radius;\n"
|
||||||
|
" float4 spherePos1 = rigidBodies[bodyIndexA].m_pos;\n"
|
||||||
|
" float4 sphereOrn = rigidBodies[bodyIndexA].m_quat;\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
" float4 pos = rigidBodies[bodyIndexB].m_pos;\n"
|
||||||
|
" float4 quat = rigidBodies[bodyIndexB].m_quat;\n"
|
||||||
|
"\n"
|
||||||
|
" float4 spherePos = spherePos1 - pos;\n"
|
||||||
|
"\n"
|
||||||
|
" int collidableIndex = rigidBodies[bodyIndexB].m_collidableIdx;\n"
|
||||||
|
" int shapeIndex = collidables[collidableIndex].m_shapeIndex;\n"
|
||||||
|
" int numFaces = convexShapes[shapeIndex].m_numFaces;\n"
|
||||||
|
" float4 closestPnt = (float4)(0, 0, 0, 0);\n"
|
||||||
|
" float4 hitNormalWorld = (float4)(0, 0, 0, 0);\n"
|
||||||
|
" float minDist = -1000000.f;\n"
|
||||||
|
" bool bCollide = true;\n"
|
||||||
|
"\n"
|
||||||
|
" for ( int f = 0; f < numFaces; f++ )\n"
|
||||||
|
" {\n"
|
||||||
|
" btGpuFace face = faces[convexShapes[shapeIndex].m_faceOffset+f];\n"
|
||||||
|
"\n"
|
||||||
|
" // set up a plane equation \n"
|
||||||
|
" float4 planeEqn;\n"
|
||||||
|
" float4 n1 = qtRotate(quat, (float4)(face.m_plane.xyz, 0));\n"
|
||||||
|
" planeEqn = n1;\n"
|
||||||
|
" planeEqn.w = face.m_plane.w;\n"
|
||||||
|
" \n"
|
||||||
|
" \n"
|
||||||
|
" // compute a signed distance from the vertex in cloth to the face of rigidbody.\n"
|
||||||
|
" float4 pntReturn;\n"
|
||||||
|
" float dist = signedDistanceFromPointToPlane(spherePos, planeEqn, &pntReturn);\n"
|
||||||
|
"\n"
|
||||||
|
" // If the distance is positive, the plane is a separating plane. \n"
|
||||||
|
" if ( dist > radius )\n"
|
||||||
|
" {\n"
|
||||||
|
" bCollide = false;\n"
|
||||||
|
" break;\n"
|
||||||
|
" }\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
" if (dist>0)\n"
|
||||||
|
" {\n"
|
||||||
|
" //might hit an edge or vertex\n"
|
||||||
|
" float4 out;\n"
|
||||||
|
" bool isInPoly = IsPointInPolygon(spherePos,\n"
|
||||||
|
" pos,\n"
|
||||||
|
" quat,\n"
|
||||||
|
" &face,\n"
|
||||||
|
" &convexVertices[convexShapes[shapeIndex].m_vertexOffset],\n"
|
||||||
|
" convexIndices,\n"
|
||||||
|
" &out);\n"
|
||||||
|
" if (isInPoly)\n"
|
||||||
|
" {\n"
|
||||||
|
" if (dist>minDist)\n"
|
||||||
|
" {\n"
|
||||||
|
" minDist = dist;\n"
|
||||||
|
" closestPnt = pntReturn;\n"
|
||||||
|
" hitNormalWorld = planeEqn;\n"
|
||||||
|
" \n"
|
||||||
|
" }\n"
|
||||||
|
" } else\n"
|
||||||
|
" {\n"
|
||||||
|
" float4 tmp = spherePos-out;\n"
|
||||||
|
" float l2 = dot(tmp,tmp);\n"
|
||||||
|
" if (l2<radius*radius)\n"
|
||||||
|
" {\n"
|
||||||
|
" dist = sqrt(l2);\n"
|
||||||
|
" if (dist>minDist)\n"
|
||||||
|
" {\n"
|
||||||
|
" minDist = dist;\n"
|
||||||
|
" closestPnt = out;\n"
|
||||||
|
" hitNormalWorld = tmp/dist;\n"
|
||||||
|
" \n"
|
||||||
|
" }\n"
|
||||||
|
" \n"
|
||||||
|
" } else\n"
|
||||||
|
" {\n"
|
||||||
|
" bCollide = false;\n"
|
||||||
|
" break;\n"
|
||||||
|
" }\n"
|
||||||
|
" }\n"
|
||||||
|
" } else\n"
|
||||||
|
" {\n"
|
||||||
|
" if ( dist > minDist )\n"
|
||||||
|
" {\n"
|
||||||
|
" minDist = dist;\n"
|
||||||
|
" closestPnt = pntReturn;\n"
|
||||||
|
" hitNormalWorld.xyz = planeEqn.xyz;\n"
|
||||||
|
" }\n"
|
||||||
|
" }\n"
|
||||||
|
" \n"
|
||||||
|
" }\n"
|
||||||
|
"\n"
|
||||||
|
" \n"
|
||||||
|
"\n"
|
||||||
|
" if (bCollide)\n"
|
||||||
|
" {\n"
|
||||||
|
" float4 normalOnSurfaceB1 = -hitNormalWorld;\n"
|
||||||
|
" float4 pOnB1 = closestPnt+pos;\n"
|
||||||
|
" float actualDepth = minDist-radius;\n"
|
||||||
|
" pOnB1.w = actualDepth;\n"
|
||||||
|
"\n"
|
||||||
|
" int dstIdx;\n"
|
||||||
|
" AppendInc( nGlobalContactsOut, dstIdx );\n"
|
||||||
|
" \n"
|
||||||
|
" if (dstIdx < numPairs)\n"
|
||||||
|
" {\n"
|
||||||
|
" __global Contact4* c = &globalContactsOut[dstIdx];\n"
|
||||||
|
" c->m_worldNormal = normalOnSurfaceB1;\n"
|
||||||
|
" c->m_coeffs = (u32)(0.f*0xffff) | ((u32)(0.7f*0xffff)<<16);\n"
|
||||||
|
" c->m_batchIdx = pairIndex;\n"
|
||||||
|
" c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;\n"
|
||||||
|
" c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;\n"
|
||||||
|
" c->m_worldPos[0] = pOnB1;\n"
|
||||||
|
" GET_NPOINTS(*c) = 1;\n"
|
||||||
|
" }//if (dstIdx < numPairs)\n"
|
||||||
|
" }//if (hasCollision)\n"
|
||||||
|
"\n"
|
||||||
|
"}\n"
|
||||||
|
" \n"
|
||||||
|
"\n"
|
||||||
|
" \n"
|
||||||
|
"void computeContactPlaneConvex(int pairIndex,\n"
|
||||||
|
" int bodyIndexA, int bodyIndexB, \n"
|
||||||
|
" int collidableIndexA, int collidableIndexB, \n"
|
||||||
|
" __global const BodyData* rigidBodies, \n"
|
||||||
|
" __global const btCollidableGpu* collidables,\n"
|
||||||
|
" __global const btGpuFace* faces,\n"
|
||||||
|
" __global Contact4* restrict globalContactsOut,\n"
|
||||||
|
" counter32_t nGlobalContactsOut,\n"
|
||||||
|
" int numPairs)\n"
|
||||||
|
"{\n"
|
||||||
|
" float4 planeEq = faces[collidables[collidableIndexA].m_shapeIndex].m_plane;\n"
|
||||||
|
" float radius = collidables[collidableIndexB].m_radius;\n"
|
||||||
|
" float4 posA1 = rigidBodies[bodyIndexA].m_pos;\n"
|
||||||
|
" float4 ornA1 = rigidBodies[bodyIndexA].m_quat;\n"
|
||||||
|
" float4 posB1 = rigidBodies[bodyIndexB].m_pos;\n"
|
||||||
|
" float4 ornB1 = rigidBodies[bodyIndexB].m_quat;\n"
|
||||||
|
" \n"
|
||||||
|
" bool hasCollision = false;\n"
|
||||||
|
" float4 planeNormal1 = make_float4(planeEq.x,planeEq.y,planeEq.z,0.f);\n"
|
||||||
|
" float planeConstant = planeEq.w;\n"
|
||||||
|
" float4 convexInPlaneTransPos1; Quaternion convexInPlaneTransOrn1;\n"
|
||||||
|
" {\n"
|
||||||
|
" float4 invPosA;Quaternion invOrnA;\n"
|
||||||
|
" trInverse(posA1,ornA1,&invPosA,&invOrnA);\n"
|
||||||
|
" trMul(invPosA,invOrnA,posB1,ornB1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);\n"
|
||||||
|
" }\n"
|
||||||
|
" float4 planeInConvexPos1; Quaternion planeInConvexOrn1;\n"
|
||||||
|
" {\n"
|
||||||
|
" float4 invPosB;Quaternion invOrnB;\n"
|
||||||
|
" trInverse(posB1,ornB1,&invPosB,&invOrnB);\n"
|
||||||
|
" trMul(invPosB,invOrnB,posA1,ornA1,&planeInConvexPos1,&planeInConvexOrn1); \n"
|
||||||
|
" }\n"
|
||||||
|
" float4 vtx1 = qtRotate(planeInConvexOrn1,-planeNormal1)*radius;\n"
|
||||||
|
" float4 vtxInPlane1 = transform(&vtx1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);\n"
|
||||||
|
" float distance = dot3F4(planeNormal1,vtxInPlane1) - planeConstant;\n"
|
||||||
|
" hasCollision = distance < 0.f;//m_manifoldPtr->getContactBreakingThreshold();\n"
|
||||||
|
" if (hasCollision)\n"
|
||||||
|
" {\n"
|
||||||
|
" float4 vtxInPlaneProjected1 = vtxInPlane1 - distance*planeNormal1;\n"
|
||||||
|
" float4 vtxInPlaneWorld1 = transform(&vtxInPlaneProjected1,&posA1,&ornA1);\n"
|
||||||
|
" float4 normalOnSurfaceB1 = qtRotate(ornA1,planeNormal1);\n"
|
||||||
|
" float4 pOnB1 = vtxInPlaneWorld1+normalOnSurfaceB1*distance;\n"
|
||||||
|
" pOnB1.w = distance;\n"
|
||||||
|
"\n"
|
||||||
|
" int dstIdx;\n"
|
||||||
|
" AppendInc( nGlobalContactsOut, dstIdx );\n"
|
||||||
|
" \n"
|
||||||
|
" if (dstIdx < numPairs)\n"
|
||||||
|
" {\n"
|
||||||
|
" __global Contact4* c = &globalContactsOut[dstIdx];\n"
|
||||||
|
" c->m_worldNormal = normalOnSurfaceB1;\n"
|
||||||
|
" c->m_coeffs = (u32)(0.f*0xffff) | ((u32)(0.7f*0xffff)<<16);\n"
|
||||||
|
" c->m_batchIdx = pairIndex;\n"
|
||||||
|
" c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;\n"
|
||||||
|
" c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;\n"
|
||||||
|
" c->m_worldPos[0] = pOnB1;\n"
|
||||||
|
" GET_NPOINTS(*c) = 1;\n"
|
||||||
|
" }//if (dstIdx < numPairs)\n"
|
||||||
|
" }//if (hasCollision)\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"__kernel void primitiveContactsKernel( __global const int2* pairs, \n"
|
||||||
|
" __global const BodyData* rigidBodies, \n"
|
||||||
|
" __global const btCollidableGpu* collidables,\n"
|
||||||
|
" __global const ConvexPolyhedronCL* convexShapes, \n"
|
||||||
|
" __global const float4* vertices,\n"
|
||||||
|
" __global const float4* uniqueEdges,\n"
|
||||||
|
" __global const btGpuFace* faces,\n"
|
||||||
|
" __global const int* indices,\n"
|
||||||
|
" __global Contact4* restrict globalContactsOut,\n"
|
||||||
|
" counter32_t nGlobalContactsOut,\n"
|
||||||
|
" int numPairs)\n"
|
||||||
|
"{\n"
|
||||||
|
"\n"
|
||||||
|
" int i = get_global_id(0);\n"
|
||||||
|
" int pairIndex = i;\n"
|
||||||
|
" \n"
|
||||||
|
" float4 worldVertsB1[64];\n"
|
||||||
|
" float4 worldVertsB2[64];\n"
|
||||||
|
" int capacityWorldVerts = 64; \n"
|
||||||
|
"\n"
|
||||||
|
" float4 localContactsOut[64];\n"
|
||||||
|
" int localContactCapacity=64;\n"
|
||||||
|
" \n"
|
||||||
|
" float minDist = -1e30f;\n"
|
||||||
|
" float maxDist = 0.02f;\n"
|
||||||
|
"\n"
|
||||||
|
" if (i<numPairs)\n"
|
||||||
|
" {\n"
|
||||||
|
"\n"
|
||||||
|
" int bodyIndexA = pairs[i].x;\n"
|
||||||
|
" int bodyIndexB = pairs[i].y;\n"
|
||||||
|
" \n"
|
||||||
|
" int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
|
||||||
|
" int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
|
||||||
|
" \n"
|
||||||
|
"\n"
|
||||||
|
" if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&\n"
|
||||||
|
" collidables[collidableIndexB].m_shapeType == SHAPE_PLANE)\n"
|
||||||
|
" {\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
" computeContactPlaneConvex( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA, \n"
|
||||||
|
" rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,numPairs);\n"
|
||||||
|
" return;\n"
|
||||||
|
" }\n"
|
||||||
|
"\n"
|
||||||
|
" if (collidables[collidableIndexA].m_shapeType == SHAPE_PLANE &&\n"
|
||||||
|
" collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)\n"
|
||||||
|
" {\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
" computeContactPlaneConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB, \n"
|
||||||
|
" rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,numPairs);\n"
|
||||||
|
" return;\n"
|
||||||
|
" \n"
|
||||||
|
" }\n"
|
||||||
|
" \n"
|
||||||
|
" if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&\n"
|
||||||
|
" collidables[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)\n"
|
||||||
|
" {\n"
|
||||||
|
" \n"
|
||||||
|
" computeContactSphereConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB, \n"
|
||||||
|
" rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,numPairs);\n"
|
||||||
|
" return;\n"
|
||||||
|
" }\n"
|
||||||
|
"\n"
|
||||||
|
" if (collidables[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&\n"
|
||||||
|
" collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)\n"
|
||||||
|
" {\n"
|
||||||
|
" \n"
|
||||||
|
" computeContactSphereConvex(pairIndex, bodyIndexB, bodyIndexA, collidableIndexB, collidableIndexA, \n"
|
||||||
|
" rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,numPairs);\n"
|
||||||
|
" return;\n"
|
||||||
|
" }\n"
|
||||||
|
" \n"
|
||||||
|
" \n"
|
||||||
|
" \n"
|
||||||
|
" \n"
|
||||||
|
" \n"
|
||||||
|
" if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&\n"
|
||||||
|
" collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)\n"
|
||||||
|
" {\n"
|
||||||
|
" //sphere-sphere\n"
|
||||||
|
" float radiusA = collidables[collidableIndexA].m_radius;\n"
|
||||||
|
" float radiusB = collidables[collidableIndexB].m_radius;\n"
|
||||||
|
" float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
|
||||||
|
" float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
|
||||||
|
"\n"
|
||||||
|
" float4 diff = posA-posB;\n"
|
||||||
|
" float len = length(diff);\n"
|
||||||
|
" \n"
|
||||||
|
" ///iff distance positive, don't generate a new contact\n"
|
||||||
|
" if ( len <= (radiusA+radiusB))\n"
|
||||||
|
" {\n"
|
||||||
|
" ///distance (negative means penetration)\n"
|
||||||
|
" float dist = len - (radiusA+radiusB);\n"
|
||||||
|
" float4 normalOnSurfaceB = make_float4(1.f,0.f,0.f,0.f);\n"
|
||||||
|
" if (len > 0.00001)\n"
|
||||||
|
" {\n"
|
||||||
|
" normalOnSurfaceB = diff / len;\n"
|
||||||
|
" }\n"
|
||||||
|
" float4 contactPosB = posB + normalOnSurfaceB*radiusB;\n"
|
||||||
|
" contactPosB.w = dist;\n"
|
||||||
|
" \n"
|
||||||
|
" int dstIdx;\n"
|
||||||
|
" AppendInc( nGlobalContactsOut, dstIdx );\n"
|
||||||
|
" \n"
|
||||||
|
" if (dstIdx < numPairs)\n"
|
||||||
|
" {\n"
|
||||||
|
" __global Contact4* c = &globalContactsOut[dstIdx];\n"
|
||||||
|
" c->m_worldNormal = -normalOnSurfaceB;\n"
|
||||||
|
" c->m_coeffs = (u32)(0.f*0xffff) | ((u32)(0.7f*0xffff)<<16);\n"
|
||||||
|
" c->m_batchIdx = pairIndex;\n"
|
||||||
|
" int bodyA = pairs[pairIndex].x;\n"
|
||||||
|
" int bodyB = pairs[pairIndex].y;\n"
|
||||||
|
" c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;\n"
|
||||||
|
" c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;\n"
|
||||||
|
" c->m_worldPos[0] = contactPosB;\n"
|
||||||
|
" GET_NPOINTS(*c) = 1;\n"
|
||||||
|
" }//if (dstIdx < numPairs)\n"
|
||||||
|
" }//if ( len <= (radiusA+radiusB))\n"
|
||||||
|
"\n"
|
||||||
|
" return;\n"
|
||||||
|
" }//SHAPE_SPHERE SHAPE_SPHERE\n"
|
||||||
|
"\n"
|
||||||
|
" }// if (i<numPairs)\n"
|
||||||
|
"\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
;
|
||||||
@@ -1,9 +1,13 @@
|
|||||||
|
|
||||||
#define TRIANGLE_NUM_CONVEX_FACES 5
|
#define TRIANGLE_NUM_CONVEX_FACES 5
|
||||||
|
|
||||||
|
#define SHAPE_CONVEX_HULL 3
|
||||||
#define SHAPE_PLANE 4
|
#define SHAPE_PLANE 4
|
||||||
|
#define SHAPE_CONCAVE_TRIMESH 5
|
||||||
|
#define SHAPE_COMPOUND_OF_CONVEX_HULLS 6
|
||||||
#define SHAPE_SPHERE 7
|
#define SHAPE_SPHERE 7
|
||||||
|
|
||||||
|
|
||||||
#pragma OPENCL EXTENSION cl_amd_printf : enable
|
#pragma OPENCL EXTENSION cl_amd_printf : enable
|
||||||
#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
|
#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
|
||||||
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
|
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
|
||||||
@@ -41,12 +45,8 @@ typedef struct
|
|||||||
{
|
{
|
||||||
float4 m_worldPos[4];
|
float4 m_worldPos[4];
|
||||||
float4 m_worldNormal; // w: m_nPoints
|
float4 m_worldNormal; // w: m_nPoints
|
||||||
// float m_restituitionCoeff;
|
|
||||||
// float m_frictionCoeff;
|
|
||||||
u32 m_coeffs;
|
u32 m_coeffs;
|
||||||
u32 m_batchIdx;
|
u32 m_batchIdx;
|
||||||
// int m_nPoints;
|
|
||||||
// int m_padding0;
|
|
||||||
|
|
||||||
int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr
|
int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr
|
||||||
int m_bodyBPtrAndSignBit;
|
int m_bodyBPtrAndSignBit;
|
||||||
@@ -872,11 +872,6 @@ int extractManifoldSequential(const float4* p, int nPoints, float4 nearNormal, i
|
|||||||
contactIdx[2] = idx[2];
|
contactIdx[2] = idx[2];
|
||||||
contactIdx[3] = idx[3];
|
contactIdx[3] = idx[3];
|
||||||
|
|
||||||
// if( max00.y < 0.0f )
|
|
||||||
// contactIdx[0] = (int)max00.x;
|
|
||||||
|
|
||||||
//does this sort happen on GPU too?
|
|
||||||
//std::sort( contactIdx, contactIdx+4 );
|
|
||||||
|
|
||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
@@ -908,7 +903,7 @@ __kernel void extractManifoldAndAddContactKernel(__global const int2* pairs,
|
|||||||
{
|
{
|
||||||
localPoints[i] = pointsIn[i];
|
localPoints[i] = pointsIn[i];
|
||||||
}
|
}
|
||||||
// int contactIdx[4] = {-1,-1,-1,-1};
|
|
||||||
int contactIdx[4];// = {-1,-1,-1,-1};
|
int contactIdx[4];// = {-1,-1,-1,-1};
|
||||||
contactIdx[0] = -1;
|
contactIdx[0] = -1;
|
||||||
contactIdx[1] = -1;
|
contactIdx[1] = -1;
|
||||||
@@ -954,66 +949,7 @@ void trMul(float4 translationA, Quaternion orientationA,
|
|||||||
*translationOut = transform(&translationB,&translationA,&orientationA);
|
*translationOut = transform(&translationB,&translationA,&orientationA);
|
||||||
}
|
}
|
||||||
|
|
||||||
void computeContactPlaneConvex(int pairIndex,
|
|
||||||
int bodyIndexA, int bodyIndexB,
|
|
||||||
int collidableIndexA, int collidableIndexB,
|
|
||||||
__global const BodyData* rigidBodies,
|
|
||||||
__global const btCollidableGpu* collidables,
|
|
||||||
__global const btGpuFace* faces,
|
|
||||||
__global Contact4* restrict globalContactsOut,
|
|
||||||
counter32_t nGlobalContactsOut,
|
|
||||||
int numPairs)
|
|
||||||
{
|
|
||||||
float4 planeEq = faces[collidables[collidableIndexA].m_shapeIndex].m_plane;
|
|
||||||
float radius = collidables[collidableIndexB].m_radius;
|
|
||||||
float4 posA1 = rigidBodies[bodyIndexA].m_pos;
|
|
||||||
float4 ornA1 = rigidBodies[bodyIndexA].m_quat;
|
|
||||||
float4 posB1 = rigidBodies[bodyIndexB].m_pos;
|
|
||||||
float4 ornB1 = rigidBodies[bodyIndexB].m_quat;
|
|
||||||
|
|
||||||
bool hasCollision = false;
|
|
||||||
float4 planeNormal1 = make_float4(planeEq.x,planeEq.y,planeEq.z,0.f);
|
|
||||||
float planeConstant = planeEq.w;
|
|
||||||
float4 convexInPlaneTransPos1; Quaternion convexInPlaneTransOrn1;
|
|
||||||
{
|
|
||||||
float4 invPosA;Quaternion invOrnA;
|
|
||||||
trInverse(posA1,ornA1,&invPosA,&invOrnA);
|
|
||||||
trMul(invPosA,invOrnA,posB1,ornB1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);
|
|
||||||
}
|
|
||||||
float4 planeInConvexPos1; Quaternion planeInConvexOrn1;
|
|
||||||
{
|
|
||||||
float4 invPosB;Quaternion invOrnB;
|
|
||||||
trInverse(posB1,ornB1,&invPosB,&invOrnB);
|
|
||||||
trMul(invPosB,invOrnB,posA1,ornA1,&planeInConvexPos1,&planeInConvexOrn1);
|
|
||||||
}
|
|
||||||
float4 vtx1 = qtRotate(planeInConvexOrn1,-planeNormal1)*radius;
|
|
||||||
float4 vtxInPlane1 = transform(&vtx1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);
|
|
||||||
float distance = dot3F4(planeNormal1,vtxInPlane1) - planeConstant;
|
|
||||||
hasCollision = distance < 0.f;//m_manifoldPtr->getContactBreakingThreshold();
|
|
||||||
if (hasCollision)
|
|
||||||
{
|
|
||||||
float4 vtxInPlaneProjected1 = vtxInPlane1 - distance*planeNormal1;
|
|
||||||
float4 vtxInPlaneWorld1 = transform(&vtxInPlaneProjected1,&posA1,&ornA1);
|
|
||||||
float4 normalOnSurfaceB1 = qtRotate(ornA1,planeNormal1);
|
|
||||||
float4 pOnB1 = vtxInPlaneWorld1+normalOnSurfaceB1*distance;
|
|
||||||
pOnB1.w = distance;
|
|
||||||
|
|
||||||
int dstIdx;
|
|
||||||
AppendInc( nGlobalContactsOut, dstIdx );
|
|
||||||
|
|
||||||
if (dstIdx < numPairs)
|
|
||||||
{
|
|
||||||
__global Contact4* c = &globalContactsOut[dstIdx];
|
|
||||||
c->m_worldNormal = normalOnSurfaceB1;
|
|
||||||
c->m_coeffs = (u32)(0.f*0xffff) | ((u32)(0.7f*0xffff)<<16);
|
|
||||||
c->m_batchIdx = pairIndex;
|
|
||||||
c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
|
|
||||||
c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
|
|
||||||
c->m_worldPos[0] = pOnB1;
|
|
||||||
GET_NPOINTS(*c) = 1;
|
|
||||||
}//if (dstIdx < numPairs)
|
|
||||||
}//if (hasCollision)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
__kernel void clipHullHullKernel( __global const int2* pairs,
|
__kernel void clipHullHullKernel( __global const int2* pairs,
|
||||||
@@ -1052,74 +988,6 @@ __kernel void clipHullHullKernel( __global const int2* pairs,
|
|||||||
|
|
||||||
int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
|
int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
|
||||||
int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
|
int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
|
||||||
|
|
||||||
|
|
||||||
if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
|
|
||||||
collidables[collidableIndexB].m_shapeType == SHAPE_PLANE)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
computeContactPlaneConvex( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA,
|
|
||||||
rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,numPairs);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (collidables[collidableIndexA].m_shapeType == SHAPE_PLANE &&
|
|
||||||
collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
computeContactPlaneConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
|
|
||||||
rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,numPairs);
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
|
|
||||||
collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
|
|
||||||
{
|
|
||||||
//sphere-sphere
|
|
||||||
float radiusA = collidables[collidableIndexA].m_radius;
|
|
||||||
float radiusB = collidables[collidableIndexB].m_radius;
|
|
||||||
float4 posA = rigidBodies[bodyIndexA].m_pos;
|
|
||||||
float4 posB = rigidBodies[bodyIndexB].m_pos;
|
|
||||||
|
|
||||||
float4 diff = posA-posB;
|
|
||||||
float len = length(diff);
|
|
||||||
|
|
||||||
///iff distance positive, don't generate a new contact
|
|
||||||
if ( len <= (radiusA+radiusB))
|
|
||||||
{
|
|
||||||
///distance (negative means penetration)
|
|
||||||
float dist = len - (radiusA+radiusB);
|
|
||||||
float4 normalOnSurfaceB = make_float4(1.f,0.f,0.f,0.f);
|
|
||||||
if (len > 0.00001)
|
|
||||||
{
|
|
||||||
normalOnSurfaceB = diff / len;
|
|
||||||
}
|
|
||||||
float4 contactPosB = posB + normalOnSurfaceB*radiusB;
|
|
||||||
contactPosB.w = dist;
|
|
||||||
|
|
||||||
int dstIdx;
|
|
||||||
AppendInc( nGlobalContactsOut, dstIdx );
|
|
||||||
|
|
||||||
if (dstIdx < numPairs)
|
|
||||||
{
|
|
||||||
__global Contact4* c = &globalContactsOut[dstIdx];
|
|
||||||
c->m_worldNormal = -normalOnSurfaceB;
|
|
||||||
c->m_coeffs = (u32)(0.f*0xffff) | ((u32)(0.7f*0xffff)<<16);
|
|
||||||
c->m_batchIdx = pairIndex;
|
|
||||||
int bodyA = pairs[pairIndex].x;
|
|
||||||
int bodyB = pairs[pairIndex].y;
|
|
||||||
c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;
|
|
||||||
c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;
|
|
||||||
c->m_worldPos[0] = contactPosB;
|
|
||||||
GET_NPOINTS(*c) = 1;
|
|
||||||
}//if (dstIdx < numPairs)
|
|
||||||
}//if ( len <= (radiusA+radiusB))
|
|
||||||
|
|
||||||
return;
|
|
||||||
}//SHAPE_SPHERE SHAPE_SPHERE
|
|
||||||
|
|
||||||
if (hasSeparatingAxis[i])
|
if (hasSeparatingAxis[i])
|
||||||
{
|
{
|
||||||
@@ -1261,10 +1129,6 @@ __kernel void clipCompoundsHullHullKernel( __global const int4* gpuCompoundPai
|
|||||||
|
|
||||||
int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
|
int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
|
||||||
int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
|
int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int numLocalContactsOut = clipHullAgainstHull(gpuCompoundSepNormalsOut[i],
|
int numLocalContactsOut = clipHullAgainstHull(gpuCompoundSepNormalsOut[i],
|
||||||
&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],
|
&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],
|
||||||
|
|||||||
@@ -3,9 +3,13 @@ static const char* satClipKernelsCL= \
|
|||||||
"\n"
|
"\n"
|
||||||
"#define TRIANGLE_NUM_CONVEX_FACES 5\n"
|
"#define TRIANGLE_NUM_CONVEX_FACES 5\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"#define SHAPE_CONVEX_HULL 3\n"
|
||||||
"#define SHAPE_PLANE 4\n"
|
"#define SHAPE_PLANE 4\n"
|
||||||
|
"#define SHAPE_CONCAVE_TRIMESH 5\n"
|
||||||
|
"#define SHAPE_COMPOUND_OF_CONVEX_HULLS 6\n"
|
||||||
"#define SHAPE_SPHERE 7\n"
|
"#define SHAPE_SPHERE 7\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"\n"
|
||||||
"#pragma OPENCL EXTENSION cl_amd_printf : enable\n"
|
"#pragma OPENCL EXTENSION cl_amd_printf : enable\n"
|
||||||
"#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable\n"
|
"#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable\n"
|
||||||
"#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n"
|
"#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n"
|
||||||
@@ -43,12 +47,8 @@ static const char* satClipKernelsCL= \
|
|||||||
"{\n"
|
"{\n"
|
||||||
" float4 m_worldPos[4];\n"
|
" float4 m_worldPos[4];\n"
|
||||||
" float4 m_worldNormal; // w: m_nPoints\n"
|
" float4 m_worldNormal; // w: m_nPoints\n"
|
||||||
"// float m_restituitionCoeff;\n"
|
|
||||||
"// float m_frictionCoeff;\n"
|
|
||||||
" u32 m_coeffs;\n"
|
" u32 m_coeffs;\n"
|
||||||
" u32 m_batchIdx;\n"
|
" u32 m_batchIdx;\n"
|
||||||
"// int m_nPoints;\n"
|
|
||||||
"// int m_padding0;\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
" int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr\n"
|
" int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr\n"
|
||||||
" int m_bodyBPtrAndSignBit;\n"
|
" int m_bodyBPtrAndSignBit;\n"
|
||||||
@@ -874,11 +874,6 @@ static const char* satClipKernelsCL= \
|
|||||||
" contactIdx[2] = idx[2];\n"
|
" contactIdx[2] = idx[2];\n"
|
||||||
" contactIdx[3] = idx[3];\n"
|
" contactIdx[3] = idx[3];\n"
|
||||||
"\n"
|
"\n"
|
||||||
"// if( max00.y < 0.0f )\n"
|
|
||||||
"// contactIdx[0] = (int)max00.x;\n"
|
|
||||||
"\n"
|
|
||||||
" //does this sort happen on GPU too?\n"
|
|
||||||
" //std::sort( contactIdx, contactIdx+4 );\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
" return 4;\n"
|
" return 4;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
@@ -910,7 +905,7 @@ static const char* satClipKernelsCL= \
|
|||||||
" {\n"
|
" {\n"
|
||||||
" localPoints[i] = pointsIn[i];\n"
|
" localPoints[i] = pointsIn[i];\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"// int contactIdx[4] = {-1,-1,-1,-1};\n"
|
"\n"
|
||||||
" int contactIdx[4];// = {-1,-1,-1,-1};\n"
|
" int contactIdx[4];// = {-1,-1,-1,-1};\n"
|
||||||
" contactIdx[0] = -1;\n"
|
" contactIdx[0] = -1;\n"
|
||||||
" contactIdx[1] = -1;\n"
|
" contactIdx[1] = -1;\n"
|
||||||
@@ -956,66 +951,7 @@ static const char* satClipKernelsCL= \
|
|||||||
" *translationOut = transform(&translationB,&translationA,&orientationA);\n"
|
" *translationOut = transform(&translationB,&translationA,&orientationA);\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n"
|
"\n"
|
||||||
"void computeContactPlaneConvex(int pairIndex,\n"
|
|
||||||
" int bodyIndexA, int bodyIndexB, \n"
|
|
||||||
" int collidableIndexA, int collidableIndexB, \n"
|
|
||||||
" __global const BodyData* rigidBodies, \n"
|
|
||||||
" __global const btCollidableGpu* collidables,\n"
|
|
||||||
" __global const btGpuFace* faces,\n"
|
|
||||||
" __global Contact4* restrict globalContactsOut,\n"
|
|
||||||
" counter32_t nGlobalContactsOut,\n"
|
|
||||||
" int numPairs)\n"
|
|
||||||
"{\n"
|
|
||||||
" float4 planeEq = faces[collidables[collidableIndexA].m_shapeIndex].m_plane;\n"
|
|
||||||
" float radius = collidables[collidableIndexB].m_radius;\n"
|
|
||||||
" float4 posA1 = rigidBodies[bodyIndexA].m_pos;\n"
|
|
||||||
" float4 ornA1 = rigidBodies[bodyIndexA].m_quat;\n"
|
|
||||||
" float4 posB1 = rigidBodies[bodyIndexB].m_pos;\n"
|
|
||||||
" float4 ornB1 = rigidBodies[bodyIndexB].m_quat;\n"
|
|
||||||
" \n"
|
|
||||||
" bool hasCollision = false;\n"
|
|
||||||
" float4 planeNormal1 = make_float4(planeEq.x,planeEq.y,planeEq.z,0.f);\n"
|
|
||||||
" float planeConstant = planeEq.w;\n"
|
|
||||||
" float4 convexInPlaneTransPos1; Quaternion convexInPlaneTransOrn1;\n"
|
|
||||||
" {\n"
|
|
||||||
" float4 invPosA;Quaternion invOrnA;\n"
|
|
||||||
" trInverse(posA1,ornA1,&invPosA,&invOrnA);\n"
|
|
||||||
" trMul(invPosA,invOrnA,posB1,ornB1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);\n"
|
|
||||||
" }\n"
|
|
||||||
" float4 planeInConvexPos1; Quaternion planeInConvexOrn1;\n"
|
|
||||||
" {\n"
|
|
||||||
" float4 invPosB;Quaternion invOrnB;\n"
|
|
||||||
" trInverse(posB1,ornB1,&invPosB,&invOrnB);\n"
|
|
||||||
" trMul(invPosB,invOrnB,posA1,ornA1,&planeInConvexPos1,&planeInConvexOrn1); \n"
|
|
||||||
" }\n"
|
|
||||||
" float4 vtx1 = qtRotate(planeInConvexOrn1,-planeNormal1)*radius;\n"
|
|
||||||
" float4 vtxInPlane1 = transform(&vtx1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);\n"
|
|
||||||
" float distance = dot3F4(planeNormal1,vtxInPlane1) - planeConstant;\n"
|
|
||||||
" hasCollision = distance < 0.f;//m_manifoldPtr->getContactBreakingThreshold();\n"
|
|
||||||
" if (hasCollision)\n"
|
|
||||||
" {\n"
|
|
||||||
" float4 vtxInPlaneProjected1 = vtxInPlane1 - distance*planeNormal1;\n"
|
|
||||||
" float4 vtxInPlaneWorld1 = transform(&vtxInPlaneProjected1,&posA1,&ornA1);\n"
|
|
||||||
" float4 normalOnSurfaceB1 = qtRotate(ornA1,planeNormal1);\n"
|
|
||||||
" float4 pOnB1 = vtxInPlaneWorld1+normalOnSurfaceB1*distance;\n"
|
|
||||||
" pOnB1.w = distance;\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
" int dstIdx;\n"
|
|
||||||
" AppendInc( nGlobalContactsOut, dstIdx );\n"
|
|
||||||
" \n"
|
|
||||||
" if (dstIdx < numPairs)\n"
|
|
||||||
" {\n"
|
|
||||||
" __global Contact4* c = &globalContactsOut[dstIdx];\n"
|
|
||||||
" c->m_worldNormal = normalOnSurfaceB1;\n"
|
|
||||||
" c->m_coeffs = (u32)(0.f*0xffff) | ((u32)(0.7f*0xffff)<<16);\n"
|
|
||||||
" c->m_batchIdx = pairIndex;\n"
|
|
||||||
" c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;\n"
|
|
||||||
" c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;\n"
|
|
||||||
" c->m_worldPos[0] = pOnB1;\n"
|
|
||||||
" GET_NPOINTS(*c) = 1;\n"
|
|
||||||
" }//if (dstIdx < numPairs)\n"
|
|
||||||
" }//if (hasCollision)\n"
|
|
||||||
"}\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
"\n"
|
||||||
"__kernel void clipHullHullKernel( __global const int2* pairs, \n"
|
"__kernel void clipHullHullKernel( __global const int2* pairs, \n"
|
||||||
@@ -1054,74 +990,6 @@ static const char* satClipKernelsCL= \
|
|||||||
" \n"
|
" \n"
|
||||||
" int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
|
" int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
|
||||||
" int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
|
" int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
|
||||||
" \n"
|
|
||||||
"\n"
|
|
||||||
" if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&\n"
|
|
||||||
" collidables[collidableIndexB].m_shapeType == SHAPE_PLANE)\n"
|
|
||||||
" {\n"
|
|
||||||
"\n"
|
|
||||||
"\n"
|
|
||||||
" computeContactPlaneConvex( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA, \n"
|
|
||||||
" rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,numPairs);\n"
|
|
||||||
" return;\n"
|
|
||||||
" }\n"
|
|
||||||
"\n"
|
|
||||||
" if (collidables[collidableIndexA].m_shapeType == SHAPE_PLANE &&\n"
|
|
||||||
" collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)\n"
|
|
||||||
" {\n"
|
|
||||||
"\n"
|
|
||||||
"\n"
|
|
||||||
" computeContactPlaneConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB, \n"
|
|
||||||
" rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,numPairs);\n"
|
|
||||||
" return;\n"
|
|
||||||
" \n"
|
|
||||||
" }\n"
|
|
||||||
" \n"
|
|
||||||
" if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&\n"
|
|
||||||
" collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)\n"
|
|
||||||
" {\n"
|
|
||||||
" //sphere-sphere\n"
|
|
||||||
" float radiusA = collidables[collidableIndexA].m_radius;\n"
|
|
||||||
" float radiusB = collidables[collidableIndexB].m_radius;\n"
|
|
||||||
" float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
|
|
||||||
" float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
|
|
||||||
"\n"
|
|
||||||
" float4 diff = posA-posB;\n"
|
|
||||||
" float len = length(diff);\n"
|
|
||||||
" \n"
|
|
||||||
" ///iff distance positive, don't generate a new contact\n"
|
|
||||||
" if ( len <= (radiusA+radiusB))\n"
|
|
||||||
" {\n"
|
|
||||||
" ///distance (negative means penetration)\n"
|
|
||||||
" float dist = len - (radiusA+radiusB);\n"
|
|
||||||
" float4 normalOnSurfaceB = make_float4(1.f,0.f,0.f,0.f);\n"
|
|
||||||
" if (len > 0.00001)\n"
|
|
||||||
" {\n"
|
|
||||||
" normalOnSurfaceB = diff / len;\n"
|
|
||||||
" }\n"
|
|
||||||
" float4 contactPosB = posB + normalOnSurfaceB*radiusB;\n"
|
|
||||||
" contactPosB.w = dist;\n"
|
|
||||||
" \n"
|
|
||||||
" int dstIdx;\n"
|
|
||||||
" AppendInc( nGlobalContactsOut, dstIdx );\n"
|
|
||||||
" \n"
|
|
||||||
" if (dstIdx < numPairs)\n"
|
|
||||||
" {\n"
|
|
||||||
" __global Contact4* c = &globalContactsOut[dstIdx];\n"
|
|
||||||
" c->m_worldNormal = -normalOnSurfaceB;\n"
|
|
||||||
" c->m_coeffs = (u32)(0.f*0xffff) | ((u32)(0.7f*0xffff)<<16);\n"
|
|
||||||
" c->m_batchIdx = pairIndex;\n"
|
|
||||||
" int bodyA = pairs[pairIndex].x;\n"
|
|
||||||
" int bodyB = pairs[pairIndex].y;\n"
|
|
||||||
" c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;\n"
|
|
||||||
" c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;\n"
|
|
||||||
" c->m_worldPos[0] = contactPosB;\n"
|
|
||||||
" GET_NPOINTS(*c) = 1;\n"
|
|
||||||
" }//if (dstIdx < numPairs)\n"
|
|
||||||
" }//if ( len <= (radiusA+radiusB))\n"
|
|
||||||
"\n"
|
|
||||||
" return;\n"
|
|
||||||
" }//SHAPE_SPHERE SHAPE_SPHERE\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
" if (hasSeparatingAxis[i])\n"
|
" if (hasSeparatingAxis[i])\n"
|
||||||
" {\n"
|
" {\n"
|
||||||
@@ -1263,10 +1131,6 @@ static const char* satClipKernelsCL= \
|
|||||||
" \n"
|
" \n"
|
||||||
" int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
|
" int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
|
||||||
" int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
|
" int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
|
||||||
"\n"
|
|
||||||
" \n"
|
|
||||||
"\n"
|
|
||||||
"\n"
|
|
||||||
" \n"
|
" \n"
|
||||||
" int numLocalContactsOut = clipHullAgainstHull(gpuCompoundSepNormalsOut[i],\n"
|
" int numLocalContactsOut = clipHullAgainstHull(gpuCompoundSepNormalsOut[i],\n"
|
||||||
" &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],\n"
|
" &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user