我想在 3D 中获得鼠标点击位置。下面是我的代码,很简单,有什么问题。我在哪里做错了..?为什么我没有得到 x 和 y 的确切值?任何的想法 ?
void glPerspective()
{
glViewport(0, 0, WINDOW_SIZE_W, WINDOW_SIZE_H);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLdouble)WINDOW_SIZE_W / (GLdouble)WINDOW_SIZE_H, 0.1, 100000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, m_zoom, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPerspective(); // perspective view
Some_rotation_and_translation();
glGetDoublev( GL_PROJECTION_MATRIX, OGLMprojection );
glGetDoublev( GL_MODELVIEW_MATRIX, OGLMmodelview );
glGetIntegerv( GL_VIEWPORT, OGLMviewport );
Render_Triangular_model();
glPopMatrix();
swapbuffer();
}
GLpoint GetOGLMousePos(GLint x, GLint y)
{
GLdouble winX = 0.0, winY = 0.0, winZ = 0.0;
GLdouble posX = 0.0, posY = 0.0, posZ = 0.0;
winX = (float)x;
winY = (float)OGLMviewport[3] - (float)y; // invert winY so that down lowers value
glReadPixels( x, GLint(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
gluUnProject( winX, winY, winZ, OGLMmodelview, OGLMprojection, OGLMviewport, &posX, &posY, &posZ);
return GLpoint(posX, posY, posZ);
}