public void GetWorldCoord(int ix, int iy, double fz, ref msgVectorStruct coord) { double x, y, z, winX, winY, winZ; // Fix the yPos value. MS Windows origin 0,0 is upper left // while OpenGL windows origin 0,0 is lower left... winX = ix; winY = m_iViewport[3] - iy; // Add the camera's focal length, or distance from 'LookAt' to 'Eye' position // to the given 'z' coordinate. fz += GetFocalLength(); // Calculate the winZ coordinate: if (m_bPerspective) { // Compensate for perspective view winZ = 0.5 + (((m_fFar + m_fNear) - (2 * m_fFar * m_fNear) / fz)) / (2 * (m_fFar - m_fNear)); } else { // winZ is linearly interpolated between the Near_Far clipping plane winZ = (fz - m_fNear) / (m_fFar - m_fNear); } // Unproject the point unsafe { fixed(double *m_dModelViewMatrixPtr = m_dModelViewMatrix) { fixed(double *m_dProjectionMatrixPtr = m_dProjectionMatrix) { fixed(int *m_iViewportPtr = m_iViewport) { OpenGLControl.gluUnProject(winX, winY, winZ, m_dModelViewMatrixPtr, m_dProjectionMatrixPtr, m_iViewportPtr, &x, &y, &z); } } } } coord.x = x; coord.y = y; coord.z = z; }