int main(int argc, char* argv[]) { SimpleOpenGL3App* app = new SimpleOpenGL3App("Standalone Example (Software Renderer, TinyRenderer)", 1024, 768, true); int textureWidth = 640; int textureHeight = 480; SW_And_OpenGLGuiHelper gui(app, false, textureWidth, textureHeight, app->m_primRenderer); CommonExampleOptions options(&gui); CommonExampleInterface* example = StandaloneExampleCreateFunc(options); example->initPhysics(); example->resetCamera(); do { app->m_instancingRenderer->init(); app->m_instancingRenderer->updateCamera(app->getUpAxis()); example->stepSimulation(1. / 60.); example->renderScene(); DrawGridData dg; dg.upAxis = app->getUpAxis(); app->drawGrid(dg); app->swapBuffer(); } while (!app->m_window->requestedExit()); example->exitPhysics(); delete example; delete app; return 0; }
int main(int argc, char* argv[]) { SimpleOpenGL3App* app = new SimpleOpenGL3App("Bullet Standalone Example",1024,768,true); OpenGLGuiHelper gui(app,false); CommonExampleOptions options(&gui); CommonExampleInterface* example = StandaloneExampleCreateFunc(options); example->initPhysics(); do { app->m_instancingRenderer->init(); app->m_instancingRenderer->updateCamera(); example->stepSimulation(1./60.); example->renderScene(); app->drawGrid(); app->swapBuffer(); } while (!app->m_window->requestedExit()); example->exitPhysics(); delete example; delete app; return 0; }
int main(int argc, char* argv[]) { float dt = 1./120.f; int width = 1024; int height=768; SimpleOpenGL3App* app = new SimpleOpenGL3App("AllBullet2Demos",width,height); app->m_instancingRenderer->setCameraDistance(13); app->m_instancingRenderer->setCameraPitch(0); app->m_instancingRenderer->setCameraTargetPosition(b3MakeVector3(0,0,0)); app->m_window->setMouseMoveCallback(MyMouseMoveCallback); app->m_window->setMouseButtonCallback(MyMouseButtonCallback); GLint err = glGetError(); assert(err==GL_NO_ERROR); sth_stash* fontstash=app->getFontStash(); gui = new GwenUserInterface; gui->init(width,height,fontstash,app->m_window->getRetinaScale()); const char* names[] = {"test1", "test2","test3"}; gui->registerComboBox(13,3,&names[0],1); const char* names2[] = {"comboF", "comboG","comboH"}; gui->registerComboBox(2,3,&names2[0],1); gui->setComboBoxCallback(MyComboBoxCallback); do { GLint err = glGetError(); assert(err==GL_NO_ERROR); app->m_instancingRenderer->init(); app->m_instancingRenderer->updateCamera(); app->drawGrid(); char bla[1024]; static int frameCount = 0; frameCount++; sprintf(bla,"Simple test frame %d", frameCount); app->drawText(bla,10,10); static int toggle = 1; if (1) { gui->draw(app->m_instancingRenderer->getScreenWidth(),app->m_instancingRenderer->getScreenHeight()); } toggle=1-toggle; app->swapBuffer(); } while (!app->m_window->requestedExit()); delete gui; delete app; return 0; }
int main(int argc, char* argv[]) { float dt = 1./120.f; #ifdef BT_DEBUG char* name = "Bullet 2 CPU FeatherstoneMultiBodyDemo (Debug build=SLOW)"; #else char* name = "Bullet 2 CPU FeatherstoneMultiBodyDemo"; #endif SimpleOpenGL3App* app = new SimpleOpenGL3App(name,1024,768); app->m_instancingRenderer->setCameraDistance(40); app->m_instancingRenderer->setCameraPitch(0); app->m_instancingRenderer->setCameraTargetPosition(b3MakeVector3(0,0,0)); app->m_window->setMouseMoveCallback(MyMouseMoveCallback); app->m_window->setMouseButtonCallback(MyMouseButtonCallback); BasicDemo* demo = new BasicDemo(app); demo->initPhysics(); sDemo = demo; GLint err = glGetError(); assert(err==GL_NO_ERROR); do { GLint err = glGetError(); assert(err==GL_NO_ERROR); app->m_instancingRenderer->init(); app->m_instancingRenderer->updateCamera(); demo->stepSimulation(); demo->drawObjects(); app->drawGrid(10,0.01); char bla[1024]; static int frameCount = 0; frameCount++; sprintf(bla,"Simulation frame %d", frameCount); app->drawText(bla,10,10); app->swapBuffer(); } while (!app->m_window->requestedExit()); demo->exitPhysics(); delete demo; delete app; return 0; }
int main(int argc, char* argv[]) { SimpleOpenGL3App* app = new SimpleOpenGL3App("Bullet Standalone Example",1024,768,true); prevMouseButtonCallback = app->m_window->getMouseButtonCallback(); prevMouseMoveCallback = app->m_window->getMouseMoveCallback(); app->m_window->setMouseButtonCallback((b3MouseButtonCallback)OnMouseDown); app->m_window->setMouseMoveCallback((b3MouseMoveCallback)OnMouseMove); OpenGLGuiHelper gui(app,false); CommonExampleOptions options(&gui); example = StandaloneExampleCreateFunc(options); example->initPhysics(); example->resetCamera(); do { app->m_instancingRenderer->init(); app->m_instancingRenderer->updateCamera(app->getUpAxis()); example->stepSimulation(1./60.); example->renderScene(); DrawGridData dg; dg.upAxis = app->getUpAxis(); app->drawGrid(dg); app->swapBuffer(); } while (!app->m_window->requestedExit()); example->exitPhysics(); delete example; delete app; return 0; }
void initPhysics() { Bullet2MultiBodyDemo::initPhysics(); //create ground int cubeShapeId = m_glApp->registerCubeShape(); float pos[]={0,0,0}; float orn[]={0,0,0,1}; { float color[]={0.3,0.3,1,1}; float halfExtents[]={50,50,50,1}; btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-50,0)); btBoxShape* groundShape = new btBoxShape(btVector3(btScalar(halfExtents[0]),btScalar(halfExtents[1]),btScalar(halfExtents[2]))); //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here: { btScalar mass(0.); //rigidbody is dynamic if and only if mass is non zero, otherwise static bool isDynamic = (mass != 0.f); btVector3 localInertia(0,0,0); if (isDynamic) groundShape->calculateLocalInertia(mass,localInertia); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia); btRigidBody* body = new btRigidBody(rbInfo); int index = m_glApp->m_instancingRenderer->registerGraphicsInstance(cubeShapeId,groundTransform.getOrigin(),groundTransform.getRotation(),color,halfExtents); body ->setUserIndex(index); //add the body to the dynamics world m_dynamicsWorld->addRigidBody(body); } } #if 0 { float halfExtents[]={1,1,1,1}; btTransform startTransform; startTransform.setIdentity(); btScalar mass = 1.f; btVector3 localInertia; btBoxShape* colShape = new btBoxShape(btVector3(halfExtents[0],halfExtents[1],halfExtents[2])); colShape ->calculateLocalInertia(mass,localInertia); for (int k=0;k<ARRAY_SIZE_Y;k++) { for (int i=0;i<ARRAY_SIZE_X;i++) { for(int j = 0;j<ARRAY_SIZE_Z;j++) { static int curColor=0; b3Vector4 color = colors[curColor]; curColor++; startTransform.setOrigin(btVector3( btScalar(2.0*i), btScalar(20+2.0*k), btScalar(2.0*j))); int index = m_glApp->m_instancingRenderer->registerGraphicsInstance(cubeShapeId,startTransform.getOrigin(),startTransform.getRotation(),color,halfExtents); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia); btRigidBody* body = new btRigidBody(rbInfo); body->setUserIndex(index); m_dynamicsWorld->addRigidBody(body); } } } } #endif btMultiBodySettings settings; settings.m_isFixedBase = false; settings.m_basePosition.setValue(0,10,0); settings.m_numLinks = 10; btMultiBody* mb = createFeatherstoneMultiBody(m_dynamicsWorld,settings); m_glApp->m_instancingRenderer->writeTransforms(); }
btMultiBody* createFeatherstoneMultiBody(class btMultiBodyDynamicsWorld* world, const btMultiBodySettings& settings) { static int curColor=0; int cubeShapeId = m_glApp->registerCubeShape(); int n_links = settings.m_numLinks; float mass = 13.5*scaling; btVector3 inertia = btVector3 (91,344,253)*scaling*scaling; btMultiBody * bod = new btMultiBody(n_links, mass, inertia, settings.m_isFixedBase, settings.m_canSleep); // bod->setHasSelfCollision(false); //btQuaternion orn(btVector3(0,0,1),-0.25*SIMD_HALF_PI);//0,0,0,1); btQuaternion orn(0,0,0,1); bod->setBasePos(settings.m_basePosition); bod->setWorldToBaseRot(orn); btVector3 vel(0,0,0); bod->setBaseVel(vel); { btVector3 joint_axis_hinge(1,0,0); btVector3 joint_axis_prismatic(0,0,1); btQuaternion parent_to_child = orn.inverse(); btVector3 joint_axis_child_prismatic = quatRotate(parent_to_child ,joint_axis_prismatic); btVector3 joint_axis_child_hinge = quatRotate(parent_to_child , joint_axis_hinge); int this_link_num = -1; int link_num_counter = 0; btVector3 pos = btVector3 (0,0,9.0500002)*scaling; btVector3 joint_axis_position = btVector3 (0,0,4.5250001)*scaling; for (int i=0;i<n_links;i++) { float initial_joint_angle=0.3; if (i>0) initial_joint_angle = -0.06f; const int child_link_num = link_num_counter++; if (settings.m_usePrismatic)// && i==(n_links-1)) { bod->setupPrismatic(child_link_num, mass, inertia, this_link_num, parent_to_child, joint_axis_child_prismatic, quatRotate(parent_to_child , pos),settings.m_disableParentCollision); } else { bod->setupRevolute(child_link_num, mass, inertia, this_link_num,parent_to_child, joint_axis_child_hinge, joint_axis_position,quatRotate(parent_to_child , (pos - joint_axis_position)),settings.m_disableParentCollision); } bod->setJointPos(child_link_num, initial_joint_angle); this_link_num = i; if (0)//!useGroundShape && i==4) { btVector3 pivotInAworld(0,20,46); btVector3 pivotInAlocal = bod->worldPosToLocal(i, pivotInAworld); btVector3 pivotInBworld = pivotInAworld; btMultiBodyPoint2Point* p2p = new btMultiBodyPoint2Point(bod,i,&btTypedConstraint::getFixedBody(),pivotInAlocal,pivotInBworld); world->addMultiBodyConstraint(p2p); } //add some constraint limit if (settings.m_usePrismatic) { // btMultiBodyConstraint* con = new btMultiBodyJointLimitConstraint(bod,n_links-1,2,3); if (settings.m_createConstraints) { btMultiBodyConstraint* con = new btMultiBodyJointLimitConstraint(bod,i,-1,1); world->addMultiBodyConstraint(con); } } else { if (settings.m_createConstraints) { if (1) { btMultiBodyJointMotor* con = new btMultiBodyJointMotor(bod,i,0,500000); world->addMultiBodyConstraint(con); } btMultiBodyConstraint* con = new btMultiBodyJointLimitConstraint(bod,i,-1,1); world->addMultiBodyConstraint(con); } } } } //add a collider for the base { btAlignedObjectArray<btQuaternion> world_to_local; world_to_local.resize(n_links+1); btAlignedObjectArray<btVector3> local_origin; local_origin.resize(n_links+1); world_to_local[0] = bod->getWorldToBaseRot(); local_origin[0] = bod->getBasePos(); //float halfExtents[3]={7.5,0.05,4.5}; float halfExtents[3]={7.5,0.45,4.5}; { float pos[4]={local_origin[0].x(),local_origin[0].y(),local_origin[0].z(),1}; float quat[4]={-world_to_local[0].x(),-world_to_local[0].y(),-world_to_local[0].z(),world_to_local[0].w()}; if (1) { btCollisionShape* box = new btBoxShape(btVector3(halfExtents[0],halfExtents[1],halfExtents[2])*scaling); btRigidBody* body = new btRigidBody(mass,0,box,inertia); btMultiBodyLinkCollider* col= new btMultiBodyLinkCollider(bod,-1); body->setCollisionShape(box); col->setCollisionShape(box); btTransform tr; tr.setIdentity(); tr.setOrigin(local_origin[0]); tr.setRotation(btQuaternion(quat[0],quat[1],quat[2],quat[3])); body->setWorldTransform(tr); col->setWorldTransform(tr); b3Vector4 color = colors[curColor++]; curColor&=3; int index = m_glApp->m_instancingRenderer->registerGraphicsInstance(cubeShapeId,tr.getOrigin(),tr.getRotation(),color,halfExtents); col->setUserIndex(index); world->addCollisionObject(col,short(btBroadphaseProxy::DefaultFilter),short(btBroadphaseProxy::AllFilter)); col->setFriction(friction); bod->setBaseCollider(col); } } for (int i=0;i<bod->getNumLinks();i++) { const int parent = bod->getParent(i); world_to_local[i+1] = bod->getParentToLocalRot(i) * world_to_local[parent+1]; local_origin[i+1] = local_origin[parent+1] + (quatRotate(world_to_local[i+1].inverse() , bod->getRVector(i))); } for (int i=0;i<bod->getNumLinks();i++) { btVector3 posr = local_origin[i+1]; float pos[4]={posr.x(),posr.y(),posr.z(),1}; float quat[4]={-world_to_local[i+1].x(),-world_to_local[i+1].y(),-world_to_local[i+1].z(),world_to_local[i+1].w()}; btCollisionShape* box = new btBoxShape(btVector3(halfExtents[0],halfExtents[1],halfExtents[2])*scaling); btMultiBodyLinkCollider* col = new btMultiBodyLinkCollider(bod,i); col->setCollisionShape(box); btTransform tr; tr.setIdentity(); tr.setOrigin(posr); tr.setRotation(btQuaternion(quat[0],quat[1],quat[2],quat[3])); col->setWorldTransform(tr); col->setFriction(friction); b3Vector4 color = colors[curColor++]; curColor&=3; int index = m_glApp->m_instancingRenderer->registerGraphicsInstance(cubeShapeId,tr.getOrigin(),tr.getRotation(),color,halfExtents); col->setUserIndex(index); world->addCollisionObject(col,short(btBroadphaseProxy::DefaultFilter),short(btBroadphaseProxy::AllFilter)); bod->getLink(i).m_collider=col; //app->drawBox(halfExtents, pos,quat); } } world->addMultiBody(bod); return bod; }
virtual void createCollisionShapeGraphicsObject(btCollisionShape* collisionShape) { //todo: support all collision shape types switch (collisionShape->getShapeType()) { case BOX_SHAPE_PROXYTYPE: { btBoxShape* box = (btBoxShape*)collisionShape; btVector3 halfExtents = box->getHalfExtentsWithMargin(); int cubeShapeId = m_glApp->registerCubeShape(halfExtents.x(), halfExtents.y(), halfExtents.z()); box->setUserIndex(cubeShapeId); break; } case TRIANGLE_MESH_SHAPE_PROXYTYPE: { break; } default: { if (collisionShape->isConvex()) { btConvexShape* convex = (btConvexShape*)collisionShape; { btShapeHull* hull = new btShapeHull(convex); hull->buildHull(0.0); { //int strideInBytes = 9*sizeof(float); //int numVertices = hull->numVertices(); //int numIndices =hull->numIndices(); btAlignedObjectArray<GraphicsVertex> gvertices; btAlignedObjectArray<int> indices; for (int t=0;t<hull->numTriangles();t++) { btVector3 triNormal; int index0 = hull->getIndexPointer()[t*3+0]; int index1 = hull->getIndexPointer()[t*3+1]; int index2 = hull->getIndexPointer()[t*3+2]; btVector3 pos0 =hull->getVertexPointer()[index0]; btVector3 pos1 =hull->getVertexPointer()[index1]; btVector3 pos2 =hull->getVertexPointer()[index2]; triNormal = (pos1-pos0).cross(pos2-pos0); triNormal.normalize(); for (int v=0;v<3;v++) { int index = hull->getIndexPointer()[t*3+v]; GraphicsVertex vtx; btVector3 pos =hull->getVertexPointer()[index]; vtx.pos[0] = pos.x(); vtx.pos[1] = pos.y(); vtx.pos[2] = pos.z(); vtx.pos[3] = 0.f; vtx.normal[0] =triNormal.x(); vtx.normal[1] =triNormal.y(); vtx.normal[2] =triNormal.z(); vtx.texcoord[0] = 0.5f; vtx.texcoord[1] = 0.5f; indices.push_back(gvertices.size()); gvertices.push_back(vtx); } } int shapeId = m_glApp->m_instancingRenderer->registerShape(&gvertices[0].pos[0],gvertices.size(),&indices[0],indices.size()); convex->setUserIndex(shapeId); } } } else { btAssert(0); } } }; }
virtual void setUpAxis(int axis) { m_glApp->setUpAxis(axis); }
int main(int argc, char* argv[]) { b3CommandLineArgs myArgs(argc,argv); SimpleOpenGL3App* app = new SimpleOpenGL3App("SimpleOpenGL3App",640,480,true); app->m_instancingRenderer->getActiveCamera()->setCameraDistance(13); app->m_instancingRenderer->getActiveCamera()->setCameraPitch(0); app->m_instancingRenderer->getActiveCamera()->setCameraTargetPosition(0,0,0); sOldKeyboardCB = app->m_window->getKeyboardCallback(); app->m_window->setKeyboardCallback(MyKeyboardCallback); sOldMouseMoveCB = app->m_window->getMouseMoveCallback(); app->m_window->setMouseMoveCallback(MyMouseMoveCallback); sOldMouseButtonCB = app->m_window->getMouseButtonCallback(); app->m_window->setMouseButtonCallback(MyMouseButtonCallback); sOldWheelCB = app->m_window->getWheelCallback(); app->m_window->setWheelCallback(MyWheelCallback); sOldResizeCB = app->m_window->getResizeCallback(); app->m_window->setResizeCallback(MyResizeCallback); int textureWidth = gWidth; int textureHeight = gHeight; TGAImage rgbColorBuffer(gWidth,gHeight,TGAImage::RGB); b3AlignedObjectArray<float> depthBuffer; depthBuffer.resize(gWidth*gHeight); TinyRenderObjectData renderData(textureWidth, textureHeight,rgbColorBuffer,depthBuffer);//, "african_head/african_head.obj");//floor.obj"); //renderData.loadModel("african_head/african_head.obj"); renderData.loadModel("floor.obj"); //renderData.createCube(1,1,1); myArgs.GetCmdLineArgument("mp4_file",gVideoFileName); if (gVideoFileName) app->dumpFramesToVideo(gVideoFileName); myArgs.GetCmdLineArgument("png_file",gPngFileName); char fileName[1024]; unsigned char* image=new unsigned char[textureWidth*textureHeight*4]; int textureHandle = app->m_renderer->registerTexture(image,textureWidth,textureHeight); int cubeIndex = app->registerCubeShape(1,1,1); b3Vector3 pos = b3MakeVector3(0,0,0); b3Quaternion orn(0,0,0,1); b3Vector3 color=b3MakeVector3(1,0,0); b3Vector3 scaling=b3MakeVector3 (1,1,1); app->m_renderer->registerGraphicsInstance(cubeIndex,pos,orn,color,scaling); app->m_renderer->writeTransforms(); do { static int frameCount = 0; frameCount++; if (gPngFileName) { printf("gPngFileName=%s\n",gPngFileName); sprintf(fileName,"%s%d.png",gPngFileName,frameCount++); app->dumpNextFrameToPng(fileName); } app->m_instancingRenderer->init(); app->m_instancingRenderer->updateCamera(); ///clear the color and z (depth) buffer for(int y=0;y<textureHeight;++y) { unsigned char* pi=image+(y)*textureWidth*3; for(int x=0;x<textureWidth;++x) { TGAColor color; color.bgra[0] = 255; color.bgra[1] = 255; color.bgra[2] = 255; color.bgra[3] = 255; renderData.m_rgbColorBuffer.set(x,y,color); renderData.m_depthBuffer[x+y*textureWidth] = -1e30f; } } float projMat[16]; app->m_instancingRenderer->getActiveCamera()->getCameraProjectionMatrix(projMat); float viewMat[16]; app->m_instancingRenderer->getActiveCamera()->getCameraViewMatrix(viewMat); B3_ATTRIBUTE_ALIGNED16(float modelMat[16]); //sync the object transform b3Transform tr; tr.setIdentity(); static float posUp = 0.f; // posUp += 0.001; b3Vector3 org = b3MakeVector3(0,posUp,0); tr.setOrigin(org); tr.getOpenGLMatrix(modelMat); for (int i=0;i<4;i++) { for (int j=0;j<4;j++) { renderData.m_viewMatrix[i][j] = viewMat[i+4*j]; renderData.m_modelMatrix[i][j] = modelMat[i+4*j]; } } //render the object TinyRenderer::renderObject(renderData); #if 1 //update the texels of the texture using a simple pattern, animated using frame index for(int y=0;y<textureHeight;++y) { unsigned char* pi=image+(y)*textureWidth*3; for(int x=0;x<textureWidth;++x) { TGAColor color = renderData.m_rgbColorBuffer.get(x,y); pi[0] = color.bgra[2]; pi[1] = color.bgra[1]; pi[2] = color.bgra[0]; pi[3] = 255; pi+=3; } } #else //update the texels of the texture using a simple pattern, animated using frame index for(int y=0;y<textureHeight;++y) { const int t=(y+frameCount)>>4; unsigned char* pi=image+y*textureWidth*3; for(int x=0;x<textureWidth;++x) { TGAColor color = renderData.m_rgbColorBuffer.get(x,y); const int s=x>>4; const unsigned char b=180; unsigned char c=b+((s+(t&1))&1)*(255-b); pi[0]=pi[1]=pi[2]=pi[3]=c; pi+=3; } } #endif app->m_renderer->activateTexture(textureHandle); app->m_renderer->updateTexture(textureHandle,image); float color[4] = {1,1,1,1}; app->m_primRenderer->drawTexturedRect(0,0,gWidth/3,gHeight/3,color,0,0,1,1,true); app->m_renderer->renderScene(); app->drawGrid(); char bla[1024]; sprintf(bla,"Simple test frame %d", frameCount); app->drawText(bla,10,10); app->swapBuffer(); } while (!app->m_window->requestedExit()); delete app; return 0; }