diff --git a/demo/gpudemo/main_opengl3core.cpp b/demo/gpudemo/main_opengl3core.cpp index 32b09a127..83e28d3ee 100644 --- a/demo/gpudemo/main_opengl3core.cpp +++ b/demo/gpudemo/main_opengl3core.cpp @@ -66,6 +66,10 @@ int selectedDemo = 0; GpuDemo::CreateFunc* allDemos[]= { GpuConvexPlaneScene::MyCreateFunc, + + GpuCompoundScene::MyCreateFunc, + GpuCompoundPlaneScene::MyCreateFunc, + GpuConvexScene::MyCreateFunc, ConcaveCompoundScene::MyCreateFunc, @@ -78,7 +82,7 @@ GpuDemo::CreateFunc* allDemos[]= - GpuCompoundScene::MyCreateFunc, + PairBench::MyCreateFunc, diff --git a/demo/gpudemo/rigidbody/GpuCompoundScene.cpp b/demo/gpudemo/rigidbody/GpuCompoundScene.cpp index b799dd0ca..c6c2e2240 100644 --- a/demo/gpudemo/rigidbody/GpuCompoundScene.cpp +++ b/demo/gpudemo/rigidbody/GpuCompoundScene.cpp @@ -24,19 +24,135 @@ void GpuCompoundScene::setupScene(const ConstructionInfo& ci) { - int strideInBytes = 9*sizeof(float); + + createStaticEnvironment(ci); + + int strideInBytes = 9*sizeof(float); int numVertices = sizeof(cube_vertices)/strideInBytes; int numIndices = sizeof(cube_indices)/sizeof(int); + float scaling[4] = {1,1,1,1}; + GLInstanceVertex* cubeVerts = (GLInstanceVertex*)&cube_vertices[0]; + int stride2 = sizeof(GLInstanceVertex); + btAssert(stride2 == strideInBytes); + int index=0; + int colIndex = -1; btAlignedObjectArray vertexArray; btAlignedObjectArray indexArray; + { + int childColIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling); + + + btVector3 childPositions[3] = { + btVector3(0,-2,0), + btVector3(0,0,0), + btVector3(0,2,0) + }; + + + btAlignedObjectArray childShapes; + int numChildShapes = 3; + for (int i=0;im_np->registerCompoundShape(&childShapes); + + } + + + //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); + int shapeId = ci.m_instancingRenderer->registerShape(&vertexArray[0].xyzw[0],vertexArray.size(),&indexArray[0],indexArray.size()); + + btVector4 colors[4] = + { + btVector4(1,0,0,1), + btVector4(0,1,0,1), + btVector4(0,0,1,1), + btVector4(0,1,1,1), + }; + + int curColor = 0; + for (int i=0;iregisterGraphicsInstance(shapeId,position,orn,color,scaling); + int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index); + + index++; + } + } + } + + float camPos[4]={0,0,0};//65.5,4.5,65.5,0}; + //float camPos[4]={1,12.5,1.5,0}; + m_instancingRenderer->setCameraTargetPosition(camPos); + m_instancingRenderer->setCameraDistance(20); + +} + + + + + + +void GpuCompoundScene::createStaticEnvironment(const ConstructionInfo& ci) +{ + + int strideInBytes = 9*sizeof(float); + + //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); int group=1; int mask=1; int index=0; - float scaling[4] = {1,1,1,1}; int colIndex = 0; { @@ -110,106 +226,26 @@ void GpuCompoundScene::setupScene(const ConstructionInfo& ci) } } - GLInstanceVertex* cubeVerts = (GLInstanceVertex*)&cube_vertices[0]; - int stride2 = sizeof(GLInstanceVertex); - btAssert(stride2 == strideInBytes); - - { - int childColIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling); - - - btVector3 childPositions[3] = { - btVector3(0,-2,0), - btVector3(0,0,0), - btVector3(0,2,0) - }; - - - btAlignedObjectArray childShapes; - int numChildShapes = 3; - for (int i=0;im_np->registerCompoundShape(&childShapes); - - } - - //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); - int shapeId = ci.m_instancingRenderer->registerShape(&vertexArray[0].xyzw[0],vertexArray.size(),&indexArray[0],indexArray.size()); - - btVector4 colors[4] = - { - btVector4(1,0,0,1), - btVector4(0,1,0,1), - btVector4(0,0,1,1), - btVector4(0,1,1,1), - }; - - int curColor = 0; - for (int i=0;iregisterGraphicsInstance(shapeId,position,orn,color,scaling); - int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index); - - index++; - } - } - } - - float camPos[4]={0,0,0};//65.5,4.5,65.5,0}; - //float camPos[4]={1,12.5,1.5,0}; - m_instancingRenderer->setCameraTargetPosition(camPos); - m_instancingRenderer->setCameraDistance(20); - } +void GpuCompoundPlaneScene::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.01,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); +} \ No newline at end of file diff --git a/demo/gpudemo/rigidbody/GpuCompoundScene.h b/demo/gpudemo/rigidbody/GpuCompoundScene.h index f90b68bf9..c08e5bbe7 100644 --- a/demo/gpudemo/rigidbody/GpuCompoundScene.h +++ b/demo/gpudemo/rigidbody/GpuCompoundScene.h @@ -22,6 +22,29 @@ public: virtual void setupScene(const ConstructionInfo& ci); + virtual void createStaticEnvironment(const ConstructionInfo& ci); + }; + +class GpuCompoundPlaneScene : public GpuCompoundScene +{ +public: + + GpuCompoundPlaneScene(){} + virtual ~GpuCompoundPlaneScene(){} + virtual const char* getName() + { + return "GpuCompoundPlane"; + } + + static GpuDemo* MyCreateFunc() + { + GpuDemo* demo = new GpuCompoundPlaneScene; + return demo; + } + + virtual void createStaticEnvironment(const ConstructionInfo& ci); + +}; #endif //GPU_COMPOUND_SCENE_H diff --git a/opencl/gpu_sat/host/ConvexHullContact.cpp b/opencl/gpu_sat/host/ConvexHullContact.cpp index a80af7663..35a1bea9d 100644 --- a/opencl/gpu_sat/host/ConvexHullContact.cpp +++ b/opencl/gpu_sat/host/ConvexHullContact.cpp @@ -387,6 +387,14 @@ int extractManifoldSequentialGlobal( const float4* p, int nPoints, const float4& } + + + + + + + + void computeContactPlaneConvex(int pairIndex, int bodyIndexA, int bodyIndexB, int collidableIndexA, int collidableIndexB, @@ -517,6 +525,148 @@ void computeContactPlaneConvex(int pairIndex, // printf("computeContactPlaneConvex\n"); } + + + +void computeContactPlaneCompound(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 shapeTypeB = collidables[collidableIndexB].m_shapeType; + btAssert(shapeTypeB == SHAPE_COMPOUND_OF_CONVEX_HULLS); + + + + 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); + btVector3 planeNormalWorld = quatRotate(ornA,planeNormal); + 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; + btTransform convexInPlane; + convexInPlane = planeTransform.inverse() * convexWorldTransform; + + btVector3 planeNormalInConvex = planeInConvex.getBasis()*-planeNormal; + float maxDot = -1e30; + int hitVertex=-1; + btVector3 hitVtx; + +#define MAX_PLANE_CONVEX_POINTS 64 + + btVector3 contactPoints[MAX_PLANE_CONVEX_POINTS]; + int numPoints = 0; + + btInt4 contactIdx; + contactIdx.s[0] = 0; + contactIdx.s[1] = 1; + contactIdx.s[2] = 2; + contactIdx.s[3] = 3; + + for (int i=0;im_numVertices;i++) + { + btVector3 vtx = convexVertices[hullB->m_vertexOffset+i]; + float curDot = vtx.dot(planeNormalInConvex); + + + if (curDot>maxDot) + { + hitVertex=i; + maxDot=curDot; + hitVtx = vtx; + //make sure the deepest points is always included + if (numPoints==MAX_PLANE_CONVEX_POINTS) + numPoints--; + } + + if (numPoints4) + { + numReducedPoints = extractManifoldSequentialGlobal( contactPoints, numPoints, planeNormalInConvex, &contactIdx); + } + int dstIdx; +// dstIdx = nGlobalContactsOut++;//AppendInc( nGlobalContactsOut, dstIdx ); + + if (numReducedPoints>0) + { + if (nGlobalContactsOut < maxContactCapacity) + { + dstIdx=nGlobalContactsOut; + nGlobalContactsOut++; + + btContact4* c = &globalContactsOut[dstIdx]; + c->m_worldNormal = planeNormalWorld; + 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; + for (int i=0;im_worldPos[i] = pOnB1; + } + c->m_worldNormal[3] = numReducedPoints; + }//if (dstIdx < numPairs) + } + + + +// printf("computeContactPlaneConvex\n"); +} + + + + + void computeContactSphereConvex(int pairIndex, int bodyIndexA, int bodyIndexB, int collidableIndexA, int collidableIndexB, @@ -785,7 +935,26 @@ void GpuSatCollision::computeConvexConvexContactsGPUSAT( const btOpenCLArraym_numVertices;i++)\n" +" {\n" +" float4 vtx = convexVertices[hullB->m_vertexOffset+i];\n" +" float curDot = dot(vtx,planeNormalInConvex);\n" +"\n" +"\n" +" if (curDot>maxDot)\n" +" {\n" +" hitVertex=i;\n" +" maxDot=curDot;\n" +" hitVtx = vtx;\n" +" //make sure the deepest points is always included\n" +" if (numPoints==MAX_PLANE_CONVEX_POINTS)\n" +" numPoints--;\n" +" }\n" +"\n" +" if (numPoints4)\n" +" //{\n" +"// numReducedPoints = extractManifoldSequentialGlobal( contactPoints, numPoints, planeNormalInConvex, &contactIdx);\n" +" //}\n" +"\n" +" if (numReducedPoints>0)\n" +" {\n" +" int dstIdx;\n" +" AppendInc( nGlobalContactsOut, dstIdx );\n" +"\n" +" if (dstIdx < maxContactCapacity)\n" +" {\n" +" __global Contact4* c = &globalContactsOut[dstIdx];\n" +" c->m_worldNormal = planeNormalWorld;\n" +" //c->setFrictionCoeff(0.7);\n" +" //c->setRestituitionCoeff(0.f);\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" +"\n" +" switch (numReducedPoints)\n" +" {\n" +" case 4:\n" +" c->m_worldPos[3] = contactPoints[contactIdx.w];\n" +" case 3:\n" +" c->m_worldPos[2] = contactPoints[contactIdx.z];\n" +" case 2:\n" +" c->m_worldPos[1] = contactPoints[contactIdx.y];\n" +" case 1:\n" +" c->m_worldPos[0] = contactPoints[contactIdx.x];\n" +" default:\n" +" {\n" +" }\n" +" };\n" +" \n" +" GET_NPOINTS(*c) = numReducedPoints;\n" +" }//if (dstIdx < numPairs)\n" +" } \n" +"}\n" +"\n" +"\n" +"void computeContactPlaneSphere(int pairIndex,\n" " int bodyIndexA, int bodyIndexB, \n" " int collidableIndexA, int collidableIndexB, \n" " __global const BodyData* rigidBodies, \n" @@ -559,8 +699,6 @@ static const char* primitiveContactsKernelsCL= \ "}\n" "\n" "\n" -"\n" -"\n" "__kernel void primitiveContactsKernel( __global const int2* pairs, \n" " __global const BodyData* rigidBodies, \n" " __global const btCollidableGpu* collidables,\n" @@ -596,27 +734,61 @@ static const char* primitiveContactsKernelsCL= \ " int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n" " int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n" " \n" +" if (collidables[collidableIndexA].m_shapeType == SHAPE_PLANE &&\n" +" collidables[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)\n" +" {\n" "\n" -" if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&\n" +" float4 posB;\n" +" posB = rigidBodies[bodyIndexB].m_pos;\n" +" Quaternion ornB;\n" +" ornB = rigidBodies[bodyIndexB].m_quat;\n" +" computeContactPlaneConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB, \n" +" rigidBodies,collidables,convexShapes,vertices,indices,\n" +" faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity, posB,ornB);\n" +" return;\n" +" }\n" +"\n" +"\n" +" if (collidables[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&\n" " collidables[collidableIndexB].m_shapeType == SHAPE_PLANE)\n" " {\n" "\n" +" float4 posA;\n" +" posA = rigidBodies[bodyIndexA].m_pos;\n" +" Quaternion ornA;\n" +" ornA = rigidBodies[bodyIndexA].m_quat;\n" +"\n" "\n" " computeContactPlaneConvex( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA, \n" -" rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity);\n" +" rigidBodies,collidables,convexShapes,vertices,indices,\n" +" faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,posA,ornA);\n" +"\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,maxContactCapacity);\n" +" computeContactPlaneSphere(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB, \n" +" rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity);\n" " return;\n" -" \n" " }\n" +"\n" +"\n" +" if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&\n" +" collidables[collidableIndexB].m_shapeType == SHAPE_PLANE)\n" +" {\n" +"\n" +"\n" +" computeContactPlaneSphere( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA, \n" +" rigidBodies,collidables,\n" +" faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity);\n" +"\n" +" return;\n" +" }\n" +"\n" +" \n" +"\n" " \n" " if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&\n" " collidables[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)\n" @@ -651,6 +823,7 @@ static const char* primitiveContactsKernelsCL= \ " \n" " \n" " \n" +" \n" " \n" " \n" " if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&\n" @@ -774,6 +947,24 @@ static const char* primitiveContactsKernelsCL= \ " int shapeTypeB = collidables[collidableIndexB].m_shapeType;\n" "\n" " int pairIndex = i;\n" +" if ((shapeTypeA == SHAPE_PLANE) && (shapeTypeB==SHAPE_CONVEX_HULL))\n" +" {\n" +"\n" +" computeContactPlaneConvex( pairIndex, bodyIndexA,bodyIndexB, collidableIndexA,collidableIndexB, \n" +" rigidBodies,collidables,convexShapes,vertices,indices,\n" +" faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,posB,ornB);\n" +" return;\n" +" }\n" +"\n" +" if ((shapeTypeA == SHAPE_CONVEX_HULL) && (shapeTypeB==SHAPE_PLANE))\n" +" {\n" +"\n" +" computeContactPlaneConvex( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA, \n" +" rigidBodies,collidables,convexShapes,vertices,indices,\n" +" faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,posA,ornA);\n" +" return;\n" +" }\n" +"\n" " if ((shapeTypeA == SHAPE_CONVEX_HULL) && (shapeTypeB == SHAPE_SPHERE))\n" " {\n" " float4 spherePos = rigidBodies[bodyIndexB].m_pos;\n"