void GL2CL(CLPhysicsDemo& demo, GLInstancingRenderer& render) { BT_PROFILE("simulationLoop"); int VBOsize = demo.m_maxShapeBufferCapacityInBytes+demo.m_numPhysicsInstances*(4+4+4+3)*sizeof(float); cl_int ciErrNum = CL_SUCCESS; if(useInterop) { #ifndef __APPLE__ clBuffer = g_interopBuffer->getCLBUffer(); BT_PROFILE("clEnqueueAcquireGLObjects"); { BT_PROFILE("clEnqueueAcquireGLObjects"); ciErrNum = clEnqueueAcquireGLObjects(g_cqCommandQue, 1, &clBuffer, 0, 0, NULL); clFinish(g_cqCommandQue); } #else assert(0); #endif } else { glBindBuffer(GL_ARRAY_BUFFER, render.getInternalData()->m_vbo); glFlush(); BT_PROFILE("glMapBuffer and clEnqueueWriteBuffer"); blocking= CL_TRUE; hostPtr= (char*)glMapBuffer( GL_ARRAY_BUFFER,GL_READ_WRITE);//GL_WRITE_ONLY if (!clBuffer) { int maxVBOsize = demo.m_maxShapeBufferCapacityInBytes+MAX_CONVEX_BODIES_CL*(4+4+4+3)*sizeof(float); clBuffer = clCreateBuffer(g_cxMainContext, CL_MEM_READ_WRITE,maxVBOsize, 0, &ciErrNum); clFinish(g_cqCommandQue); oclCHECKERROR(ciErrNum, CL_SUCCESS); ciErrNum = clEnqueueWriteBuffer ( g_cqCommandQue, clBuffer, blocking, 0, VBOsize, hostPtr,0,0,0 ); clFinish(g_cqCommandQue); } } gFpIO.m_clObjectsBuffer = clBuffer; gFpIO.m_positionOffset = demo.m_maxShapeBufferCapacityInBytes/4; }
void graphics_from_physics(GLInstancingRenderer& renderer, bool syncTransformsOnly) { int cubeShapeIndex = -1; int strideInBytes = sizeof(float)*9; if (!syncTransformsOnly) { int numVertices = sizeof(cube_vertices)/strideInBytes; int numIndices = sizeof(cube_indices)/sizeof(int); cubeShapeIndex = renderer.registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); } int curGraphicsIndex=0; for(int i=0;i<physics_get_num_rigidbodies();i++) { const PfxRigidState &state = physics_get_state(i); const PfxCollidable &coll = physics_get_collidable(i); const PfxRigidBody& body = physics_get_body(i); float color[4]={0,0,0,1}; if (body.getMass()==0.f) { color[0]=1.f; } else { color[1]=1.f; } PfxTransform3 rbT(state.getOrientation(), state.getPosition()); PfxShapeIterator itrShape(coll); for(int j=0;j<coll.getNumShapes();j++,++itrShape) { const PfxShape &shape = *itrShape; PfxTransform3 offsetT = shape.getOffsetTransform(); PfxTransform3 worldT = rbT * offsetT; PfxQuat ornWorld( worldT.getUpper3x3()); switch(shape.getType()) { case kPfxShapeSphere: printf("render sphere\n"); /*render_sphere( worldT, PfxVector3(1,1,1), PfxFloatInVec(shape.getSphere().m_radius)); */ break; case kPfxShapeBox: { // printf("render box\n"); float cubeScaling[4] = {shape.getBox().m_half.getX(),shape.getBox().m_half.getY(),shape.getBox().m_half.getZ(),1}; float rotOrn[4] = {ornWorld.getX(),ornWorld.getY(),ornWorld.getZ(),ornWorld.getW()}; float position[4]={worldT.getTranslation().getX(),worldT.getTranslation().getY(),worldT.getTranslation().getZ(),0}; if (!syncTransformsOnly) { renderer.registerGraphicsInstance(cubeShapeIndex,position,rotOrn,color,cubeScaling); } else { renderer.writeSingleInstanceTransformToCPU(position,rotOrn,curGraphicsIndex); } curGraphicsIndex++; /* render_box( worldT, PfxVector3(1,1,1), shape.getBox().m_half); */ break; } case kPfxShapeCapsule: printf("render_capsule\n"); /*render_capsule( worldT, PfxVector3(1,1,1), PfxFloatInVec(shape.getCapsule().m_radius), PfxFloatInVec(shape.getCapsule().m_halfLen)); */ break; case kPfxShapeCylinder: printf("render_cylinder\n"); /* render_cylinder( worldT, PfxVector3(1,1,1), PfxFloatInVec(shape.getCylinder().m_radius), PfxFloatInVec(shape.getCylinder().m_halfLen)); */ break; case kPfxShapeConvexMesh: printf("render_mesh\n"); /* render_mesh( worldT, PfxVector3(1,1,1), convexMeshId); */ break; case kPfxShapeLargeTriMesh: printf("render_mesh\n"); /* render_mesh( worldT, PfxVector3(1,1,1), landscapeMeshId); */ break; default: break; } } } }
void createSceneProgrammatically(GLInstancingRenderer& renderer) { int strideInBytes = sizeof(float)*9; bool noHeightField = false; int barrelShapeIndex = -1; int cubeShapeIndex = -1; int tetraShapeIndex = -1; float position[4]={0,0,0,0}; btQuaternion born(btVector3(1,0,0),SIMD_PI*0.25*0.5); float orn[4] = {0,0,0,1}; // float rotOrn[4] = {born.getX(),born.getY(),born.getZ(),born.getW()};// float rotOrn[4] ={0,0,0,1}; float color[4] = {1,1,1,1}; int index=0; float cubeScaling[4] = {1,1,1,1}; { int numVertices = sizeof(cube_vertices)/strideInBytes; int numIndices = sizeof(cube_indices)/sizeof(int); cubeShapeIndex = renderer.registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); } if (1) for (int i=0;i<NUM_OBJECTS_X;i++) { for (int j=0;j<NUM_OBJECTS_Y;j++) { int k=0; for (;k<NUM_OBJECTS_Z;k++) { float mass = 1.f;//j? 1.f : 0.f; position[0]=(i*X_GAP-NUM_OBJECTS_X/2)+(j&1); position[1]=1+(j*Y_GAP);//-NUM_OBJECTS_Y/2); position[2]=(k*Z_GAP-NUM_OBJECTS_Z/2)+(j&1); position[3] = 0.f; renderer.registerGraphicsInstance(cubeShapeIndex,position,rotOrn,color,cubeScaling); index++; } } } { { int numVertices = sizeof(tetra_vertices)/strideInBytes; int numIndices = sizeof(tetra_indices)/sizeof(int); tetraShapeIndex = renderer.registerShape(&tetra_vertices[0],numVertices,tetra_indices,numIndices); } { float groundScaling[4] = {2.5,2,2.5,1}; for (int i=0;i<50;i++) for (int j=0;j<50;j++) if (1) { void* ptr = (void*) index; float posnew[4]; posnew[0] = i*5.01-120; posnew[1] = 0; posnew[2] = j*5.01-120; posnew[3] = 1.f; color[0] = 1.f; color[1] = 0.f; color[2] = 0.f; renderer.registerGraphicsInstance(tetraShapeIndex,posnew,orn,color,groundScaling); } } } }
//very incomplete conversion from physics to graphics void graphics_from_physics(GLInstancingRenderer& renderer, bool syncTransformsOnly, const btDiscreteDynamicsWorld* world) { int cubeShapeIndex = -1; int strideInBytes = sizeof(float)*9; if (!syncTransformsOnly) { int numVertices = sizeof(cube_vertices)/strideInBytes; int numIndices = sizeof(cube_indices)/sizeof(int); cubeShapeIndex = renderer.registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); } int numColObj = world->getNumCollisionObjects(); int curGraphicsIndex = 0; for (int i=0;i<numColObj;i++) { btCollisionObject* colObj = world->getCollisionObjectArray()[i]; btVector3 pos = colObj->getWorldTransform().getOrigin(); btQuaternion orn = colObj->getWorldTransform().getRotation(); float position[4] = {pos.getX(),pos.getY(),pos.getZ(),0.f}; float orientation[4] = {orn.getX(),orn.getY(),orn.getZ(),orn.getW()}; float color[4] = {0,0,0,1}; btVector3 localScaling = colObj->getCollisionShape()->getLocalScaling(); if (colObj->isStaticOrKinematicObject()) { color[0]=1.f; }else { color[1]=1.f; } switch (colObj->getCollisionShape()->getShapeType()) { case BOX_SHAPE_PROXYTYPE: { btBoxShape* box = (btBoxShape*)colObj->getCollisionShape(); btVector3 halfExtents = box->getHalfExtentsWithMargin(); float cubeScaling[4] = {halfExtents.getX(),halfExtents.getY(), halfExtents.getZ(),1}; if (!syncTransformsOnly) { renderer.registerGraphicsInstance(cubeShapeIndex,position,orientation,color,cubeScaling); } else { renderer.writeSingleInstanceTransformToCPU(position,orientation,curGraphicsIndex); } curGraphicsIndex++; } break; default: break; } //convert it now! } }
int main(int argc, char* argv[]) { CommandLineArgs args(argc,argv); if (args.CheckCmdLineFlag("help")) { Usage(); return 0; } args.GetCmdLineArgument("enable_interop", useInterop); printf("useInterop=%d\n",useInterop); args.GetCmdLineArgument("enable_convexheightfield", useConvexHeightfield); printf("enable_convexheightfield=%d\n",useConvexHeightfield); args.GetCmdLineArgument("enable_gpusap", useSapGpuBroadphase); printf("enable_gpusap=%d\n",useSapGpuBroadphase); args.GetCmdLineArgument("pause_simulation", pauseSimulation); printf("pause_simulation=%d\n",pauseSimulation); args.GetCmdLineArgument("x_dim", NUM_OBJECTS_X); args.GetCmdLineArgument("y_dim", NUM_OBJECTS_Y); args.GetCmdLineArgument("z_dim", NUM_OBJECTS_Z); args.GetCmdLineArgument("x_gap", X_GAP); args.GetCmdLineArgument("y_gap", Y_GAP); args.GetCmdLineArgument("z_gap", Z_GAP); printf("x_dim=%d, y_dim=%d, z_dim=%d\n",NUM_OBJECTS_X,NUM_OBJECTS_Y,NUM_OBJECTS_Z); printf("x_gap=%f, y_gap=%f, z_gap=%f\n",X_GAP,Y_GAP,Z_GAP); args.GetCmdLineArgument("enable_static", keepStaticObjects); printf("enable_static=%d\n",keepStaticObjects); char* tmpfile = 0; args.GetCmdLineArgument("load_bulletfile", tmpfile ); if (tmpfile) fileName = tmpfile; printf("load_bulletfile=%s\n",fileName); printf("\n"); #ifdef _WIN32 Win32OpenGLWindow* window = new Win32OpenGLWindow(); #else MacOpenGLWindow* window = new MacOpenGLWindow(); #endif window->init(1024,768); #ifdef _WIN32 GLenum err = glewInit(); #endif window->startRendering(); window->endRendering(); GLInstancingRenderer render; CLPhysicsDemo demo(window); demo.init(-1,-1,useInterop); render.InitShaders(); if (useInterop) demo.setupInterop(); createScene(render, demo, useConvexHeightfield,fileName); printf("numPhysicsInstances= %d\n", demo.m_numPhysicsInstances); printf("numDynamicPhysicsInstances= %d\n", demo.m_numDynamicPhysicsInstances); render.writeTransforms(); window->startRendering(); render.RenderScene(); window->endRendering(); while (!window->requestedExit()) { CProfileManager::Reset(); if (shootObject) { shootObject = false; btVector3 linVel;// = (m_cameraPosition-m_cameraTargetPosition).normalize()*-100; int x,y; window->getMouseCoordinates(x,y); render.getMouseDirection(&linVel[0],x,y); linVel.normalize(); linVel*=100; // btVector3 startPos; demo.setObjectLinearVelocity(&linVel[0],0); float orn[4] = {0,0,0,1}; float pos[4] = {m_cameraPosition[0],m_cameraPosition[1],m_cameraPosition[2],1}; // demo.setObjectTransform(pos,orn,0); render.writeSingleTransform(pos,orn,0); // createScene(render, demo); // printf("numPhysicsInstances= %d\n", demo.m_numPhysicsInstances); // printf("numDynamicPhysicsInstances= %d\n", demo.m_numDynamicPhysicsInstances); // render.writeTransforms(); } if (!pauseSimulation ) demo.stepSimulation(); window->startRendering(); render.RenderScene(); window->endRendering(); { BT_PROFILE("glFinish"); glFinish(); } CProfileManager::Increment_Frame_Counter(); if (printStats && !pauseSimulation) { static int count = 3; count--; if (count<0) { count = 100; CProfileManager::dumpAll(); printf("total broadphase pairs= %d\n", numPairsTotal); printf("numPairsOut (culled) = %d\n", numPairsOut); //printStats = false; } else { // printf("."); } } } demo.cleanup(); render.CleanupShaders(); window->exit(); delete window; return 0; }