Translate mouse coordinates to world coordinates
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.
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.
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.
I was misunderstanding the use of gluUnProject. I found this page:
http://www.opengl.org/resources/faq/tech...ection.htm
which details just what OSC said. Thanks for the info.
http://www.opengl.org/resources/faq/tech...ection.htm
which details just what OSC said. Thanks for the info.
My own solution for mouse coordinate conversion:
This does work for me.
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.
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Translate Like chaiwise  12passion  0  3,352 
Dec 11, 2012 12:27 AM Last Post: 12passion 

Updating Map Annotations  coordinates  PHANTOMIAS  0  3,417 
Dec 8, 2009 08:39 AM Last Post: PHANTOMIAS 

Local (X, Y) Coordinates from 3D plane coordinates  merrill541  5  10,370 
Jun 29, 2009 01:32 AM Last Post: RhinosoRoss 

calculating X and Y coordinates w/ an angle and distance  ferum  13  19,849 
Jun 25, 2008 10:53 PM Last Post: rosenth 

Find 3D Direction with 2D Coordinates  suan832001  1  3,451 
Aug 24, 2007 07:03 AM Last Post: backslash 