nhartung

06-21-2010, 10:00 PM

Right now i'm working on a 2D tile based map editor and i'm attempting to convert mouse coordinates to vertex coordinates (if that makes sense). Basically I want to know which tile the user is clicking on based on the mouse coordinates.

I have been looking for awhile and i think glUnProject is the way to go. Although I could have completely misinterpreted the use of this function. I have looked at a few tutorials and have managed the following attempt using one of NeHe's tutorials:

GLint viewport[4];

GLdouble modelview[16];

GLdouble projection[16];

GLfloat winX, winY, winZ;

GLdouble posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );

glGetDoublev( GL_PROJECTION_MATRIX, projection );

glGetIntegerv( GL_VIEWPORT, viewport );

cout << mouse_x << " " << mouse_y << '\n';

winX = (float)mouse_x;

winY = (float)viewport[3] - (float)mouse_y;

glReadPixels( mouse_x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

cout << winZ << '\n';

cout << posX << " " << posY << " " << posZ << '\n';

This is giving me some odd results. The final values are something like -9.25596e+061 for every value. Since this is a just a 2d application I tried using winZ to be zero (it is actually evaluating to 0 anyway when I use glReadPixels) so i'm not sure if this is causing any problems.

I have been looking for awhile and i think glUnProject is the way to go. Although I could have completely misinterpreted the use of this function. I have looked at a few tutorials and have managed the following attempt using one of NeHe's tutorials:

GLint viewport[4];

GLdouble modelview[16];

GLdouble projection[16];

GLfloat winX, winY, winZ;

GLdouble posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );

glGetDoublev( GL_PROJECTION_MATRIX, projection );

glGetIntegerv( GL_VIEWPORT, viewport );

cout << mouse_x << " " << mouse_y << '\n';

winX = (float)mouse_x;

winY = (float)viewport[3] - (float)mouse_y;

glReadPixels( mouse_x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

cout << winZ << '\n';

cout << posX << " " << posY << " " << posZ << '\n';

This is giving me some odd results. The final values are something like -9.25596e+061 for every value. Since this is a just a 2d application I tried using winZ to be zero (it is actually evaluating to 0 anyway when I use glReadPixels) so i'm not sure if this is causing any problems.