title: Incorrect 3D coordinates from 2D mouse click in OpenGL?
body: <p>I want to get the mouse click position in 3D. Below is my code, its simple, What wrong in it. where I am doing wrong.. ? Why I am not getting the exact values of x and y ? any idea ?</p>
  
  <pre><code>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, &amp;winZ );
          gluUnProject( winX, winY, winZ, OGLMmodelview, OGLMprojection, OGLMviewport, &amp;posX, &amp;posY, &amp;posZ);
          return GLpoint(posX, posY, posZ);
      }
  </code></pre>