PDA

View Full Version : converting window coordinates to world coordinates

pwonder
09-25-2004, 09:18 AM
Hello,

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?

thx!

pwonder
09-25-2004, 09:33 AM
... I forgot to add, this is in 2D. And was wondering if there is a way to do this without using gluUnProject

M/\dm/\n
09-25-2004, 09:39 AM
Mouse coords are returned in screen coords. If you know window position everything is simple.

pwonder
09-25-2004, 09:43 AM
Yeah i know the screen coordinates.. i am trying to figure out a math function to convert them to the world coordinates.... but not having much luck.

like if my window is (800,600) at the bottom right corner this maps to (1,0) in world coordinates.. right?

so 'x' would be divided by 800.
for 'y' i am not sure.

M/\dm/\n
09-25-2004, 10:52 AM
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?

09-25-2004, 07:48 PM
there is a way to do this without UnProject: use the same method that UnProject uses in your own code ;)

http://oss.sgi.com/cgi-bin/cvsweb.cgi/projects/ogl-sample/main/gfx/lib/glu/libutil/project.c

it's just a bunch of matrix algebra ... maybe you can make it faster :)

09-26-2004, 06:49 AM
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.

pwonder
09-26-2004, 11:51 AM
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, &amp;worldX, &amp;worldY, &amp;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!