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;
}
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;
}
相关文章推荐
- ogre世界坐标与屏幕坐标相互转换
- ogre世界坐标鱼屏幕坐标相互转换
- NGUI 与世界坐标与屏幕坐标之间的转换的帮助类
- cocos2d-x -------------------------------世界坐标本地坐标之间的转换
- 世界坐标本地坐标之间的转换
- cocos2dx中。两个节点之间,世界坐标与本地坐标之间的转换
- Unity世界坐标与屏幕坐标之间如何转换
- OSG学习笔记25——屏幕坐标——相机坐标——世界坐标之间的转换
- UE4 屏幕坐标转换到世界坐标
- [unity3d]屏幕坐标跟世界坐标的转换
- windows 屏幕坐标 窗口坐标 客户区坐标 逻辑坐标 设备坐标之间的关系及转换
- 坐标变换_世界坐标系与用户坐标系之间的转换
- 屏幕坐标到opengl世界坐标转换
- Unity3D世界的坐标与小地图UI上对应坐标之间的转换
- 两种屏幕-世界坐标转换的性能比较
- 屏幕坐标和世界坐标的转换+对象池技术(3D打地鼠小游戏)
- Unity3D屏幕坐标到世界坐标转换浅析
- NGUI,世界坐标,屏幕坐标转换
- unity 屏幕坐标&世界坐标相互转换
- OGRE中OIS与CEGUI的屏幕坐标转换