View Full Version : Projecting 2d coordinates into 3d
LongJumper
2005.04.13, 03:55 PM
I need to be able to click somewhere on a window, get those 2d coordinates, send a "ray" directly "into" that spot and find where that ray hits something I've drawn in 3D. I'm guessing this is a pretty common thing to do, but I don't know how to do it.
ThemsAllTook
2005.04.13, 05:03 PM
Take a look at gluProject and gluUnProject. Those should get you started.
- Alex Diener
FCCovett
2005.04.13, 06:01 PM
http://www.lighthouse3d.com/opengl/picking/
Puzzler183
2005.04.13, 06:56 PM
You need picking and the selection buffer... I'm pretty sure that other than the lighthouse tutorial, http://nehe.gamedev.net/ has a good one on it.
LongJumper
2005.04.14, 12:42 AM
I'm not really grasping it this well, or in better words, it doesn't work.
So basically this is the order of events that take place:
Going along with no mouse clicks, go to the drawing function where we:
1. Set the agl context, update the context, and clear the depth and color bits.
2. Set up the camera, which includes...
2a. Putting a clean matrix on the projection stack and calling gluPerspective.
2b. Putting a clean matrix on the modelview stack
2c. Calling gluLookAt.
3. Doing all my drawing
4. Swapping buffers
Now say we get a mouse click, we do the following:
1. Set the agl context, update, clear like before.
2. Then instead of setting up the camera like before, we...
2a. Select the buffer and set the render mode to GL_SELECT.
2b. Push the current(the normal drawing) projectin matrix down and put a clean one on top.
2c. Call gluPickMatrix with the coordinates of the mouse(y inverted) with a relatively small "pick" region and the viewport of the new projection matrix(which is just looking down the z plane, right?)
2d. Then call gluPerspective with the drawing cameras perspective.
2e. Go to the modelview matrix and call glInitNames.
2f. Then do the gluLookAt with the drawing camera
3. Do the all the same drawing, but of course with glPush/PopName calls for each time you draw something pickable.
4. Then pop the projection matrix(the one with the small viewport for picking) off so we're back to the original projection matrix, flush the modelview matrix and return to render mode.
5. Then process the hits.
However, I'm not getting any hits regardless of where I click. Where am I going wrong?
LongJumper
2005.04.15, 10:27 PM
I'm wondering how the set up of my camera relates to the object picking. I'm definetly not getting the right hits right now. Does anyone have any sample code using gluLookAt in their camera's setup and utilizing gluPickMatrix?
edit: I'm still toying with it. I turned on render mode with the pick matrix, and it's definetly displaying the wrong area. I queried the modelview and viewport matrices when picking and when rendering, and they equal in both cases. So why is it finding the wrong spot?
double edit: Nevermind, had to accomodate for the fact that it wasn't full screen, so needed to add in the window's location.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.