转自nehe的教程:Using gluUnProject http://nehe.gamedev.net/article/using_gluunproject/16013/
CVector3 GetOGLPos(int x, int y)
{
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ ); //获取深度值
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ); //使用glu,如果高版本的opengl,3D数学库中通常包含此函数,
return CVector3(posX, posY, posZ);
}
来源:CSDN
作者:漂_泊
链接:https://blog.csdn.net/iMatt/article/details/103829532