int gluUnProject ( GLdouble winx, GLdouble winy, GLdouble winz, const GLdouble modelMatrix[16], const GLdouble projMatrix[16], const GLint viewport[4], GLdouble *objx, GLdouble *objy, GLdouble *objz); //这个函数使用由模型视图矩阵(modelMatrix)、投影矩阵(projMatrix)、和视口(viewport)定义的变换,把指定的窗口二维坐标(winx, winy, winz)映射到物体三维坐标 //它产生的物体坐标是在objx、objy、objz中返回的,这个函数返回GL_TRUE表示返回成功或GL_FALSE表示失败。窗口深度坐标是winz根据gldepthRange()指定的,范围[0,1]。
实例:
void mouse(int button, int state, int x, int y) { GLint viewport[4]; GLdouble mvmatrix[16], projmatrix[16]; GLint realy; /* OpenGL y coordinate position */ GLdouble wx, wy, wz; /* returned world x, y, z coords */ switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { glGetIntegerv (GL_VIEWPORT, viewport);//获取视口 glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);//获取模型视图矩阵 glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);//获取投影矩阵 /* note viewport[3] is height of window in pixels */ realy = viewport[3] - (GLint) y - 1; printf ("Coordinates at cursor are (%4d, %4d)\n", x, realy); gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0, //所求得的物体三维坐标位于近侧裁剪平面上0.0 mvmatrix, projmatrix, viewport, &wx, &wy, &wz); printf ("World coords at z=0.0 are (%f, %f, %f)\n", wx, wy, wz); gluUnProject ((GLdouble) x, (GLdouble) realy, 1.0, //所求得的物体三维坐标位于远侧裁剪平面上1.0 mvmatrix, projmatrix, viewport, &wx, &wy, &wz); printf ("World coords at z=1.0 are (%f, %f, %f)\n", wx, wy, wz); } break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) exit(0); break; default: break; } }
int APIENTRY gluProject ( GLdouble objx, GLdouble objy, GLdouble objz, const GLdouble modelMatrix[16], const GLdouble projMatrix[16], const GLint viewport[4], GLdouble *winx, GLdouble *winy, GLdouble *winz); //与gluUnProject()功能相反,其作用是将物体坐标变换为窗口坐标
《高效学习OpenGL》 之 逆变换和模拟变换 gluUnProject(),gluProject()
原文:http://blog.csdn.net/biggbang/article/details/19476151