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; }
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 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); } }
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); }