picking with raytracing
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.
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
I know how to do the raytracing part, but not how to do the first.
Hope this helps
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.
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.
Quote:Original post by daniel_i_lClose - assuming P1 is evaluated at the near plane and P2 at the far plane, it will be P2 - P1.
If I call glUnProject twice, will P1 - P2 be the direction of the ray?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement