1. ## Mouse Coordinates

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!

2. ## Re: Mouse Coordinates

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);
}
}

3. ## Re: Mouse Coordinates

So gluUnProject will put the ortho-equivalents to the screen coordinates into finalCoordX, finalCoordY, and finalCoordZ? Thanks again!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•