GetScreenspaceCoords
Get screenspace coordinates of a MovableObject
Welcome to the new Ogre Wiki!
If you haven't done so already, be sure to visit the Wiki Portal to read about how the wiki works. Especially the Ogre Wiki Overview page.
If you haven't done so already, be sure to visit the Wiki Portal to read about how the wiki works. Especially the Ogre Wiki Overview page.
Obtaining the screenspace (2D) coordinates of a 3D MovableObject is a fairly common issue. The most common need for this ability is for determining where to place an object's title (an overlay) on the screen.
/** * This little snippet gets the screenspace coordinates for a MovableObject * * @param object The object to retrieve the coordidnates of. * @param camera The active camera * @param result The Vector2 to store the result in * * @return Returns true if the object is visible and the coordinates were * retrieved, false otherwise. */ bool getScreenspaceCoords(Ogre::MovableObject* object, Ogre::Camera* camera, Ogre::Vector2& result) { if(!object->isInScene()) return false; const Ogre::AxisAlignedBox &AABB = object->getWorldBoundingBox(true); /** * If you need the point above the object instead of the center point: * This snippet derives the average point between the top-most corners of the bounding box * 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; */ // Get the center point of the object's bounding box Ogre::Vector3 point = AABB.getCenter(); // Is the camera facing that point? If not, return false Ogre::Plane cameraPlane = Plane(Vector3(camera->getDerivedOrientation().zAxis()), camera->getDerivedPosition()); if(cameraPlane.getSide(point) != Plane::NEGATIVE_SIDE) return false; // Transform the 3D point into screen space point = camera->getProjectionMatrix() * (camera->getViewMatrix() * point); // Transform from coordinate space [-1, 1] to [0, 1] and update in-value result.x = (point.x / 2) + 0.5f; result.y = 1 - ((point.y / 2) + 0.5f); return true; }
Contributors to this page: jacmoe
and
Ajs15822
.
Page last modified on Saturday 02 of January, 2010 04:06:18 GMT by jacmoe
.
The content on this page is licensed under the terms of the Creative Commons Attribution-ShareAlike License.
As an exception, any source code contributed within the content is released into the Public Domain.
Sidebar
Search box
Online users
63
online users

