Build your own 3D renderer - Casting rays to render shadows

Unil now, two objects have not interacted with each other. However, one piece of geometry might block light from reaching another, resulting in a shadow.

A ray is cast from a point on an object to a light source. On the left, the ray intersects another object before hitting the light source. On the right, the ray does not hit another object before hitting the light source.

When the ray between a point on an object and a light source is intersected by another object, the point is in shadow. Otherwise, the point is not in shadow from that light source, though it may be in shadow from another light source.

Suppose we are trying to find the color at a certain point on an object. We will do the following:

  1. Cast a ray from that point to each light source.

  2. For each ray, we will perform an intersection test with each other piece of geometry in the scene.

  3. If the ray intersects with any geometry before reaching the light source (intersecting at `0 < t <= 1`), the point is in shadow from that light source.

If the point is in shadow from a particular light source, we will ignore the diffuse and specular contributions from that light source when computing the Phong illumination.

We will continue including the contributions from any lights that are not blocked by other objects. We will also always include the ambient term. This term approximates indirect lighting, ensuring shadowed areas are not completely black.