mirror of
https://github.com/ssloy/tinyraytracer.git
synced 2025-09-02 10:32:39 +02:00
Updated Home (markdown)
14
Home.md
14
Home.md
@@ -131,6 +131,20 @@ At this point, I recommend you to take a pencil and check on paper all the calcu
|
||||
<li>view direction, along the z-axis, in the direction of minus infinity</li>
|
||||
</ul>
|
||||
|
||||
Let me illustrate how we compute the initial direction of the ray to trace. In the main loop we have this formula:
|
||||
|
||||
```c++
|
||||
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:
|
||||
|
||||
<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).
|
||||
|
||||
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.
|
||||
|
||||
<h1>Step 3: add more spheres</h1>
|
||||
The hardest part is over, and now our path is clear. If we know how to draw one sphere, it will not take us long to add few more. <a href="https://github.com/ssloy/tinyraytracer/commit/c19c430151cb659372b4988876173b022164e371">Check the changes</a> in the code, and this is the resulting image:
|
||||
|
||||
|
Reference in New Issue
Block a user