您的位置:首页 > 其它

ogre 世界坐标、本地坐标、屏幕坐标之间的转换

2017-06-06 17:19 971 查看
1.本地坐标转为世界坐标

void local2World(const Vector3 locP, Vector3 &worldP, const SceneNode* node)
{
Matrix4 worldMat;
node->getWorldTransforms(&worldMat); //获取该物体的世界坐标矩阵
worldP = worldMat * locP;
}
2.世界坐标转为屏幕坐标

#define EPSILON 0.000001
bool world2Screen(Vector3 objPos, Vector2& screenPos)
{
Matrix4 viewMat = mCamera->getViewMatrix();
Matrix4 projMat = mCamera->getProjectionMatrix();

Vector4 inP = Vector4(objPos.x, objPos.y, objPos.z ,1.0);
Vector4 outP = viewMat * inP;
outP = projMat * outP;

if(outP.w <= EPSILON)
return false;

outP.x /= outP.w;
outP.y /= outP.w;
outP.z /= outP.w;

  //[-1,1]->[0,1]
outP.x = outP.x*0.5 + 0.5;
outP.y = outP.y*0.5 + 0.5;
outP.z = outP.z*0.5 + 0.5;

outP.x = outP.x * mWindow->getWidth();
outP.y = (1-outP.y) * mWindow->getHeight();

screenPos.x = outP.x;
screenPos.y = outP.y;
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: