1
0
mirror of https://github.com/ssloy/tinyraytracer.git synced 2025-01-17 06:08:14 +01:00

Updated Home (markdown)

Dmitry V. Sokolov 2019-01-22 13:15:03 +01:00
parent ea31efe4a9
commit dec00ef002

@ -137,11 +137,11 @@ Let me illustrate how we compute the initial direction of the ray to trace. In t
float x = (2*(i + 0.5)/(float)width - 1)*tan(fov/2.)*width/(float)height;
float y = -(2*(j + 0.5)/(float)height - 1)*tan(fov/2.);
```
Where it comes from? Pretty simple. Our camera is placed in the origin and it faces the -z direction. Let me illustrate the stuff, this images shows the camera from the top, the y axis points out of the screen:
Where it comes from? Pretty simple. Our camera is placed in the origin and it faces the -z direction. Let me illustrate the stuff, this image shows the camera from the top, the y axis points out of the screen:
<img src="https://raw.githubusercontent.com/ssloy/tinyraytracer/master/trace.png"/>
As i said, the camera is placed at the origin, and the scene is projected at the screen that lies the plane z = -1. The field of view specifies what sector of the space will be visible at the screen. In our image the screen is 16 pixels wide; can you compute its length in world coordinates? It is pretty simple: let us focus on the triangle formed by the red, gray and gray dashed line. It is easy to see that tan(field of view / 2) = (screen width)*0.5/(screen-camera distance). We placed the screen at the distance of 1 from the camera, thus (screen width) = tan(field of view / 2).
As i said, the camera is placed at the origin, and the scene is projected at the screen that lies the plane z = -1. The field of view specifies what sector of the space will be visible at the screen. In our image the screen is 16 pixels wide; can you compute its length in world coordinates? It is pretty simple: let us focus on the triangle formed by the red, gray and gray dashed line. It is easy to see that tan(field of view / 2) = (screen width)*0.5/(screen-camera distance). We placed the screen at the distance of 1 from the camera, thus (screen width) = 2*tan(field of view / 2).
Now let us say that we want to cast a vector through the center of the 12th pixel of the screen, i.e. we want to compute the blue vector. How can we do that? What is the distance from the left of the screen to the tip of the blue vector? First of all, it is 12+0.5 pixels. We know that 16 pixels of the screen correspond to 2*tan(fov/2) world units. Thus, tip of the vector is located at (12+0.5)/16 * 2*tan(fov/2) world units from the left edge, or at the distance of (12+0.5)*2/16 * tan(fov/2) - tan(fov/2) from the intersection between the screen and the -z axis. Add the screen aspect ratio to the computations and you will find exactly the formulas for the ray direction.