plane-convex prep

This commit is contained in:
erwin coumans
2013-04-08 09:09:29 -07:00
parent c8fcf779bb
commit 0a721ce5a3
5 changed files with 226 additions and 21 deletions

View File

@@ -38,9 +38,9 @@ public:
:useOpenCL(true),
preferredOpenCLPlatformIndex(-1),
preferredOpenCLDeviceIndex(-1),
arraySizeX(11),
arraySizeY(11),
arraySizeZ(11),
arraySizeX(1),
arraySizeY(1),
arraySizeZ(1),
m_useConcaveMesh(false),
gapX(14.3),
gapY(14.0),

View File

@@ -65,6 +65,8 @@ btAlignedObjectArray<const char*> demoNames;
int selectedDemo = 0;
GpuDemo::CreateFunc* allDemos[]=
{
GpuConvexPlaneScene::MyCreateFunc,
GpuConvexScene::MyCreateFunc,
ConcaveCompoundScene::MyCreateFunc,
ConcaveSphereScene::MyCreateFunc,
@@ -74,7 +76,7 @@ GpuDemo::CreateFunc* allDemos[]=
GpuSphereScene::MyCreateFunc,
GpuConvexScene::MyCreateFunc,
GpuCompoundScene::MyCreateFunc,
PairBench::MyCreateFunc,

View File

@@ -21,29 +21,20 @@
void GpuConvexScene::setupScene(const ConstructionInfo& ci)
{
createStaticEnvironment(ci);
int strideInBytes = 9*sizeof(float);
int numVertices = sizeof(cube_vertices)/strideInBytes;
int numIndices = sizeof(cube_indices)/sizeof(int);
//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);
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
int group=1;
int mask=1;
int index=10;
{
btVector4 scaling(400,1,400,1);
int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
btVector3 position(0,0,0);
btQuaternion orn(0,0,0,1);
btVector4 color(0,0,1,1);
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index);
index++;
}
@@ -59,6 +50,7 @@ void GpuConvexScene::setupScene(const ConstructionInfo& ci)
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->registerSphereShape(1);
for (int i=0;i<ci.arraySizeX;i++)
{
for (int j=0;j<ci.arraySizeY;j++)
@@ -86,7 +78,7 @@ void GpuConvexScene::setupScene(const ConstructionInfo& ci)
float camPos[4]={ci.arraySizeX,ci.arraySizeY/2,ci.arraySizeZ,0};
//float camPos[4]={1,12.5,1.5,0};
m_instancingRenderer->setCameraTargetPosition(camPos);
m_instancingRenderer->setCameraDistance(120);
m_instancingRenderer->setCameraDistance(40);
char msg[1024];
@@ -94,3 +86,53 @@ void GpuConvexScene::setupScene(const ConstructionInfo& ci)
sprintf(msg,"Num objects = %d",numInstances);
ci.m_gui->setStatusBarMessage(msg,true);
}
void GpuConvexScene::createStaticEnvironment(const ConstructionInfo& ci)
{
int strideInBytes = 9*sizeof(float);
int numVertices = sizeof(cube_vertices)/strideInBytes;
int numIndices = sizeof(cube_indices)/sizeof(int);
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
int group=1;
int mask=1;
int index=0;
{
btVector4 scaling(400,1,400,1);
int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
btVector3 position(0,0,0);
btQuaternion orn(0,0,0,1);
btVector4 color(0,0,1,1);
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index);
}
}
void GpuConvexPlaneScene::createStaticEnvironment(const ConstructionInfo& ci)
{
int index=0;
btVector3 normal(0,1,0);
float constant=0.f;
int colIndex = m_data->m_np->registerPlaneShape(normal,constant);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
btVector3 position(0,0,0);
btQuaternion orn(0,0,0,1);
// btQuaternion orn(btVector3(1,0,0),0.3);
btVector4 color(0,0,1,1);
btVector4 scaling(100,0.0001,100,1);
int strideInBytes = 9*sizeof(float);
int numVertices = sizeof(cube_vertices)/strideInBytes;
int numIndices = sizeof(cube_indices)/sizeof(int);
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index);
}

View File

@@ -22,6 +22,30 @@ public:
virtual void setupScene(const ConstructionInfo& ci);
virtual void createStaticEnvironment(const ConstructionInfo& ci);
};
class GpuConvexPlaneScene : public GpuConvexScene
{
public:
GpuConvexPlaneScene(){}
virtual ~GpuConvexPlaneScene(){}
virtual const char* getName()
{
return "GRBConvexPlane";
}
static GpuDemo* MyCreateFunc()
{
GpuDemo* demo = new GpuConvexPlaneScene;
return demo;
}
virtual void createStaticEnvironment(const ConstructionInfo& ci);
};
#endif //GPU_CONVEX_SCENE_H

View File

@@ -291,6 +291,125 @@ inline bool IsPointInPolygon(const float4& p,
return true;
}
void computeContactPlaneConvex(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)
{
int shapeIndex = collidables[collidableIndexB].m_shapeIndex;
const btConvexPolyhedronCL* hullB = &convexShapes[shapeIndex];
btVector3 posB = rigidBodies[bodyIndexB].m_pos;
btQuaternion ornB = rigidBodies[bodyIndexB].m_quat;
btVector3 posA = rigidBodies[bodyIndexA].m_pos;
btQuaternion ornA = rigidBodies[bodyIndexA].m_quat;
int numContactsOut = 0;
int numWorldVertsB1= 0;
btVector3 planeEq = faces[collidables[collidableIndexA].m_shapeIndex].m_plane;
btVector3 planeNormal(planeEq.x,planeEq.y,planeEq.z);
float planeConstant = planeEq.w;
btTransform convexWorldTransform;
convexWorldTransform.setIdentity();
convexWorldTransform.setOrigin(posB);
convexWorldTransform.setRotation(ornB);
btTransform planeTransform;
planeTransform.setIdentity();
planeTransform.setOrigin(posA);
planeTransform.setRotation(ornA);
btTransform planeInConvex;
planeInConvex= convexWorldTransform.inverse() * planeTransform;
btVector3 planeNormalInConvex = planeInConvex.getBasis()*-planeNormal;
float maxDot = -1e30;
int hitVertex=-1;
btVector3 hitVtx;
for (int i=0;i<hullB->m_numVertices;i++)
{
btVector3 vtx = convexVertices[hullB->m_vertexOffset+i];
float curDot = vtx.dot(planeNormalInConvex);
if (curDot>maxDot)
{
hitVertex=i;
maxDot=curDot;
hitVtx = vtx;
}
}
btVector3 hitVtxWorld = convexWorldTransform*hitVtx;
float dist = 0;
int dstIdx;
// dstIdx = nGlobalContactsOut++;//AppendInc( nGlobalContactsOut, dstIdx );
if (nGlobalContactsOut < maxContactCapacity)
{
dstIdx=nGlobalContactsOut;
nGlobalContactsOut++;
btContact4* c = &globalContactsOut[dstIdx];
c->m_worldNormal = -planeNormalInConvex;
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;
btVector3 pOnB1 = hitVtxWorld;
pOnB1[3] = -0.01f;
c->m_worldPos[0] = pOnB1;
int numPoints = 1;
c->m_worldNormal[3] = numPoints;
}//if (dstIdx < numPairs)
/*
int closestFaceB=-1;
float dmax = -FLT_MAX;
{
for(int face=0;face<hullB->m_numFaces;face++)
{
const float4 Normal = make_float4(faces[hullB->m_faceOffset+face].m_plane.x,
faces[hullB->m_faceOffset+face].m_plane.y, faces[hullB->m_faceOffset+face].m_plane.z,0.f);
const float4 WorldNormal = quatRotate(ornB, Normal);
float d = dot3F4(WorldNormal,separatingNormal);
if (d > dmax)
{
dmax = d;
closestFaceB = face;
}
}
}
{
const btGpuFace polyB = faces[hullB->m_faceOffset+closestFaceB];
const int numVertices = polyB.m_numIndices;
for(int e0=0;e0<numVertices;e0++)
{
const float4 b = vertices[hullB->m_vertexOffset+indices[polyB.m_indexOffset+e0]];
worldVertsB1[pairIndex*capacityWorldVerts+numWorldVertsB1++] = transform(b,posB,ornB);
}
}
*/
printf("computeContactPlaneConvex\n");
}
void computeContactSphereConvex(int pairIndex,
int bodyIndexA, int bodyIndexB,
@@ -478,7 +597,7 @@ void GpuSatCollision::computeConvexConvexContactsGPUSAT( const btOpenCLArray<btI
return;
//#define CHECK_ON_HOST
#define CHECK_ON_HOST
#ifdef CHECK_ON_HOST
btAlignedObjectArray<btYetAnotherAabb> hostAabbs;
clAabbsWS.copyToHost(hostAabbs);
@@ -542,6 +661,24 @@ void GpuSatCollision::computeConvexConvexContactsGPUSAT( const btOpenCLArray<btI
}
if (hostCollidables[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&
hostCollidables[collidableIndexB].m_shapeType == SHAPE_PLANE)
{
computeContactPlaneConvex(i,bodyIndexB,bodyIndexA,collidableIndexB,collidableIndexA,&hostBodyBuf[0],
&hostCollidables[0],&hostConvexData[0],&hostVertices[0],&hostIndices[0],&hostFaces[0],&hostContacts[0],nContacts,nPairs);
printf("convex-plane\n");
}
if (hostCollidables[collidableIndexA].m_shapeType == SHAPE_PLANE &&
hostCollidables[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)
{
computeContactPlaneConvex(i,bodyIndexA,bodyIndexB,collidableIndexA,collidableIndexB,&hostBodyBuf[0],
&hostCollidables[0],&hostConvexData[0],&hostVertices[0],&hostIndices[0],&hostFaces[0],&hostContacts[0],nContacts,nPairs);
printf("plane-convex\n");
}
}