View Full Version : Glut Mouse Coordinates

Erin C.
11-06-2008, 01:57 PM
Is glutMouseFunc using a different coordinate system than my openGL?

For example, I have this texture drawn:

glTexCoord2f(0,1); glVertex3f(0,-200,0);
glTexCoord2f(1,1); glVertex3f(100,-200,0);
glTexCoord2f(1,0); glVertex3f(100,-250,0);
glTexCoord2f(0,0); glVertex3f(0,-250,0);

But when I'm trying to click on that texture w/ this function....

void mouse(int button, int state, int x, int y)
if (button==GLUT_LEFT && state==GLUT_DOWN)
if (x>0 &amp;&amp; x<100 &amp;&amp; y>-250 &amp;&amp; y<-250)
cout << "inside\n";
cout << "not inside\n";
cout << "x: " << x << "\n";
cout << "y: " << y << "\n";

My output is saying "not inside" with x=223 & y=314.

11-06-2008, 02:08 PM
In "window" coordinate, the origin (0,0) is top left of the viewport.In OpenGL the origin is bottom left of the viewport. When you click glut give you the window coordinate. All you have to do is calculate this: y = height_of_viewport - y - 1.

Edit: Notice that you compare a screen coordinate (mouse click) with an object coordinate (your rectangle). This is fine if you use a perspective projection like this glOrtho(0,0,viewport_width,viewport_height). If not you need to call gluProject (http://www.opengl.org/sdk/docs/man/xhtml/gluProject.xml) to map each corner of your rectangle in screen coordinate.

Erin C.
11-06-2008, 09:42 PM
I can't find any good resources (tutorials, examples) etc. w/ gluProject. Does anyone know of any?

11-07-2008, 07:04 AM
What do you mean good resources? The manual page that TNT posted a link to explains everything very well.