void CameraBlurListener::notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat) { if (pass_id == 999) { if(mApp->pGame->pause == false) { //acquire the texture flipping attribute in the first frame if(compositorinstance) { mRequiresTextureFlipping = compositorinstance->getRenderTarget("previousscene")->requiresTextureFlipping(); compositorinstance=NULL; } // this is the camera you're using #ifndef ROAD_EDITOR Ogre::Camera *cam = mApp->mSplitMgr->mCameras.front(); #else Ogre::Camera *cam = mApp->mCamera; #endif // get the pass Ogre::Pass *pass = mat->getBestTechnique()->getPass(0); Ogre::GpuProgramParametersSharedPtr params = pass->getFragmentProgramParameters(); const Ogre::RenderTarget::FrameStats& stats = mApp->getWindow()->getStatistics(); float m_lastFPS =stats.lastFPS; Ogre::Matrix4 projectionMatrix = cam->getProjectionMatrix(); if (mRequiresTextureFlipping) { // Because we're not using setProjectionMatrix, this needs to be done here // Invert transformed y projectionMatrix[1][0] = -projectionMatrix[1][0]; projectionMatrix[1][1] = -projectionMatrix[1][1]; projectionMatrix[1][2] = -projectionMatrix[1][2]; projectionMatrix[1][3] = -projectionMatrix[1][3]; } Ogre::Matrix4 iVP = (projectionMatrix * cam->getViewMatrix()).inverse(); if (params->_findNamedConstantDefinition("EPF_ViewProjectionInverseMatrix")) params->setNamedConstant("EPF_ViewProjectionInverseMatrix", iVP); if (params->_findNamedConstantDefinition("EPF_PreviousViewProjectionMatrix")) params->setNamedConstant("EPF_PreviousViewProjectionMatrix", prevviewproj); if (params->_findNamedConstantDefinition("intensity")) params->setNamedConstant("intensity", mApp->pSet->motionblurintensity); float interpolationFactor = m_lastFPS * 0.03f ; //* m_timeScale m_timeScale is a multiplier to control motion blur interactively Ogre::Quaternion current_orientation = cam->getDerivedOrientation(); Ogre::Vector3 current_position = cam->getDerivedPosition(); Ogre::Quaternion estimatedOrientation = Ogre::Quaternion::Slerp(interpolationFactor, current_orientation, (m_pPreviousOrientation)); Ogre::Vector3 estimatedPosition = (1-interpolationFactor) * current_position + interpolationFactor * (m_pPreviousPosition); Ogre::Matrix4 prev_viewMatrix = Ogre::Math::makeViewMatrix(estimatedPosition, estimatedOrientation);//.inverse().transpose(); // compute final matrix prevviewproj = projectionMatrix * prev_viewMatrix; // update position and orientation for next update time m_pPreviousOrientation = current_orientation; m_pPreviousPosition = current_position; } } }
// this callback we will use to modify SSAO parameters void notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat) { if (pass_id != 42) // not SSAO, return return; // this is the camera you're using Ogre::Camera *cam = mInstance->getChain()->getViewport()->getCamera(); // calculate the far-top-right corner in view-space Ogre::Vector3 farCorner = cam->getViewMatrix(true) * cam->getWorldSpaceCorners()[4]; // get the pass Ogre::Pass *pass = mat->getBestTechnique()->getPass(0); // get the vertex shader parameters Ogre::GpuProgramParametersSharedPtr params = pass->getVertexProgramParameters(); // set the camera's far-top-right corner if (params->_findNamedConstantDefinition("farCorner")) params->setNamedConstant("farCorner", farCorner); // get the fragment shader parameters params = pass->getFragmentProgramParameters(); // set the projection matrix we need static const Ogre::Matrix4 CLIP_SPACE_TO_IMAGE_SPACE( 0.5, 0, 0, 0.5, 0, -0.5, 0, 0.5, 0, 0, 1, 0, 0, 0, 0, 1); if (params->_findNamedConstantDefinition("ptMat")) params->setNamedConstant("ptMat", CLIP_SPACE_TO_IMAGE_SPACE * cam->getProjectionMatrixWithRSDepth()); if (params->_findNamedConstantDefinition("far")) params->setNamedConstant("far", cam->getFarClipDistance()); }
void Stall::SetPosition(Ogre::Rectangle2D *rect, Ogre::Vector3 pos, float size) { Ogre::Camera *camera = CommonDeclarations::GetCamera()->GetOgreCamera(); Ogre::Matrix4 vm = camera->getViewMatrix(); Ogre::Matrix4 pm = camera->getProjectionMatrix(); Ogre::Vector3 vpos = vm*pos; Ogre::Vector3 ppos = pm*vpos; rect->setCorners(ppos.x-size, ppos.y+size, ppos.x+size/AspectRatio, ppos.y-size); }
Ogre::Vector2 DemoApp::GetScreenspaceCoords(const Ogre::Vector3& iPoint, const Ogre::Camera& iCamera) { Ogre:: Vector3 point = iCamera.getProjectionMatrix() * (iCamera.getViewMatrix() * iPoint); Ogre::Vector2 screenSpacePoint = Ogre::Vector2::ZERO; screenSpacePoint.x = (point.x / 2.f) + 0.5f; screenSpacePoint.y = (point.y / 2.f) + 0.5f; return screenSpacePoint; }
void TextComponent::OnUpdate(double time_diff) { if(mRefresh && mFont != "") { // calculate the text width mTextWidth = 0; Ogre::Font* font = dynamic_cast<Ogre::Font*>(Ogre::FontManager::getSingleton().getByName(Utils::ToStdString(mFont)).getPointer()); if(font == nullptr) { Logger::Get().Warning("Cannot find font: \"" + mFont + "\"."); } else { std::string str = Utils::ToStdString(mText); for(Ogre::String::iterator iter = str.begin(); iter < str.end(); ++iter) { if(*iter == 0x0020) { mTextWidth += font->getGlyphAspectRatio(0x0030); } else { mTextWidth += font->getGlyphAspectRatio(*iter); } } mTextWidth *= mFontSize; } mRefresh = false; } // set the position if(DisplayManager::Get()->GetMainCamera() == nullptr) { Logger::Get().Error("Cannot get main camera for text component: no main camera set. Disabling text component " + mName + "."); Disable(); return; } Ogre::Camera* camera = DisplayManager::Get()->GetMainCamera()->GetCamera(); Ogre::Vector3 screen_pos(camera->getProjectionMatrix() * camera->getViewMatrix() * GetNode()->GetPosition(Node::SCENE)); if(screen_pos.z >= 1) { // behind or in the camera, hide mOverlay->hide(); } else { mOverlay->show(); } float x = 1.0f - ((-screen_pos.x * 0.5f) + 0.5f); // 0 <= x <= 1 // left := 0,right := 1 float y = ((-screen_pos.y * 0.5f) + 0.5f); // 0 <= y <= 1 // bottom := 0,top := 1 x *= camera->getViewport()->getActualWidth(); y *= camera->getViewport()->getActualHeight(); mPanel->setMetricsMode(Ogre::GMM_PIXELS); mPanel->setWidth(mTextWidth + 2 * mPadding.x); mPanel->setHeight(mFontSize + 2 * mPadding.y); mPanel->setLeft(x - mTextWidth / 2 - mPadding.x); mPanel->setTop(y - mFontSize / 2 - mPadding.y); mLabel->setPosition(mPadding.x, mPadding.y); mLabel->setDimensions(mTextWidth, mFontSize); }
void MovableTextArea::getWorldTransforms(Ogre::Matrix4 *xform) const { Ogre::Camera* camera = Ogre::Root::getSingleton().getAutoCreatedWindow()->getViewport(0)->getCamera(); Ogre::Vector3 p = m_Position; p.z = p.z + m_AdditionalHeight; p = camera->getProjectionMatrix() * camera->getViewMatrix() * p; //p.x = 1 + p.x; //p.y = -1 + p.y; // we round aligment to pixel border to reduce blurring int width = Ogre::OverlayManager::getSingleton().getViewportWidth() / 2; int height = Ogre::OverlayManager::getSingleton().getViewportHeight() / 2; p.x = (float)((int)((1 + p.x) * width)) / width; p.y = (float)((int)((-1 + p.y) * height)) / height; p.z = 0; *xform = Ogre::Matrix3::IDENTITY; xform->setTrans(p); }
void GodRaysListener::notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat) { if(pass_id !=1 && pass_id !=2 && pass_id !=3 ) { return ; } // this is the camera you're using #ifndef ROAD_EDITOR Ogre::Camera *cam = mApp->mSplitMgr->mCameras.front(); #else Ogre::Camera *cam = mApp->mCamera; #endif //update the sun position Ogre::Light* sun =((App*)mApp)->sun; Ogre::GpuProgramParametersSharedPtr params= mat->getTechnique(0)->getPass(0)->getVertexProgramParameters(); Ogre::GpuProgramParametersSharedPtr fparams= mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters(); //disable god rays when the sun is not facing us float enable=0.0f; if(sun != NULL) { Ogre::Vector3 sunPosition = sun->getDirection() *100; Ogre::Vector3 worldViewPosition = cam->getViewMatrix() * sunPosition; Ogre::Vector3 hcsPosition = cam->getProjectionMatrix() * worldViewPosition; float unclampedLuminance = abs(hcsPosition.x)+abs(hcsPosition.y); clamp(hcsPosition); Ogre::Vector2 sunScreenSpacePosition = Ogre::Vector2(0.5f + (0.5f * hcsPosition.x), 0.5f + (0.5f * -hcsPosition.y)); SunScreenSpacePosition = Ogre::Vector4 ( sunScreenSpacePosition.x, sunScreenSpacePosition.y, 0, 1 ); enable = (1.0f / ((unclampedLuminance > 1.0f) ? unclampedLuminance : 1.0f)) * (hcsPosition.z < 1 ? 0.0f : 1.0f); } params->setNamedConstant("lightPosition", SunScreenSpacePosition); fparams->setNamedConstant("enableEffect", enable); }
BOOL CEngineInterface::Axis_Trans(AXIS_TYPE typeSource, const fVector3& fvSource, AXIS_TYPE typeTar, fVector3& fvTarget) { register Fairy::TerrainData* pTerrainData = m_pFairySystem->getTerrainData(); // 尚未加载 if(pTerrainData && (pTerrainData->getXSize()==0 || pTerrainData->getZSize()==0)) { pTerrainData = 0; } fVector3 fvScale = GetSacle(); //检查是否是合法的坐标 fvTarget = fvSource; if(!Axis_CheckValid(typeSource, fvSource)) return FALSE; if(typeSource == typeTar) return TRUE; switch(typeSource) { //----------------------------------------------- case AX_GAME: { if(AX_PLAN==typeTar) return TRUE; else if(AX_GFX == typeTar) { if(pTerrainData) { fvTarget.x = pTerrainData->mPosition.x + (FLOAT)fvSource.x * pTerrainData->mScale.x; fvTarget.y = pTerrainData->mPosition.y + (FLOAT)fvSource.y * pTerrainData->mScale.y; fvTarget.z = pTerrainData->mPosition.z + (FLOAT)fvSource.z * pTerrainData->mScale.z; } else { fvTarget.x = (FLOAT)fvSource.x * fvScale.x; fvTarget.y = (FLOAT)fvSource.y * fvScale.y; fvTarget.z = (FLOAT)fvSource.z * fvScale.z; } return TRUE; } else if(AX_SCREEN==typeTar) { KLThrow("(AX_GAME->AX_SCREEN) Not support yet!"); } } break; //----------------------------------------------- case AX_PLAN: { if(!pTerrainData) return FALSE; FLOAT fGfxX = pTerrainData->mPosition.x + (FLOAT)fvSource.x * pTerrainData->mScale.x; FLOAT fGfxZ = pTerrainData->mPosition.z + (FLOAT)fvSource.z * pTerrainData->mScale.z; if(AX_GAME==typeTar) { //取得相地形高度作为Y坐标(游戏坐标) fvTarget.y = (pTerrainData->getHeightAtGrid(fvSource.x, fvSource.z)-pTerrainData->mPosition.y)/ pTerrainData->mScale.y; return TRUE; } else if(AX_GFX == typeTar) { //取得相地形高度作为Y坐标 fvTarget.x = fGfxX; fvTarget.z = fGfxZ; fvTarget.y = pTerrainData->getHeightAtGrid(fvSource.x, fvSource.z); return TRUE; } else if(AX_SCREEN==typeTar) { KLThrow("(AX_PLAN->AX_SCREEN) Not support yet!"); } } break; //----------------------------------------------- case AX_GFX: { if(AX_GAME==typeTar || AX_PLAN==typeTar) { if(pTerrainData) { fvTarget.x = (fvSource.x-pTerrainData->mPosition.x)/pTerrainData->mScale.x; fvTarget.y = (fvSource.y-pTerrainData->mPosition.y)/pTerrainData->mScale.y; fvTarget.z = (fvSource.z-pTerrainData->mPosition.z)/pTerrainData->mScale.z; } else { fvTarget.x = (fvSource.x)/fvScale.x; fvTarget.y = (fvSource.y)/fvScale.y; fvTarget.z = (fvSource.z)/fvScale.z; } return TRUE; } else if(AX_SCREEN==typeTar) { //KLThrow("(AX_GFX->AX_SCREEN) Not support yet!"); if(!pTerrainData) return FALSE; //得到Ogre相机 Ogre::Camera* pOgreCamera = m_pCamera_Current->GetOgreCamera(); if(!pOgreCamera) return FALSE; if(!(pOgreCamera->isVisible(Ogre::Vector3(fvSource.x, fvSource.y, fvSource.z)))) return FALSE; // x:[-1w, 1w] z:[-1h, 1h] Ogre::Vector3 vRet = pOgreCamera->getProjectionMatrix()* pOgreCamera->getViewMatrix() * Ogre::Vector3(fvSource.x, fvSource.y, fvSource.z); int nWidth = m_pFairySystem->getViewport()->getActualWidth(); int nHeight = m_pFairySystem->getViewport()->getActualHeight(); fvTarget.x = (( vRet.x + 1.0f)*nWidth/2.0f); fvTarget.y = ((-vRet.y + 1.0f)*nHeight/2.0f); return TRUE; } } break; //----------------------------------------------- case AX_SCREEN: { if(!pTerrainData) return FALSE; Ogre::Vector3 vRenderPos; BOOL bRet = m_pFairySystem->getTerrainIntersects(Fairy::Point((INT)fvSource.x, fvSource.y), vRenderPos); if(!bRet) return FALSE; if(AX_GAME==typeTar || AX_PLAN==typeTar) { return Axis_Trans(AX_GFX, fVector3(vRenderPos.x, vRenderPos.y, vRenderPos.z), AX_GAME, fvTarget); } else if(AX_GFX==typeTar) { fvTarget = fVector3(vRenderPos.x, vRenderPos.y, vRenderPos.z); return TRUE; } } break; } return FALSE; }
void EC_OgreMovableTextOverlay::Update() { if (!node_ || !visible_ || !placeable_ || renderer_.expired()) return; if(!node_->isInSceneGraph()) { overlay_->hide(); return; } Ogre::Camera* camera = renderer_.lock()->GetCurrentCamera(); if (!camera) return; Ogre::Viewport* viewport = camera->getViewport(); Ogre::Vector3 point = node_->_getDerivedPosition(); // Is the camera facing that point? If not, hide the overlay and return. Ogre::Plane cameraPlane = Ogre::Plane(Ogre::Vector3(camera->getDerivedOrientation().zAxis()), camera->getDerivedPosition()); if(cameraPlane.getSide(point) != Ogre::Plane::NEGATIVE_SIDE) { overlay_->hide(); return; } // Hide the overlay if it's too far. Ogre::Vector3 res = camera->getDerivedPosition() - point; float distance = sqrt(res.x * res.x + res.y * res.y + res.z * res.z); if (distance > MAX_VISIBILITY_DISTANCE) { overlay_->hide(); return; } // Set the alpha channel for the overlay. if (materialHasAlpha_) SetAlphaChannelIntensity(distance); // Derive the 2D screen-space coordinates for node point. point = camera->getProjectionMatrix() * (camera->getViewMatrix() * point); // Transform from coordinate space [-1, 1] to [0, 1] float x = (point.x / 2) + 0.5f; float y = 1 - ((point.y / 2) + 0.5f); // Update the position (centering the text) container_->setPosition(x - (textDim_.x / 2), y); // Update the dimensions also if the window is resized. if (windowWidth_ != viewport->getActualWidth() || windowHeight_ != viewport->getActualHeight()) { windowWidth_ = viewport->getActualWidth(); windowHeight_ = viewport->getActualHeight(); textDim_ = GetTextDimensions(text_); container_->setDimensions(textDim_.x, textDim_.y); } ///\todo Scale the text and width and height of the container? // text_element_->setMetricsMode(Ogre::GMM_RELATIVE); // text_element_->setPosition(textDim_.x, textDim_.y); // text_element_->setPosition(textDim_.x / 10, 0.01); // text_element_->setCharHeight(max_x - min_x/*2*0.0175f*///); overlay_->show(); }