PDA

View Full Version : Mouse Coordinates



ToddAtWSU
08-24-2006, 11:37 AM
I am creating a Qt-based application with a QGLWidget inside it. I want to use picking and know how to capture the screen coordinates of my mouse location, but I thought there might be some functionality to convert this screen coordinate to the world coordinate. Does OpenGL provide something like this or do I need to calculate it by hand everytime? Thanks!

glAren
08-24-2006, 12:28 PM
I use this function:

vec3 getPixelCoordAtMousePos ()
{
POINT mouse;
GetCursorPos (&mouse);
ScreenToClient (m_hwnd, &mouse);

int viewport[4];
double modelview[16];
double projection[16];

glGetDoublev (GL_MODELVIEW_MATRIX, modelview);
glGetDoublev (GL_PROJECTION_MATRIX, projection);
glGetIntegerv (GL_VIEWPORT, viewport);

float winX = (float)mouse.x;
float winY = (float)m_height - (float)mouse.y; //(float)viewport[3] - (float)mouse.y;
float winZ;

glReadPixels ((int)winX, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
double finalCoordX, finalCoordY, finalCoordZ;

if (winZ == 1.0)
return vec3 (0.0, 0.0, 0.0);
else
{
gluUnProject (winX, winY, winZ, modelview, projection, viewport, &finalCoordX, &finalCoordY, &finalCoordZ);
return vec3 (finalCoordX, finalCoordY, finalCoordZ);
}
}

ToddAtWSU
08-25-2006, 05:13 AM
So gluUnProject will put the ortho-equivalents to the screen coordinates into finalCoordX, finalCoordY, and finalCoordZ? Thanks again!