InternalDataRenderer() : m_cameraPosition(b3Vector3(0,0,0)), m_cameraTargetPosition(b3Vector3(15,2,-24)), m_cameraDistance(150), m_cameraUp(0,1,0), m_azi(100.f),//135.f), //m_ele(25.f), m_ele(25.f), m_mouseInitialized(false), m_mouseButton(0) { }
void b3DynamicBvhBroadphase::getBroadphaseAabb(b3Vector3& aabbMin,b3Vector3& aabbMax) const { B3_ATTRIBUTE_ALIGNED16(b3DbvtVolume) bounds; if(!m_sets[0].empty()) if(!m_sets[1].empty()) b3Merge( m_sets[0].m_root->volume, m_sets[1].m_root->volume,bounds); else bounds=m_sets[0].m_root->volume; else if(!m_sets[1].empty()) bounds=m_sets[1].m_root->volume; else bounds=b3DbvtVolume::FromCR(b3Vector3(0,0,0),0); aabbMin=bounds.Mins(); aabbMax=bounds.Maxs(); }
int b3GpuNarrowPhase::registerConvexHullShape(const float* vertices, int strideInBytes, int numVertices, const float* scaling) { b3AlignedObjectArray<b3Vector3> verts; unsigned char* vts = (unsigned char*) vertices; for (int i=0;i<numVertices;i++) { float* vertex = (float*) &vts[i*strideInBytes]; verts.push_back(b3Vector3(vertex[0]*scaling[0],vertex[1]*scaling[1],vertex[2]*scaling[2])); } b3ConvexUtility* utilPtr = new b3ConvexUtility(); bool merge = true; if (numVertices) { utilPtr->initializePolyhedralFeatures(&verts[0],verts.size(),merge); } int collidableIndex = registerConvexHullShape(utilPtr); return collidableIndex; }
int b3GpuDynamicsWorld::findOrRegisterCollisionShape(const btCollisionShape* colShape) { int index = m_uniqueShapes.findLinearSearch(colShape); if (index==m_uniqueShapes.size()) { if (colShape->isPolyhedral()) { m_uniqueShapes.push_back(colShape); btPolyhedralConvexShape* convex = (btPolyhedralConvexShape*)colShape; int numVertices=convex->getNumVertices(); int strideInBytes=sizeof(btVector3); btAlignedObjectArray<btVector3> tmpVertices; tmpVertices.resize(numVertices); for (int i=0;i<numVertices;i++) convex->getVertex(i,tmpVertices[i]); const float scaling[4]={1,1,1,1}; //bool noHeightField=true; //int gpuShapeIndex = m_np->registerConvexHullShape(&tmpVertices[0].getX(), strideInBytes, numVertices, scaling); const float* verts = numVertices? &tmpVertices[0].getX() : 0; int gpuShapeIndex = m_np->registerConvexHullShape(verts,strideInBytes, numVertices, scaling); m_uniqueShapeMapping.push_back(gpuShapeIndex); } else { if (colShape->getShapeType()==TRIANGLE_MESH_SHAPE_PROXYTYPE) { m_uniqueShapes.push_back(colShape); btBvhTriangleMeshShape* trimesh = (btBvhTriangleMeshShape*) colShape; btStridingMeshInterface* meshInterface = trimesh->getMeshInterface(); b3AlignedObjectArray<b3Vector3> vertices; b3AlignedObjectArray<int> indices; btVector3 trimeshScaling(1,1,1); for (int partId=0;partId<meshInterface->getNumSubParts();partId++) { const unsigned char *vertexbase = 0; int numverts = 0; PHY_ScalarType type = PHY_INTEGER; int stride = 0; const unsigned char *indexbase = 0; int indexstride = 0; int numfaces = 0; PHY_ScalarType indicestype = PHY_INTEGER; //PHY_ScalarType indexType=0; b3Vector3 triangleVerts[3]; meshInterface->getLockedReadOnlyVertexIndexBase(&vertexbase,numverts, type,stride,&indexbase,indexstride,numfaces,indicestype,partId); btVector3 aabbMin,aabbMax; for (int triangleIndex = 0 ; triangleIndex < numfaces;triangleIndex++) { unsigned int* gfxbase = (unsigned int*)(indexbase+triangleIndex*indexstride); for (int j=2;j>=0;j--) { int graphicsindex = indicestype==PHY_SHORT?((unsigned short*)gfxbase)[j]:gfxbase[j]; if (type == PHY_FLOAT) { float* graphicsbase = (float*)(vertexbase+graphicsindex*stride); triangleVerts[j] = b3Vector3( graphicsbase[0]*trimeshScaling.getX(), graphicsbase[1]*trimeshScaling.getY(), graphicsbase[2]*trimeshScaling.getZ()); } else { double* graphicsbase = (double*)(vertexbase+graphicsindex*stride); triangleVerts[j] = b3Vector3( btScalar(graphicsbase[0]*trimeshScaling.getX()), btScalar(graphicsbase[1]*trimeshScaling.getY()), btScalar(graphicsbase[2]*trimeshScaling.getZ())); } } vertices.push_back(triangleVerts[0]); vertices.push_back(triangleVerts[1]); vertices.push_back(triangleVerts[2]); indices.push_back(indices.size()); indices.push_back(indices.size()); indices.push_back(indices.size()); } } //GraphicsShape* gfxShape = 0;//b3BulletDataExtractor::createGraphicsShapeFromWavefrontObj(objData); //GraphicsShape* gfxShape = b3BulletDataExtractor::createGraphicsShapeFromConvexHull(&sUnitSpherePoints[0],MY_UNITSPHERE_POINTS); float meshScaling[4] = {1,1,1,1}; //int shapeIndex = renderer.registerShape(gfxShape->m_vertices,gfxShape->m_numvertices,gfxShape->m_indices,gfxShape->m_numIndices); //float groundPos[4] = {0,0,0,0}; //renderer.registerGraphicsInstance(shapeIndex,groundPos,rotOrn,color,meshScaling); if (vertices.size() && indices.size()) { int gpuShapeIndex = m_np->registerConcaveMesh(&vertices,&indices, meshScaling); m_uniqueShapeMapping.push_back(gpuShapeIndex); } else { printf("Error: no vertices in mesh in b3GpuDynamicsWorld::addRigidBody\n"); index = -1; b3Assert(0); } } else { if (colShape->getShapeType()==COMPOUND_SHAPE_PROXYTYPE) { btCompoundShape* compound = (btCompoundShape*) colShape; b3AlignedObjectArray<b3GpuChildShape> childShapes; for (int i=0;i<compound->getNumChildShapes();i++) { //for now, only support polyhedral child shapes b3Assert(compound->getChildShape(i)->isPolyhedral()); b3GpuChildShape child; child.m_shapeIndex = findOrRegisterCollisionShape(compound->getChildShape(i)); btVector3 pos = compound->getChildTransform(i).getOrigin(); btQuaternion orn = compound->getChildTransform(i).getRotation(); for (int v=0;v<4;v++) { child.m_childPosition[v] = pos[v]; child.m_childOrientation[v] = orn[v]; } childShapes.push_back(child); } index = m_uniqueShapes.size(); m_uniqueShapes.push_back(colShape); int gpuShapeIndex = m_np->registerCompoundShape(&childShapes); m_uniqueShapeMapping.push_back(gpuShapeIndex); /*printf("Error: unsupported compound type (%d) in b3GpuDynamicsWorld::addRigidBody\n",colShape->getShapeType()); index = -1; b3Assert(0); */ } else { if (colShape->getShapeType()==SPHERE_SHAPE_PROXYTYPE) { m_uniqueShapes.push_back(colShape); btSphereShape* sphere = (btSphereShape*)colShape; int gpuShapeIndex = m_np->registerSphereShape(sphere->getRadius()); m_uniqueShapeMapping.push_back(gpuShapeIndex); } else { if (colShape->getShapeType()==STATIC_PLANE_PROXYTYPE) { m_uniqueShapes.push_back(colShape); btStaticPlaneShape* plane = (btStaticPlaneShape*)colShape; int gpuShapeIndex = m_np->registerPlaneShape((b3Vector3&)plane->getPlaneNormal(),plane->getPlaneConstant()); m_uniqueShapeMapping.push_back(gpuShapeIndex); } else { printf("Error: unsupported shape type (%d) in b3GpuDynamicsWorld::addRigidBody\n",colShape->getShapeType()); index = -1; b3Assert(0); } } } } } } return index; }
void ParticleDemo::setupScene(const ConstructionInfo& ci) { initCL(ci.preferredOpenCLDeviceIndex,ci.preferredOpenCLPlatformIndex); int numParticles = NUM_PARTICLES_X*NUM_PARTICLES_Y*NUM_PARTICLES_Z; int maxObjects = NUM_PARTICLES_X*NUM_PARTICLES_Y*NUM_PARTICLES_Z+1024; int maxPairsSmallProxy = 32; float radius = 3.f*m_data->m_simParamCPU[0].m_particleRad; m_data->m_broadphaseGPU = new b3GpuSapBroadphase(m_clData->m_clContext ,m_clData->m_clDevice,m_clData->m_clQueue);//overlappingPairCache,b3Vector3(4.f, 4.f, 4.f), 128, 128, 128,maxObjects, maxObjects, maxPairsSmallProxy, 100.f, 128, /*m_data->m_broadphaseGPU = new b3GridBroadphaseCl(overlappingPairCache,b3Vector3(radius,radius,radius), 128, 128, 128, maxObjects, maxObjects, maxPairsSmallProxy, 100.f, 128, m_clData->m_clContext ,m_clData->m_clDevice,m_clData->m_clQueue); */ m_data->m_velocitiesGPU = new b3OpenCLArray<b3Vector3>(m_clData->m_clContext,m_clData->m_clQueue,numParticles); m_data->m_velocitiesCPU.resize(numParticles); for (int i=0;i<numParticles;i++) { m_data->m_velocitiesCPU[i].setValue(0,0,0); } m_data->m_velocitiesGPU->copyFromHost(m_data->m_velocitiesCPU); m_data->m_simParamGPU = new b3OpenCLArray<b3SimParams>(m_clData->m_clContext,m_clData->m_clQueue,1,false); m_data->m_simParamGPU->copyFromHost(m_data->m_simParamCPU); cl_int pErrNum; cl_program prog = b3OpenCLUtils::compileCLProgramFromString(m_clData->m_clContext,m_clData->m_clDevice,particleKernelsString,0,"",INTEROPKERNEL_SRC_PATH); m_data->m_updatePositionsKernel = b3OpenCLUtils::compileCLKernelFromString(m_clData->m_clContext, m_clData->m_clDevice,particleKernelsString, "updatePositionsKernel" ,&pErrNum,prog); oclCHECKERROR(pErrNum, CL_SUCCESS); m_data->m_updatePositionsKernel2 = b3OpenCLUtils::compileCLKernelFromString(m_clData->m_clContext, m_clData->m_clDevice,particleKernelsString, "integrateMotionKernel" ,&pErrNum,prog); oclCHECKERROR(pErrNum, CL_SUCCESS); m_data->m_updateAabbsKernel= b3OpenCLUtils::compileCLKernelFromString(m_clData->m_clContext, m_clData->m_clDevice,particleKernelsString, "updateAabbsKernel" ,&pErrNum,prog); oclCHECKERROR(pErrNum, CL_SUCCESS); m_data->m_collideParticlesKernel = b3OpenCLUtils::compileCLKernelFromString(m_clData->m_clContext, m_clData->m_clDevice,particleKernelsString, "collideParticlesKernel" ,&pErrNum,prog); oclCHECKERROR(pErrNum, CL_SUCCESS); m_instancingRenderer = ci.m_instancingRenderer; int strideInBytes = 9*sizeof(float); bool pointSprite = true; int shapeId =-1; if (pointSprite) { int numVertices = sizeof(point_sphere_vertices)/strideInBytes; int numIndices = sizeof(point_sphere_indices)/sizeof(int); shapeId = m_instancingRenderer->registerShape(&point_sphere_vertices[0],numVertices,point_sphere_indices,numIndices,B3_GL_POINTS); } else { int numVertices = sizeof(low_sphere_vertices)/strideInBytes; int numIndices = sizeof(low_sphere_indices)/sizeof(int); shapeId = m_instancingRenderer->registerShape(&low_sphere_vertices[0],numVertices,low_sphere_indices,numIndices); } float position[4] = {0,0,0,0}; float quaternion[4] = {0,0,0,1}; float color[4]={1,0,0,1}; float scaling[4] = {0.023,0.023,0.023,1}; int userIndex = 0; for (int x=0;x<NUM_PARTICLES_X;x++) { for (int y=0;y<NUM_PARTICLES_Y;y++) { for (int z=0;z<NUM_PARTICLES_Z;z++) { float rad = m_data->m_simParamCPU[0].m_particleRad; position[0] = x*(rad*3); position[1] = y*(rad*3); position[2] = z*(rad*3); color[0] = float(x)/float(NUM_PARTICLES_X); color[1] = float(y)/float(NUM_PARTICLES_Y); color[2] = float(z)/float(NUM_PARTICLES_Z); int id = m_instancingRenderer->registerGraphicsInstance(shapeId,position,quaternion,color,scaling); void* userPtr = (void*)userIndex; int collidableIndex = userIndex; b3Vector3 aabbMin,aabbMax; b3Vector3 particleRadius(rad,rad,rad); aabbMin = b3Vector3(position[0],position[1],position[2])-particleRadius; aabbMax = b3Vector3(position[0],position[1],position[2])+particleRadius; m_data->m_broadphaseGPU->createProxy(aabbMin,aabbMax,collidableIndex,1,1); userIndex++; } } } m_data->m_broadphaseGPU->writeAabbsToGpu(); float camPos[4]={1.5,0.5,2.5,0}; m_instancingRenderer->setCameraTargetPosition(camPos); m_instancingRenderer->setCameraDistance(4); m_instancingRenderer->writeTransforms(); }
void GLInstancingRenderer::setCameraTargetPosition(float cameraPos[4]) { m_data->m_cameraTargetPosition = b3Vector3(cameraPos[0],cameraPos[1],cameraPos[2]); }
int b3GpuNarrowPhase::registerConcaveMeshShape(b3AlignedObjectArray<b3Vector3>* vertices, b3AlignedObjectArray<int>* indices,b3Collidable& col, const float* scaling1) { b3Vector3 scaling(scaling1[0],scaling1[1],scaling1[2]); m_data->m_convexData->resize(m_data->m_numAcceleratedShapes+1); m_data->m_convexPolyhedra.resize(m_data->m_numAcceleratedShapes+1); b3ConvexPolyhedronCL& convex = m_data->m_convexPolyhedra.at(m_data->m_convexPolyhedra.size()-1); convex.mC = b3Vector3(0,0,0); convex.mE = b3Vector3(0,0,0); convex.m_extents= b3Vector3(0,0,0); convex.m_localCenter = b3Vector3(0,0,0); convex.m_radius = 0.f; convex.m_numUniqueEdges = 0; int edgeOffset = m_data->m_uniqueEdges.size(); convex.m_uniqueEdgesOffset = edgeOffset; int faceOffset = m_data->m_convexFaces.size(); convex.m_faceOffset = faceOffset; convex.m_numFaces = indices->size()/3; m_data->m_convexFaces.resize(faceOffset+convex.m_numFaces); m_data->m_convexIndices.reserve(convex.m_numFaces*3); for (int i=0;i<convex.m_numFaces;i++) { if (i%256==0) { //printf("i=%d out of %d", i,convex.m_numFaces); } b3Vector3 vert0(vertices->at(indices->at(i*3))*scaling); b3Vector3 vert1(vertices->at(indices->at(i*3+1))*scaling); b3Vector3 vert2(vertices->at(indices->at(i*3+2))*scaling); b3Vector3 normal = ((vert1-vert0).cross(vert2-vert0)).normalize(); b3Scalar c = -(normal.dot(vert0)); m_data->m_convexFaces[convex.m_faceOffset+i].m_plane[0] = normal.getX(); m_data->m_convexFaces[convex.m_faceOffset+i].m_plane[1] = normal.getY(); m_data->m_convexFaces[convex.m_faceOffset+i].m_plane[2] = normal.getZ(); m_data->m_convexFaces[convex.m_faceOffset+i].m_plane[3] = c; int indexOffset = m_data->m_convexIndices.size(); int numIndices = 3; m_data->m_convexFaces[convex.m_faceOffset+i].m_numIndices = numIndices; m_data->m_convexFaces[convex.m_faceOffset+i].m_indexOffset = indexOffset; m_data->m_convexIndices.resize(indexOffset+numIndices); for (int p=0;p<numIndices;p++) { int vi = indices->at(i*3+p); m_data->m_convexIndices[indexOffset+p] = vi;//convexPtr->m_faces[i].m_indices[p]; } } convex.m_numVertices = vertices->size(); int vertexOffset = m_data->m_convexVertices.size(); convex.m_vertexOffset =vertexOffset; m_data->m_convexVertices.resize(vertexOffset+convex.m_numVertices); for (int i=0;i<vertices->size();i++) { m_data->m_convexVertices[vertexOffset+i] = vertices->at(i)*scaling; } (*m_data->m_convexData)[m_data->m_numAcceleratedShapes] = 0; return m_data->m_numAcceleratedShapes++; }
int b3GpuNarrowPhase::registerCompoundShape(b3AlignedObjectArray<b3GpuChildShape>* childShapes) { int collidableIndex = allocateCollidable(); if (collidableIndex<0) return collidableIndex; b3Collidable& col = getCollidableCpu(collidableIndex); col.m_shapeType = SHAPE_COMPOUND_OF_CONVEX_HULLS; col.m_shapeIndex = m_data->m_cpuChildShapes.size(); { b3Assert(col.m_shapeIndex+childShapes->size()<m_data->m_config.m_maxCompoundChildShapes); for (int i=0;i<childShapes->size();i++) { m_data->m_cpuChildShapes.push_back(childShapes->at(i)); } } col.m_numChildShapes = childShapes->size(); b3SapAabb aabbWS; b3Vector3 myAabbMin(1e30f,1e30f,1e30f); b3Vector3 myAabbMax(-1e30f,-1e30f,-1e30f); //compute local AABB of the compound of all children for (int i=0;i<childShapes->size();i++) { int childColIndex = childShapes->at(i).m_shapeIndex; b3Collidable& childCol = getCollidableCpu(childColIndex); b3SapAabb aabbLoc =m_data->m_localShapeAABBCPU->at(childColIndex); b3Vector3 childLocalAabbMin(aabbLoc.m_min[0],aabbLoc.m_min[1],aabbLoc.m_min[2]); b3Vector3 childLocalAabbMax(aabbLoc.m_max[0],aabbLoc.m_max[1],aabbLoc.m_max[2]); b3Vector3 aMin,aMax; b3Scalar margin(0.f); b3Transform childTr; childTr.setIdentity(); childTr.setOrigin(b3Vector3(childShapes->at(i).m_childPosition[0], childShapes->at(i).m_childPosition[1], childShapes->at(i).m_childPosition[2])); childTr.setRotation(b3Quaternion(childShapes->at(i).m_childOrientation[0], childShapes->at(i).m_childOrientation[1], childShapes->at(i).m_childOrientation[2], childShapes->at(i).m_childOrientation[3])); b3TransformAabb(childLocalAabbMin,childLocalAabbMax,margin,childTr,aMin,aMax); myAabbMin.setMin(aMin); myAabbMax.setMax(aMax); } aabbWS.m_min[0] = myAabbMin[0];//s_convexHeightField->m_aabb.m_min.x; aabbWS.m_min[1]= myAabbMin[1];//s_convexHeightField->m_aabb.m_min.y; aabbWS.m_min[2]= myAabbMin[2];//s_convexHeightField->m_aabb.m_min.z; aabbWS.m_minIndices[3] = 0; aabbWS.m_max[0] = myAabbMax[0];//s_convexHeightField->m_aabb.m_max.x; aabbWS.m_max[1]= myAabbMax[1];//s_convexHeightField->m_aabb.m_max.y; aabbWS.m_max[2]= myAabbMax[2];//s_convexHeightField->m_aabb.m_max.z; aabbWS.m_signedMaxIndices[3] = 0; m_data->m_localShapeAABBCPU->push_back(aabbWS); // m_data->m_localShapeAABBGPU->push_back(aabbWS); clFinish(m_queue); return collidableIndex; }