void TinyRendererSetup::initPhysics() { //request a visual bitma/texture we can render to m_app->setUpAxis(2); CommonRenderInterface* render = m_app->m_renderer; m_internalData->m_textureHandle = render->registerTexture(m_internalData->m_rgbColorBuffer.buffer(),m_internalData->m_width,m_internalData->m_height); { ComboBoxParams comboParams; comboParams.m_userPointer = this; comboParams.m_numItems=sizeof(items)/sizeof(char*); comboParams.m_startItem = 1; comboParams.m_items=items; comboParams.m_callback =TinyRendererComboCallback; m_guiHelper->getParameterInterface()->registerComboBox( comboParams); } { ComboBoxParams comboParams; comboParams.m_userPointer = this; comboParams.m_numItems=sizeof(itemsanimate)/sizeof(char*); comboParams.m_startItem = 0; comboParams.m_items=itemsanimate; comboParams.m_callback =TinyRendererComboCallbackAnimate; m_guiHelper->getParameterInterface()->registerComboBox( comboParams); } }
virtual bool mouseMoveCallback(float x,float y) { if (m_replay) return false; CommonRenderInterface* renderer = m_multiThreadedHelper->m_childGuiHelper->getRenderInterface();// m_guiHelper->getRenderInterface(); if (!renderer) { return false; } btVector3 rayTo = getRayTo(int(x), int(y)); btVector3 rayFrom; renderer->getActiveCamera()->getCameraPosition(rayFrom); MyMouseCommand cmd; cmd.m_rayFrom = rayFrom; cmd.m_rayTo = rayTo; cmd.m_type = MyMouseMove; m_args[0].m_cs->lock(); m_args[0].m_mouseCommands.push_back(cmd); m_args[0].m_cs->unlock(); return false; };
virtual bool mouseButtonCallback(int button, int state, float x, float y) { if (m_replay) return false; CommonRenderInterface* renderer = m_guiHelper->getRenderInterface(); if (!renderer) { return false; } CommonWindowInterface* window = m_guiHelper->getAppInterface()->m_window; if (state==1) { if(button==0 && (!window->isModifierKeyPressed(B3G_ALT) && !window->isModifierKeyPressed(B3G_CONTROL) )) { btVector3 camPos; renderer->getActiveCamera()->getCameraPosition(camPos); btVector3 rayFrom = camPos; btVector3 rayTo = getRayTo(int(x),int(y)); MyMouseCommand cmd; cmd.m_rayFrom = rayFrom; cmd.m_rayTo = rayTo; cmd.m_type = MyMouseButtonDown; m_args[0].m_cs->lock(); m_args[0].m_mouseCommands.push_back(cmd); m_args[0].m_cs->unlock(); } } else { if (button==0) { //m_physicsServer.removePickingConstraint(); MyMouseCommand cmd; cmd.m_rayFrom.setValue(0,0,0); cmd.m_rayTo.setValue(0, 0, 0); cmd.m_type = MyMouseButtonUp; m_args[0].m_cs->lock(); m_args[0].m_mouseCommands.push_back(cmd); m_args[0].m_cs->unlock(); //remove p2p } } //printf("button=%d, state=%d\n",button,state); return false; }
SW_And_OpenGLGuiHelper(CommonGraphicsApp* glApp, bool useOpenGL2, int swWidth, int swHeight, GLPrimitiveRenderer* primRenderer) : OpenGLGuiHelper(glApp, useOpenGL2), m_swWidth(swWidth), m_swHeight(swHeight), m_rgbColorBuffer(swWidth, swHeight, TGAImage::RGB), m_primRenderer(primRenderer) { m_depthBuffer.resize(swWidth * swHeight); CommonRenderInterface* render = getRenderInterface(); m_image = new unsigned char[m_swWidth * m_swHeight * 4]; m_textureHandle = render->registerTexture(m_image, m_swWidth, m_swHeight); }
virtual bool mouseMoveCallback(float x,float y) { CommonRenderInterface* renderer = m_guiHelper->getRenderInterface(); if (!renderer) { btAssert(0); return false; } btVector3 rayTo = getRayTo(int(x), int(y)); btVector3 rayFrom; renderer->getActiveCamera()->getCameraPosition(rayFrom); m_physicsServer.movePickedBody(rayFrom,rayTo); return false; };
virtual bool mouseButtonCallback(int button, int state, float x, float y) { if (m_replay) return false; CommonRenderInterface* renderer = m_guiHelper->getRenderInterface(); if (!renderer) { btAssert(0); return false; } CommonWindowInterface* window = m_guiHelper->getAppInterface()->m_window; if (state==1) { if(button==0 && (!window->isModifierKeyPressed(B3G_ALT) && !window->isModifierKeyPressed(B3G_CONTROL) )) { btVector3 camPos; renderer->getActiveCamera()->getCameraPosition(camPos); btVector3 rayFrom = camPos; btVector3 rayTo = getRayTo(int(x),int(y)); m_physicsServer.pickBody(rayFrom, rayTo); } } else { if (button==0) { m_physicsServer.removePickingConstraint(); //remove p2p } } //printf("button=%d, state=%d\n",button,state); return false; }
void ImplicitClothExample::physicsDebugDraw(int debugFlags) { CommonRenderInterface* renderer = m_guiHelper->getRenderInterface(); b3AlignedObjectArray<unsigned int> indices; for (int i=0; i<m_cloth->springs.count; i++) { indices.push_back(m_cloth->springs[i].a); indices.push_back(m_cloth->springs[i].b); } float lineColor[4]= {0.4,0.4,1.0,1}; renderer->drawLines(&m_cloth->X[0].x,lineColor,total_points,sizeof(float3),&indices[0],indices.size(),1); float pointColor[4]= {1,0.4,0.4,1}; // renderer->drawPoints(&m_cloth->X[0].x,pointColor,total_points,sizeof(float3),3); }
void TinyRendererSetup::stepSimulation(float deltaTime) { m_internalData->updateTransforms(); if (!m_useSoftware) { for (int i=0;i<m_internalData->m_transforms.size();i++) { m_guiHelper->getRenderInterface()->writeSingleInstanceTransformToCPU(m_internalData->m_transforms[i].getOrigin(),m_internalData->m_transforms[i].getRotation(),i); } m_guiHelper->getRenderInterface()->writeTransforms(); m_guiHelper->getRenderInterface()->renderScene(); } else { TGAColor clearColor; clearColor.bgra[0] = 200; clearColor.bgra[1] = 200; clearColor.bgra[2] = 200; clearColor.bgra[3] = 255; for(int y=0;y<m_internalData->m_height;++y) { for(int x=0;x<m_internalData->m_width;++x) { m_internalData->m_rgbColorBuffer.set(x,y,clearColor); m_internalData->m_depthBuffer[x+y*m_internalData->m_width] = -1e30f; } } ATTRIBUTE_ALIGNED16(btScalar modelMat2[16]); ATTRIBUTE_ALIGNED16(float viewMat[16]); ATTRIBUTE_ALIGNED16(float projMat[16]); CommonRenderInterface* render = this->m_app->m_renderer; render->getActiveCamera()->getCameraViewMatrix(viewMat); render->getActiveCamera()->getCameraProjectionMatrix(projMat); for (int o=0;o<this->m_internalData->m_renderObjects.size();o++) { const btTransform& tr = m_internalData->m_transforms[o]; tr.getOpenGLMatrix(modelMat2); for (int i=0;i<4;i++) { for (int j=0;j<4;j++) { m_internalData->m_renderObjects[o]->m_modelMatrix[i][j] = float(modelMat2[i+4*j]); m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i+4*j]; m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i+4*j]; float eye[4]; float center[4]; render->getActiveCamera()->getCameraPosition(eye); render->getActiveCamera()->getCameraTargetPosition(center); m_internalData->m_renderObjects[o]->m_eye.setValue(eye[0],eye[1],eye[2]); m_internalData->m_renderObjects[o]->m_center.setValue(center[0],center[1],center[2]); } } TinyRenderer::renderObject(*m_internalData->m_renderObjects[o]); } //m_app->drawText("hello",500,500); render->activateTexture(m_internalData->m_textureHandle); render->updateTexture(m_internalData->m_textureHandle,m_internalData->m_rgbColorBuffer.buffer()); float color[4] = {1,1,1,1}; m_app->drawTexturedRect(0,0,m_app->m_window->getWidth(), m_app->m_window->getHeight(),color,0,0,1,1,true); } }
//to be implemented by the demo void Hinge2Vehicle::renderScene() { m_guiHelper->syncPhysicsToGraphics(m_dynamicsWorld); #if 0 for (int i=0;i<m_vehicle->getNumWheels();i++) { //synchronize the wheels with the (interpolated) chassis worldtransform m_vehicle->updateWheelTransform(i,true); CommonRenderInterface* renderer = m_guiHelper->getRenderInterface(); if (renderer) { btTransform tr = m_vehicle->getWheelInfo(i).m_worldTransform; btVector3 pos=tr.getOrigin(); btQuaternion orn = tr.getRotation(); renderer->writeSingleInstanceTransformToCPU(pos,orn,m_wheelInstances[i]); } } #endif m_guiHelper->render(m_dynamicsWorld); btVector3 wheelColor(1,0,0); btVector3 worldBoundsMin,worldBoundsMax; getDynamicsWorld()->getBroadphase()->getBroadphaseAabb(worldBoundsMin,worldBoundsMax); #if 0 int lineWidth=400; int xStart = m_glutScreenWidth - lineWidth; int yStart = 20; if((getDebugMode() & btIDebugDraw::DBG_NoHelpText)==0) { setOrthographicProjection(); glDisable(GL_LIGHTING); glColor3f(0, 0, 0); char buf[124]; sprintf(buf,"SHIFT+Cursor Left/Right - rotate lift"); GLDebugDrawString(xStart,20,buf); yStart+=20; sprintf(buf,"SHIFT+Cursor UP/Down - fork up/down"); yStart+=20; GLDebugDrawString(xStart,yStart,buf); if (m_useDefaultCamera) { sprintf(buf,"F5 - camera mode (free)"); } else { sprintf(buf,"F5 - camera mode (follow)"); } yStart+=20; GLDebugDrawString(xStart,yStart,buf); yStart+=20; if (m_dynamicsWorld->getConstraintSolver()->getSolverType()==BT_MLCP_SOLVER) { sprintf(buf,"F6 - solver (direct MLCP)"); } else { sprintf(buf,"F6 - solver (sequential impulse)"); } GLDebugDrawString(xStart,yStart,buf); btDiscreteDynamicsWorld* world = (btDiscreteDynamicsWorld*) m_dynamicsWorld; if (world->getLatencyMotionStateInterpolation()) { sprintf(buf,"F7 - motionstate interpolation (on)"); } else { sprintf(buf,"F7 - motionstate interpolation (off)"); } yStart+=20; GLDebugDrawString(xStart,yStart,buf); sprintf(buf,"Click window for keyboard focus"); yStart+=20; GLDebugDrawString(xStart,yStart,buf); resetPerspectiveProjection(); glEnable(GL_LIGHTING); } #endif }
btVector3 PhysicsServerExample::getRayTo(int x,int y) { CommonRenderInterface* renderer = m_guiHelper->getRenderInterface(); if (!renderer) { btAssert(0); return btVector3(0,0,0); } float top = 1.f; float bottom = -1.f; float nearPlane = 1.f; float tanFov = (top-bottom)*0.5f / nearPlane; float fov = btScalar(2.0) * btAtan(tanFov); btVector3 camPos,camTarget; renderer->getActiveCamera()->getCameraPosition(camPos); renderer->getActiveCamera()->getCameraTargetPosition(camTarget); btVector3 rayFrom = camPos; btVector3 rayForward = (camTarget-camPos); rayForward.normalize(); float farPlane = 10000.f; rayForward*= farPlane; btVector3 rightOffset; btVector3 cameraUp=btVector3(0,0,0); cameraUp[m_guiHelper->getAppInterface()->getUpAxis()]=1; btVector3 vertical = cameraUp; btVector3 hor; hor = rayForward.cross(vertical); hor.normalize(); vertical = hor.cross(rayForward); vertical.normalize(); float tanfov = tanf(0.5f*fov); hor *= 2.f * farPlane * tanfov; vertical *= 2.f * farPlane * tanfov; btScalar aspect; float width = float(renderer->getScreenWidth()); float height = float (renderer->getScreenHeight()); aspect = width / height; hor*=aspect; btVector3 rayToCenter = rayFrom + rayForward; btVector3 dHor = hor * 1.f/width; btVector3 dVert = vertical * 1.f/height; btVector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical; rayTo += btScalar(x) * dHor; rayTo -= btScalar(y) * dVert; return rayTo; }
virtual void render(const btDiscreteDynamicsWorld* rbWorld) { OpenGLGuiHelper::render(rbWorld); //clear the color buffer TGAColor clearColor; clearColor.bgra[0] = 255; clearColor.bgra[1] = 255; clearColor.bgra[2] = 255; clearColor.bgra[3] = 255; clearBuffers(clearColor); ATTRIBUTE_ALIGNED16(btScalar modelMat[16]); ATTRIBUTE_ALIGNED16(float viewMat[16]); ATTRIBUTE_ALIGNED16(float projMat[16]); CommonRenderInterface* render = getRenderInterface(); render->getActiveCamera()->getCameraProjectionMatrix(projMat); render->getActiveCamera()->getCameraViewMatrix(viewMat); btVector3 lightDirWorld(-5, 200, -40); switch (1) //app->getUpAxis()) { case 1: lightDirWorld = btVector3(-50.f, 100, 30); break; case 2: lightDirWorld = btVector3(-50.f, 30, 100); break; default: { } }; lightDirWorld.normalize(); for (int i = 0; i < rbWorld->getNumCollisionObjects(); i++) { btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i]; int colObjIndex = colObj->getUserIndex(); int shapeIndex = colObj->getCollisionShape()->getUserIndex(); if (colObjIndex >= 0 && shapeIndex >= 0) { TinyRenderObjectData* renderObj = 0; int* cptr = m_swInstances[colObjIndex]; if (cptr) { int c = *cptr; TinyRenderObjectData** sptr = m_swRenderObjects[c]; if (sptr) { renderObj = *sptr; //sync the object transform const btTransform& tr = colObj->getWorldTransform(); tr.getOpenGLMatrix(modelMat); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { renderObj->m_projectionMatrix[i][j] = projMat[i + 4 * j]; renderObj->m_modelMatrix[i][j] = modelMat[i + 4 * j]; renderObj->m_viewMatrix[i][j] = viewMat[i + 4 * j]; } } renderObj->m_localScaling = colObj->getCollisionShape()->getLocalScaling(); renderObj->m_lightDirWorld = lightDirWorld; renderObj->m_lightAmbientCoeff = 0.6; renderObj->m_lightDiffuseCoeff = 0.35; renderObj->m_lightSpecularCoeff = 0.05; TinyRenderer::renderObject(*renderObj); } } } } for (int y = 0; y < m_swHeight; ++y) { unsigned char* pi = m_image + (y)*m_swWidth * 3; for (int x = 0; x < m_swWidth; ++x) { const TGAColor& color = getFrameBuffer().get(x, y); pi[0] = color.bgra[2]; pi[1] = color.bgra[1]; pi[2] = color.bgra[0]; pi += 3; } } render->activateTexture(m_textureHandle); render->updateTexture(m_textureHandle, m_image); static int counter = 0; counter++; if ((counter & 7) == 0) { char filename[1024]; sprintf(filename, "framebuf%d.tga", counter); getFrameBuffer().write_tga_file(filename, true); } float color[4] = {1, 1, 1, 1}; m_primRenderer->drawTexturedRect(0, 0, m_swWidth, m_swHeight, color, 0, 0, 1, 1, true); }