1
votes

The specific name of what I am trying to do is "unproject" = to map a screen point to the 3D world.

So I found this site which has simple equations for mapping 3D coordinates to a 2D screen:

http://www.petesqbsite.com/sections/tutorials/tuts/perspective.html

I've used these equations extensively, but now i want to be able to interact with the 3D world by hovering over the screen.

Rather than having the 2D coordinates be linked to the 3D objects I wanted to find an equation which maps onscreen points to the real. I tried reverse engineering the following 2 equations to try and find the answer:

screenPoint.x = (int) ((eye.z * (realPoint.x-eye.x)) / (eye.z + realPoint.z) + eye.x);
screenPoint.y = 480 - (int) ((eye.z * (realPoint.y-eye.y)) / (eye.z + realPoint.z) + eye.y);

However I was not successful (not mathematically possible).

Any help would be greatly appreciated!

Thanks!

1
This is not possible in the general case (a projection is not invertible). But in simple cases, you could use ray tracing to determine what object projects onto a given pixel. Or you could pre-generate the info when you generate the 2D image.Oliver Charlesworth
So you are saying that with ray tracing I can, given: -camera coordinates -equation of the screen plane -equation of some flat (xz) plane in the 3D world map points from the screen to the plane in the 3D world? I am just trying to have points on the xz plane light up when I hover over them.user1170679
I found the answer to my question: stackoverflow.com/questions/9971701/…user1170679

1 Answers

0
votes

I once had a similar problem. What I did was I found out the field of view that was being used in opengl and worked out the Z distance at which 1 pixel on the screen = 1 unit in opengl. Now Whenever I point anywhere on the screen and I have the X and Y coordinates of the mouse onscreen and the Z coordinate I calculated earlier based on the FOV. I now have a definite 3d coordinate. I then "drew" an imaginary line from the camera to that point and see what it hit. I know this is not a straightforward answer, but what you're asking is not possible. Here's a way to do it with some trickery