Metal Surfaces and Reflection

Adding Shiny Surfaces to Your Ray Tracer

In the previous three lessons, you built a ray tracer capable of rendering smooth, antialiased images of matte surfaces with realistic lighting. You implemented Lambertian diffuse materials that scatter light randomly in all directions, creating the soft, non-shiny appearance of materials like chalk, unfinished wood, or matte paint. You also fixed two important visual artifacts: shadow acne caused by floating-point precision errors and incorrect brightness perception through gamma correction. Your ray tracer now produces clean, properly lit images with natural-looking diffuse surfaces.

However, if you look around the real world, you'll notice that not everything is matte. Many materials have shiny, reflective surfaces that behave very differently from the diffuse materials we've implemented so far. Think about a polished chrome bumper on a car, a stainless steel refrigerator, a silver spoon, or a brass doorknob. These metallic surfaces don't scatter light randomly like matte materials do. Instead, they reflect light in a mirror-like way, bouncing incoming rays in a specific direction determined by the surface angle. When you look at a polished metal surface, you can often see reflections of other objects in the scene, and the surface itself appears bright and shiny rather than soft and matte.

In this lesson, you'll learn how to add metallic materials to your ray tracer by implementing reflection. You'll understand the mathematics behind reflection vectors and how they differ from the random scattering we used for diffuse materials. We'll start by implementing perfect mirror-like reflection, which simulates highly polished metals like chrome or polished silver. Then we'll extend this to handle fuzzy or rough reflection, which models metals with slightly imperfect surfaces like brushed aluminum or worn steel. By the end of this lesson, you'll be able to render scenes containing both matte and shiny surfaces, with metals that reflect their surroundings and add visual interest to your images.

The key difference between diffuse and metallic materials lies in how they handle incoming light rays. As you learned in Lesson 2, diffuse materials scatter light randomly in all directions according to the Lambertian model. When a ray hits a diffuse surface, the scattered ray direction is chosen randomly from a hemisphere around the surface normal, with no relationship to the incoming ray direction. Metallic materials, on the other hand, follow the law of reflection: the outgoing ray direction is determined precisely by the incoming ray direction and the surface normal. This predictable, mirror-like behavior is what gives metals their characteristic shiny appearance and their ability to reflect clear images of their surroundings.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal