# picking with raytracing

This topic is 4556 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
Use gluUnproject to get the front and back points of a line you can test against.

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

glUnproject will need:
- 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 on other sites
If I call glUnProject twice, will P1 - P2 be the direction of the ray?
Thanks.

##### Share on other sites
Quote:
 Original post by daniel_i_lIf 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 on other sites
Thanks for all the help!

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12

• 9
• 21
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632606
• Total Posts
3007383

×