PDA

View Full Version : Coordinates in 3D on Mouse clicked Object



Tscho
07-30-2009, 03:17 AM
Hi everybody,
my problem might be trivial but i haven't found any Information so far.
I'm writing an application and need to know the 3D-coordinates of the clicked vertex when i click on a object.Is there a function that returns those values, or has anyone here an example on how to implement this ? I'm using Tao framework in C#.

Thanx,
Tscho

plasmonster
07-30-2009, 02:56 PM
Normally I'd suggest the OpenGL selection mechanism, for its simplicity. But that's fallen out of favor since its deprecation. :(

Tracing a ray is easy enough. In a nutshell what you want to do is minimize 2 quantities:
1. the orthogonal distance to the ray from the vertex (vertices closest to mouse in XY) and
2. the distance from the ray origin to the vertex along the ray (vertices closest to mouse in Z)

Let V be the vertex position under consideration, O the ray origin and the D the normalized ray direction.

The orthogonal distance for (1) is then

dist_xy = length(V - (O + dot(V - O, D) * D)),

and the z distance for (2) is

dist_z = dot(V - O, D).

So for each vertex considered, keep the one with the smallest xy and z distances. There are lots of ways to optimize this and plenty of code out there, but that's the basic idea. I would recommend any good book on computation geometry and ray-tracing for a deeper look. The oft referenced Real-Time Collision Detection does a great job distilling a lot of the more useful algorithms and offers some great tips on how to avoid common floating point pitfalls and such.

Hope it helps.

dletozeun
07-30-2009, 03:52 PM
Another solution that does not require ray casting is after rasterizing the scene, you can get the pixel depth from the depth buffer with glReadPixels. Then knowing the depth value and the mouse coordinates you can unproject these coordinates to obtain world coordinates.

gluUnproject may help you to unproject screen coordinates.

Tscho
08-03-2009, 09:08 PM
Thanks everybody, i'm trying the glreadPixel-Approach. Now if i could only figure out how to proper read the pixels-Data. So far a lot of 0's...