Sign in to follow this  
daniel_i_l

picking with raytracing

Recommended Posts

I'm trying to do picking in 3D and read good things about raytracing. Could someone explain how raytracing is used for picking, and most importantly, how do I get the origin and the direction of the ray with the camera coords(position&view) and the windows mouse coords (0,0,480,620)? Thanks.

Share this post


Link to post
Share on other sites
I'm not an expert about matrix-based projection transformations, but you need to find where the 2d picked point lies on the 3d view plane. Then you create a (normalized) vector from this point and the eyepoint (3dpoint - eyepoint) and calulate the intersection between the ray (origin=eyepoint, direction=computed vector) and the geometry. The nearest intersection belongs to the object picked.
I know how to do the raytracing part, but not how to do the first.
Hope this helps

Share this post


Link to post
Share on other sites
Yes, I use glUnproject and a ray intersection test to do my mouse picking.

glUnproject will need:
- Your GL_MODEL_VIEW_MATRIX
- Your GL_PROJECTION_MATRIX
- Your viewport coordinates as a Vector4D (x,y,wid,hgt)
Then obviously the x, y coordinates on your window.

Then it also takes a z value (from 0 to 1) for how far into the scene to project your mouse coordinates, now if you set it to 0, the point returned will always be at the center of the camera, because it's getting projected into the scene from a single point (the end of your camera frustum)

So you call glUnproject twice:
To get the origin of your ray, you set Z to 0.
To get the other point you just select any old value.

Share this post


Link to post
Share on other sites
Quote:
Original post by daniel_i_l
If I call glUnProject twice, will P1 - P2 be the direction of the ray?
Close - assuming P1 is evaluated at the near plane and P2 at the far plane, it will be P2 - P1.

You might check your other thread on this topic as well. I posted some additional info there, including a link to a site with an example of how to use gluUnproject() to construct the pick ray.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this