Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > OpenGL Coordinates

OpenGL Coordinates
Thread Tools
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
May 25, 2003, 11:25 AM
 
I'm writing a program that uses OpenGL for graphics, and I need the user to be able to click on objects on the screen. I am using an NSOpenGLView, so I will need to convert the view coordinates to OpenGL coordinates. Does anyone know of an easy way of doing this? (In theory I could calculate the inverse of the projection matrix, then multiply by the view coordinates, but I'm looking for an easier way)
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
May 25, 2003, 01:59 PM
 
check out "picking":

http://www.google.com/search?hl=en&i...opengl+picking

that's the standard way to select stuff in OpenGL.
     
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
May 25, 2003, 03:28 PM
 
Thanks, that helps a bunch. However, I want to make it so that if the user does not click on an object, it creates a new one where they click. Any more suggestions?
(Last edited by Catfish_Man; May 26, 2003 at 12:49 PM. )
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
May 28, 2003, 02:09 PM
 
easy, just read some of those tutorials. if you do the picking and your routine doesn't return any objects, you know you didn't click on anything.
     
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
May 30, 2003, 02:45 PM
 
OK, I read some of the tutorials, but I still can't figure out how to get the coordinates of a mouseclick that didn't hit any objects (in my program, that would be empty space, and I'd want to create an object where they clicked). I can see how to tell if they didn't hit anything, just not how to tell where they didn't hit anything. Is there a good way of doing this?
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
May 30, 2003, 09:52 PM
 
what you're thinking of doing is not possible. think about it -- trying to map a 2d point (on your screen) to a 3d point (opengl coords)... there are an infinite number of possibilities. if you want to figure out what those possibilities are, compute a vector from your camera taking into consideration the current focal angle and such. pick a point along that line at an arbirary distance -- that is "technically" a point that the user clicked.

in other words, you need another constraint. you have an equation with one too many variables to solve here...
     
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
May 30, 2003, 11:45 PM
 
Originally posted by 00101001:
what you're thinking of doing is not possible. think about it -- trying to map a 2d point (on your screen) to a 3d point (opengl coords)... there are an infinite number of possibilities. if you want to figure out what those possibilities are, compute a vector from your camera taking into consideration the current focal angle and such. pick a point along that line at an arbirary distance -- that is "technically" a point that the user clicked.

in other words, you need another constraint. you have an equation with one too many variables to solve here...
Right, I'm constraining this to a 2d plane (it's really a 2d program, I'm using OpenGL to make it easy to zoom in and out, as well as a few other things), so I'm really trying to find a line through that plane representing the point on the plane they clicked. Sorry I forgot to mention that.
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
May 31, 2003, 08:54 PM
 
ah gotcha, it's simple then... actually a program that i'm currently working on does exactly that ( http://lorenb.com/software/sproing/ )

anyway, this is what I do:
after I grab my mouseDown: NSEvent* in my NSView subclass, i convert the point to my view-space:

p = [[self superview] convertPoint:[event locationInWindow] toView:self];

now, in my application you can both pan and zoom the view. so i have 2 variables viewTranslate (NSPoint) and viewZoom (float).

now, if I want to convert the point to my view space, i can use the formula:

newp = NSMakePoint((p.x - viewTranslate.x) / viewZoom, (p.y - viewTranslate.y) / viewZoom);

newp is the point where I clicked in opengl coords. i can then cycle through my objects to check for selection or make something new at that spot.

let me know if this helps
     
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
May 31, 2003, 11:08 PM
 
If I understand correctly, you're basically starting out with a 1 to 1 relationship between the OpenGL coordinates and the view cooridinates (so zoomed and translated 0), then tracking any changes you make with viewTranslate and viewZoom. Clever. I'll see if I can persuade the person I'm working with to do that.

btw, your Sproing program is extremely spiffy
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Jun 1, 2003, 09:36 PM
 
yep, you understand perfectly. just make sure that when you draw, you set up your projection matrix with glOrtho(pixel_size_of_view); or else you're going to get unexpected results.

re: sproing, thanks ...still got a lot of work till it's done though.
     
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Jun 4, 2003, 07:56 PM
 
I found something interesting on the MS dev page: the gluUnProject function. It appears to take window coordinates and map them into GL coordinates. It takes 3 window variables, x y and z because of the problem you mentioned earlier, but it seems like it would be possible to draw the plane you wanted to click on, then find the depth corresponding to the point clicked in the depth buffer. Do you think that would work?
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Jun 4, 2003, 08:24 PM
 
yeah, it would probably work, never tried it_-- I'm happy with the way I'm doing it now Give it a shot, let us know how it goes. Matrix maniplations are slow btw, you probably should keep them to a minimum -- but for something like this you don't need to worry about speed differences.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:35 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2