void CObjectTitle::update(Ogre::SceneNode *pUnitObjectNode, Ogre::SceneNode *pCameraNode) { float distance = getDistance(pUnitObjectNode->getPosition().x, pUnitObjectNode->getPosition().z, pCameraNode->getPosition().x, pCameraNode->getPosition().z); if(distance > 50) { m_pOverlay->hide(); return; } if(!m_pObject->isInScene()) { m_pOverlay->hide(); return; } // Derive the average point between the top-most corners of the object's bounding box const Ogre::AxisAlignedBox &AABB = m_pObject->getWorldBoundingBox(true); Ogre::Vector3 point = (AABB.getCorner(Ogre::AxisAlignedBox::FAR_LEFT_TOP) + AABB.getCorner(Ogre::AxisAlignedBox::FAR_RIGHT_TOP) + AABB.getCorner(Ogre::AxisAlignedBox::NEAR_LEFT_TOP) + AABB.getCorner(Ogre::AxisAlignedBox::NEAR_RIGHT_TOP)) / 4; // Is the camera facing that point? If not, hide the overlay and return. Ogre::Plane cameraPlane = Ogre::Plane(Ogre::Vector3(m_pCamera->getDerivedOrientation().zAxis()), m_pCamera->getDerivedPosition()); if(cameraPlane.getSide(point) != Ogre::Plane::NEGATIVE_SIDE) { m_pOverlay->hide(); return; } // Derive the 2D screen-space coordinates for that point point = m_pCamera->getProjectionMatrix() * (m_pCamera->getViewMatrix() * point); // Transform from coordinate space [-1, 1] to [0, 1] Ogre::Real x = (point.x / 2) + 0.5f; Ogre::Real y = 1 - ((point.y / 2) + 0.5f); // Update the position (centering the text) m_pContainer->setPosition(x - (m_textDim.x / 6), y - 0.05f); m_pOverlay->show(); }
void ObjectTitle::update() { if(!object->isInScene()) { setVisible(false); overlay->hide(); return; } // Derive the average point between the top-most corners of the object's bounding box const Ogre::AxisAlignedBox &AABB = object->getWorldBoundingBox(true); Ogre::Vector3 point = (AABB.getCorner(AxisAlignedBox::FAR_LEFT_TOP) + AABB.getCorner(AxisAlignedBox::FAR_RIGHT_TOP) + AABB.getCorner(AxisAlignedBox::NEAR_LEFT_TOP) + AABB.getCorner(AxisAlignedBox::NEAR_RIGHT_TOP)) / 4; // Is the camera facing that point? If not, hide the overlay and return. Ogre::Plane cameraPlane = Plane(Vector3(camera->getDerivedOrientation().zAxis()), camera->getDerivedPosition()); if(cameraPlane.getSide(point) != Plane::NEGATIVE_SIDE) { setVisible(false); overlay->hide(); return; } // Derive the 2D screen-space coordinates for that point point = camera->getProjectionMatrix() * (camera->getViewMatrix() * point); // Transform from coordinate space [-1, 1] to [0, 1] Real x = (point.x / 2) + 0.5f; Real y = 1 - ((point.y / 2) + 0.5f); // Update the position (centering the text) container->setPosition(x - (textDim.x / 2), y); setVisible(true); overlay->show(); }
void MovableTextOverlay::_getMinMaxEdgesOfTopAABBIn2D(Ogre::Real& MinX, Ogre::Real& MinY, Ogre::Real& MaxX, Ogre::Real& MaxY) { const Ogre::Camera* mpCam = mAttrs->getCamera(); MinX = 0; MinY = 0; MaxX = 0; MaxY = 0; Ogre::Real X[4];// the 2D dots of the AABB in screencoordinates Ogre::Real Y[4]; if (!mpMov->isInScene()) return; const Ogre::AxisAlignedBox &AABB = mpMov->getWorldBoundingBox(true);// the AABB of the target const Ogre::Vector3 CornersOfTopAABB[4] = { AABB.getCorner(AxisAlignedBox::FAR_LEFT_TOP), AABB.getCorner(AxisAlignedBox::FAR_RIGHT_TOP), AABB.getCorner(AxisAlignedBox::NEAR_LEFT_TOP), AABB.getCorner(AxisAlignedBox::NEAR_RIGHT_TOP)}; Ogre::Vector3 CameraPlainNormal = mpCam->getDerivedOrientation().zAxis();//The normal vector of the plaine.this points directly infront of the cam Ogre::Plane CameraPlain = Plane(CameraPlainNormal,mpCam->getDerivedPosition());//the plaine that devides the space bevor and behin the cam for (int i = 0; i < 4; i++) { X[i] = 0; Y[i] = 0; _getScreenCoordinates(CornersOfTopAABB[i],X[i],Y[i]);// transfor into 2d dots if (CameraPlain.getSide(CornersOfTopAABB[i]) == Plane::NEGATIVE_SIDE) { if (i == 0)// accept the first set of values, no matter how bad it might be. { MinX = X[i]; MinY = Y[i]; MaxX = X[i]; MaxY = Y[i]; } else// now compare if you get "better" values { if (MinX > X[i])// get the x minimum { MinX = X[i]; } if (MinY > Y[i])// get the y minimum { MinY = Y[i]; } if (MaxX < X[i])// get the x maximum { MaxX = X[i]; } if (MaxY < Y[i])// get the y maximum { MaxY = Y[i]; } } } else { MinX = 0; MinY = 0; MaxX = 0; MaxY = 0; break; } } }
void TextCanvasBatch::update() { std::vector<TextCanvas*> visibleTexts; for( TextCanvasMap::iterator i = mTextMap.begin(); i != mTextMap.end(); i++) { if( !i->second->mTarget ) continue; if( !i->second->mTarget->isInScene() ) continue; // Derive the average point between the top-most corners of the object's bounding box const Ogre::AxisAlignedBox &AABB = i->second->mTarget->getWorldBoundingBox(true); Ogre::Vector3 point = (AABB.getCorner(Ogre::AxisAlignedBox::FAR_LEFT_TOP) + AABB.getCorner(Ogre::AxisAlignedBox::FAR_RIGHT_TOP) + AABB.getCorner(Ogre::AxisAlignedBox::NEAR_LEFT_TOP) + AABB.getCorner(Ogre::AxisAlignedBox::NEAR_RIGHT_TOP)) / 4; // Is the camera facing that point? If not, hide the overlay and return. Ogre::Plane cameraPlane = Ogre::Plane(Ogre::Vector3(mCamera->getDerivedOrientation().zAxis()), mCamera->getDerivedPosition()); if(cameraPlane.getSide(point) != Ogre::Plane::NEGATIVE_SIDE) continue; Ogre::Real distance = mCamera->getDerivedPosition().distance(point); // scale [40, 400] to [5, 0]: (-5 / 360)x + (50 / 9) = y distance = Ogre::Math::Floor((-10 / 360.0)*distance + (100 / 9.0)); if(distance < 0) distance = 0; else if(distance > 10) distance = 10; // Derive the 2D (x,y) screen-space coordinates for that point point = mCamera->getProjectionMatrix() * (mCamera->getViewMatrix() * point); Ogre::Real x = (point.x / 2) + 0.5f; Ogre::Real y = 1 - ((point.y / 2) + 0.5f); i->second->mPosition = Ogre::Vector3(x, y, distance); visibleTexts.push_back( i->second ); } mCanvas.clear(); const Ogre::uint charPerLine = 24; const Ogre::uint maxCharPerLine = 36; for(std::vector<TextCanvas*>::iterator i = visibleTexts.begin(); i != visibleTexts.end(); i++) { int x = (*i)->mPosition.x * mCamera->getViewport()->getActualWidth(); int y = (*i)->mPosition.y * mCamera->getViewport()->getActualHeight(); Ogre::uint fontSize = (*i)->mPosition.z + 10; Ogre::Real fontHeight = mAtlas.getFontMetrics(mFont, fontSize).height; Ogre::Real avgAdvance = mAtlas.getGlyphInfo(mFont, fontSize, 'x').advance; Ogre::Real spaceAdvance = mAtlas.getGlyphInfo(mFont, fontSize, 'i').advance; Ogre::Real pen = x = x - (std::min((*i)->mCaption.length(), charPerLine) * avgAdvance) / 2; y -= ((*i)->mCaption.length() / charPerLine) * fontHeight; Ogre::uint lineCharCount = 0; for(Ogre::String::const_iterator j = (*i)->mCaption.begin(); j != (*i)->mCaption.end(); j++) { lineCharCount++; if(*j == ' ') { if(lineCharCount > charPerLine) { pen = x; y += fontHeight; lineCharCount = 0; } else { pen += spaceAdvance; } continue; } if(lineCharCount > maxCharPerLine) { pen = x; y += fontHeight; lineCharCount = 0; } GlyphInfo glyph = mAtlas.getGlyphInfo(mFont, fontSize, (*j)); mCanvas.drawGlyph(glyph, glyph.bearingX + pen + 1, y - glyph.bearingY + 1, glyph.texInfo.width, glyph.texInfo.height, Ogre::ColourValue(0, 0, 0, 0.7)); mCanvas.drawGlyph(glyph, glyph.bearingX + pen, y - glyph.bearingY, glyph.texInfo.width, glyph.texInfo.height, toColour<Ogre::ColourValue>( (*i)->mColour )); pen += glyph.advance; } } }
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(); }