luismesas
20-08-2009 10:35:10
I have ported the Ogre wiki page with snippet to convert 3D points to 2D point proyected to camera.
http://www.ogre3d.org/wiki/index.php/MOGRE_Projecting_3D_position_to_2D
http://www.ogre3d.org/wiki/index.php/MOGRE_Projecting_3D_position_to_2D
// using MOGRE;
// returns a vector with x,y with clamped screencoords in [-1;1]
public Vector2 ProjectPos (Camera cam, Vector3 pos)
{
Vector2 ret = new Vector2();
Vector3 eyeSpacePos = cam.GetViewMatrix(true) * pos;
if (eyeSpacePos.z < 0)
{
Vector3 screenSpacePos = cam.ProjectionMatrix * eyeSpacePos;
ret.x = screenSpacePos.x;
ret.y = screenSpacePos.y;
}
else
{
ret.x = (-eyeSpacePos.x > 0) ? -1 : 1;
ret.y = (-eyeSpacePos.y > 0) ? -1 : 1;
}
return ret;
}