Concepts of Ray Tracing

Ray Tracing



Sun and Lights

By Daniel K., Age 14

In real life, the sun and lights emit rays of light that travel in (kind of) a straight line. When these rays of light hit an object, some of them are absorbed, some of them pass through (if the object is clear), and some of them are reflected off. The rays of light continue bouncing around until they reach your eye (or a camera).

Ray tracing is supposed to simulate these rays of light and their complex interactions and is used to render realistic CGI scenes, usually in movies and more recently in video games. However, raytracing is incredibly hard to do. You have to simulate trillions of rays of light bouncing around the environment. In movie studios, raytracing one frame of CGI can take up to a day.

However, we can do some optimization. Since the rays of light that don’t reach the camera don’t affect the picture, we can simply not simulate those rays. But how do we know that the rays of light don’t reach the camera without simulating them? Simple. Assuming light travels the same way when reversed (aka. Camera to light source), we can simulate the rays of light bouncing from the camera to a light source. Since the camera is now emitting the rays of light rather than the light sources, it means some rays of light never reach the light sources. Although this method still simulates extra unneeded rays, this way of simulating light is still far superior to the other method (light source → camera).


“Well then, what is the math involved?”, you may ask. A lot of very complex math goes on when calculating the path of a ray of light, but I’ll give you the basic rundown:

  1. A ray is cast from a point P, or the position of the camera.
  2. The vector of the ray is determined by combining the pixel the ray represents on the screen, the field of view and the angle of a camera.
  3. The vector is then multiplied by the camera’s view distance (longer means more accurate but takes longer to compute.)
  4. Uses an equation to calculate whether the ray intersects with a triangle using this equation.
  5. Repeat for all visible triangles (more efficient visibility calculation means less triangles to calculate intersections for)
  6. If an intersection is not found, either discard the ray or act as if the ray has hit an emissive material, set its color to a sky color, and skip to step 10.
  7. If an intersection is found, calculate the triangle’s surface normal (optionally use an algorithm to calculate smooth normal for smooth materials)
  8. According to the triangle’s material, refract the ray through the material or reflect it off of the material, applying the material’s color multiplicatively.
  9. If the material is not emissive (does not give off light), calculate the ray’s reflection angle and repeat steps 3-10. (limiting # of bounces to prevent infinite looping)
  10. If the material is emissive, mix the light ray color with the corresponding pixel’s color based on how many rays of light have already been calculated for that pixel.
  11. Do that (8294400 × amount of rays calculated per pixel) more times and voila! You have just raytraced a single 4k image!