picking with raytracing

Started by
5 comments, last by daniel_i_l 18 years ago
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.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
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
Use gluUnproject to get the front and back points of a line you can test against.
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.
Delphi C++ OpenGL Development: www.nitrogen.za.org
If I call glUnProject twice, will P1 - P2 be the direction of the ray?
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
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.
Thanks for all the help!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky

This topic is closed to new replies.

Advertisement