Search code examples
c++graphics3drenderingpixel

From pixel to world coordinate formula explanation


I have the following (relatively easy) problem. I am developing a ray tracer and I am following the tutorial explained in this link:

http://www.scratchapixel.com/code.php?id=3&origin=/lessons/3d-basic-rendering/introduction-to-ray-tracing

There is a formula I don't get though, which is used to map the pixel (i,j) to world coordinates. The formula is the following:

float fov = 30, aspectratio = width / height; 
float angle = tan(M_PI * 0.5 * fov / 180.);
float xx = (2 * ((x + 0.5) * invWidth) - 1) * angle * aspectratio; 
float yy = (1 - 2 * ((y + 0.5) * invHeight)) * angle;

In this tutorial the camera is placed at (0,0,0) and up/right/lookAt vector are not used at all. It seems that in every tutorial a different formula is used to map a pixel and I don't manage to get the reason. Moreover, what about if my camera is not placed at (0,0,0) but in another position that I can decide? How would the formula vary? May you kindly help me? Thanks!


Solution

  • http://scratchapixel.com/lessons/3d-basic-rendering/3d-viewing-pinhole-camera

    If you had made some more research on this website you would have found the answer. The whole point of this website is to explain that sort of technique. Just please make an effort before expecting anyone on SO to answer for you. Your question should be heavily down voted. This is pure lazyness.