首先介绍Ogre中的方法:
Vector3 tartgetpos;
Ogre::Matrix4 viewMat = mCamera->getViewMatrix();
Ogre::Matrix4 projMat = mCamera->getProjectionMatrix();
Ogre::Vector3 screenPos = projMat * viewMat * tartgetpos;
float x = (screenPos.x + 1.0f)/ 2.0f;
float y = -1*(screenPos.y - 1.0f) / 2.0f;
再谈谈直接使用OpenGL的方法:
Vector3 pos(...); //物体的位置
double xfpcoord[3];
gluProject(pos.x,pos.y,pos.z,cam_model_mat,cam_proj_mat,cam_viewport,&xfpcoord[0],&xfpcoord[1],&xfpcoord[2]);
参数解释一下,
cam_model_mat: camera model matrix
cam_proj_mat: camera projection matrix
cam_viewport: camera viewport
xfpcoord[0]: 屏幕X
xfpcoord[1]: 屏幕Y
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhucde/archive/2010/12/07/6060095.aspx