Translate mouse coordinates to world coordinates - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Translate mouse coordinates to world coordinates (/thread-4442.html)
Translate mouse coordinates to world coordinates - nalenb - Mar 14, 2006 09:55 PM
After searching for a long time I still cannot find a way to convert from the view's mouse coordinates to the opengl world coordinates. Here's what I have:
A windowed opengl view that's about 400 x 300
I draw a bunch of quads all at the same z location but different x & y locations
I want to be able to tell which quad I clicked on, I know each quads location and dimension in world coordinates, e.g. quad 1 is at -2, -2 and is 0.8 by 0.8 big.
I do glTranslatef(0.0, 0.0, -5.0f); to back off the camera from where I've drawn.
In mouseDown, get the window coordinates from the event locationInWindow
Then I grab the viewport, modelview matrix and projection matrix and call gluUnProject
The values I get back from that don't seem to make any sense. I'm guessing it's because I'm sending in 0.0 to gluUnProject for the local z coordinate which is the near plane, so I'm getting back the coordinates at the near plane.
So, am I supposed to figure out what z position to put in since I know the projection near & far planes and what z position my objects are at?
Or am I supposed to cast a ray at see what it intersects with?
Any pointers would be appreciated.
Translate mouse coordinates to world coordinates - PowerMacX - Mar 14, 2006 11:44 PM
Have you looked at glOrtho?
Translate mouse coordinates to world coordinates - OneSadCookie - Mar 15, 2006 12:56 AM
If you need the perspective view, call gluUnProject twice, passing 0.0 for z the first time and 1.0 for z the second. That gives you two points on the line in 3D space that would be transformed to the pixel that was clicked. You can then programmatically intersect that line with your geometry to find out what was hit.
Translate mouse coordinates to world coordinates - nalenb - Mar 15, 2006 06:24 AM
I was misunderstanding the use of gluUnProject. I found this page:
which details just what OSC said. Thanks for the info.
Translate mouse coordinates to world coordinates - glMatt - Mar 21, 2006 11:59 PM
My own solution for mouse coordinate conversion:
This does work for me.