Original Post
🔒 Locked
2D mouse mapping
Advertisement
printf("Mouse x: %i, y: %i\n",x,y); GLint viewport[4]; GLdouble mvmatrix[16], projmatrix[16]; GLint realy; GLdouble wx,wy,wz; glGetIntegerv(GL_VIEWPORT,viewport); glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix); glGetDoublev(GL_PROJECTION_MATRIX,projmatrix); realy = viewport[3] - (GLint) y - 1; gluUnProject((GLdouble)x, (GLdouble) realy, 0.0, mvmatrix,projmatrix,viewport,&wx,&wy,&wz); printf("World x: %f, y: %f\n",wx,wy);I used this code from the Red Book and it return reasonable results for 3D scenes. But for my 2D elements hte world x,y values are "strange" (x ~= 0-1, y ~= 4-5).
Here is also the code after which 2D elements are drawn:
glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D (0, 800, 0, 600); //gluOrtho2D (0, 800, 0, 600); glMatrixMode(GL_MODELVIEW); // Draw 2D elements here
I answered the question of mouse coordinate conversion quite recently in this thread. See if that helps any. You have the general idea though. The oddity might be the orientation of your viewport not being taken into consideration in the conversion calcs - (0,0) being lower left or top left.
F451
[Edited by - Fahrenheit451 on April 12, 2006 4:46:46 PM]
F451
[Edited by - Fahrenheit451 on April 12, 2006 4:46:46 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement