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: http://www.opengl.org/resources/faq/technical/selection.htm 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: Code: ```// 768x512 window not resizeable to make this simple NSPoint point; - (void)mouseDown:(NSEvent *)event {    point = [event locationInWindow]; } - (NSPoint)convertMousePoint:(NSPoint)mouse {    NSPoint m;    m.x = mouse.x * (2.0 / 768.0) - 1.0;    m.y = mouse.y * (2.0 / 512.0) - 1.0;    return (m);  // returns the converted point } NSPoint mouseGLCoords; mouseGLCoords = [self convertMousePoint:point];``` This does work for me.