When the mouse clicks the opengl generated window this passes the x,y coordinates of the screen to glutMouseFunc()… anyone know how to convert these into the coordinates of the opengl window, say to draw something where the user has clicked?
Say you have a window with grid on your desktop & you want to know which cell you clicked?
1)get the window coords from desktop coords
2)get the world coords from window coords
For the first substitute upper left corner from your mouse pos and check wheter the coords are not bigger than wsize.
For the second depends on app, but I guess it’s some sort of ortho, isn’t it?
Hey com’on guys, this is a beginners group - to many the use of gluunproject is complex - the poster has asked a simple question that I have struggled with too - if you want to help, post a very simple example - otherwise don’t post.
pwonder - I think gluunproject is the way you want to go - try it out with a simple case using some printf statements to see if you are getting the right answer. I’m working on it too and if I meet success will post back.
I got it to work… here is what my function that handles mouse click looks like:
Thanks for all of your help
void mouseClick( int button, int buttonState, int x, int y ){
//if any mouse button was pressed
if (buttonState == GLUT_DOWN) {
GLint viewport[4]; //var to hold the viewport info
GLdouble modelview[16]; //var to hold the modelview info
GLdouble projection[16]; //var to hold the projection matrix info
GLfloat winX, winY, winZ; //variables to hold screen x,y,z coordinates
GLdouble worldX, worldY, worldZ; //variables to hold world x,y,z coordinates
glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get the projection matrix info
glGetIntegerv( GL_VIEWPORT, viewport ); //get the viewport info
winX = (float)x;
winY = (float)viewport[3] - (float)y;
winZ = 0;
//get the world coordinates from the screen coordinates
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ);
btw: i use winZ=0; since i am only doing 2d stuff.
and fyi. at this point in the program worldX, worldY have the WORLD x,y coordinates!