# find pixel from ray

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

## Recommended Posts

Hi all, I'm wondering if someone knows how to figure out a which pixel from the screen normalized vector starting from the eye location intersects, given the standard view information (eye, look at, up, near clipping plane, fovy, etc.). It's basically the reverse of what you do in ray tracing when you generate a ray given a pixel on the screen. I have notes on how to do that but it doesn't quite work in reverse because you basically find the point on the clipping plane and then subtract the eye from it and normalize. However the normalization loses data and I'm being given normalized vectors in the first place. I hope this makes sense...Thanks for any help

##### Share on other sites
Off the top of my head, here's something that might work (although there are very likely better solutions):

Find the four corner vectors that point to the corners of the view frame (the section of the view plane that is rendered to screen). Project these vectors to find their intersection with the view plane in world-space coordinates. Then project the test vector onto the same plane as well. Then you can reverse interpolate the relative position of the test point on the view frame (from 0 to 1 on the two planar axes) and then multiply that by the frame resolution to obtain the final pixel coordinates.

##### Share on other sites
Alternatively if you have the full world space to view space transformation matrix you can invert it and multiply the vector through that (you'll need a zero in the W coordinate since this is a direction not a position). Then divide through the resulting X and Y coordinates by the W coordinate, which will give you a 2D position with each axis ranging from -1 to +1 in across the screen, which is easy to turn into a pixel coordinate. The Z component that you get in the resulting vector is unimportant.

##### Share on other sites
instead of the inverse, you can just take the transpose. the scaling factor as well as the translation part of the matrix doesn't bother as you do the scaling afterwards..

or possibly i'm wrong with this:D bah :D i'm not good at matrix math.. if you have up,right and front vector, you can simply project the direction onto these, to get the backtransformed vector, and divide it by the front-component (z..).

3 dots and a 2 divisions should do the trick. then transforming from -1..1 to 0..width|height is the last step. two mads, that are..

##### Share on other sites
Quote:
 Original post by davepermen3 dots and a 2 divisions should do the trick. then transforming from -1..1 to 0..width|height is the last step. two mads, that are..

I'm guessing our methods have something in common then because mine's also 3 dots and two divisions once you've inverted the matrix (the important thing of course is you only do this one per orientation). I'm not sure about taking the transpose - works for rotation matrices but not for scaling/transform. If the bottom row of the matrix is (0 0 0 1) that would mean you can take just the 3x3 part which you could then take the transpose of if that's orthogonal but I don't think it works if scaling is applied (which it usually is since screen's aren't square). All that having been said it's probably a lot simpler (well, faster) to generate the inverse from parameters then it is to run the inverse algorithm.

Alternatively: Take the cross product of the look forward and look up vector. This gives you the look right vector. Dot the direction vector with each of these. Divide the results from the right and up vectors by the one from the forward vector. These give you your X and Y coordinates respectively. This is effectively equivalent to the method above because what the inverse does is extract said vector from the matrix, more-or-less. Anyway this will require some scaling, the factors involved depends on where your near clipping plane is and the X and Y FOV.

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 9
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013662
×