Advertisement

coord conv

Started by March 01, 2002 10:57 AM
1 comment, last by rgbRasta 22 years, 11 months ago
How do I convert coordinates on the screen to coordinates in 3d space? I am working on a 3d editor and need to know what point just got clicked. btw: merry x-mas red green blue
redgreenblue
There are two problems here. First, you''ve clicked a 2D screen point, but in 3D space what you clicked is an infinite line. You have to find out which object, closest to the camera, is under the cursor along that line. This is a "picking" or "selection" problem. The second is how to transform world space into screen space---this is the question you seem to have asked. Here''s a summary of how to do it:

1) Pick a point, and find the pixel location (xp, yp).
2) Normalize (xp, yp) to screen space coordinates (xs, xs) by just dividing by the number of pixels on screen. The idea is that you want the lower-left corner to be (xs = 0, ys = 0), and the upper-right corner to be (xs = 1, ys = 1).
3) walk through your objects, triangle by triangle.
4) For each triangle, map the vertices from world space into screen space to produce a screen space triangle. The vertices Z coordinate for screen space triangles are basically Z-buffer depth.
5) For each screen space triangle, see if the picked point (xs, ys) lies within the interior of the screen space triangle. If so, interpolate Z-buffer depth within the triangle at (xs, ys). if this Z-buffer depth is less than the prior selected object, then set the selected object to whatever object you''re on at this time.
6) repeat steps 3-5 until done. You should now know which object you selected, and the Z-buffer depth of the selected triangle point. Call this the screen space picked point (xs, ys, zs).
7) map (xs, ys, zs) back into world space to know which 3D point you picked.

Well this subject is covered in a number of places already. The step I haven''t mentioned, moving between world and screen space, is a function of model/view and viewport transformations. The OpenGL red book, for example, covers this in detail. As does "Computer Graphics: Principles and Practice" by Foley et al. and countless other books and web sites.

For your convenience, here''s a couple of links. They''re not great, but they are better than some. Suggest you visit your local library to borrow a book such as the Foley book, which will have more detail and better discussion:

http://astronomy.swin.edu.au/~pbourke/projection/transform/

and on this site,

http://www.gamedev.net/reference/articles/article673.asp

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Advertisement
Compare your pointers coordinates to the view coordinates of the rendered points. You can only click through visible points.

This topic is closed to new replies.

Advertisement