Rendering Glass and Refraction

Introduction: Beyond Reflection - Adding Transparency

Over the past four lessons, you've built a capable ray tracer that can render realistic scenes with smooth antialiasing, natural lighting through path tracing, and two fundamental material types. Your Lambertian diffuse materials scatter light randomly to create matte surfaces like chalk or unfinished wood, while your metal materials reflect light in a mirror-like way to simulate shiny surfaces like polished chrome or brushed aluminum. These two material types already give you considerable expressive power, allowing you to create scenes with rich visual variety and realistic material interactions.

However, if you look around the real world, you'll notice an important class of materials that your ray tracer cannot yet render: transparent materials. Think about a glass window, a crystal vase, a water-filled drinking glass, a diamond ring, or even ice cubes in a beverage. These materials have a distinctive appearance that sets them apart from both matte and metallic surfaces. They don't simply scatter light like diffuse materials, nor do they just reflect it like metals. Instead, transparent materials do something more complex: they both reflect light from their surfaces and transmit light through their volume, bending the transmitted light as it passes through.

This dual behavior creates the characteristic appearance of glass and other transparent materials. When you look at a windowpane, you can see through it to objects on the other side, but you can also see faint reflections of objects on your side. The balance between transmission and reflection changes depending on the viewing angle. When you look straight through a window, you see mostly the transmitted light with only faint reflections. When you look at a window from a steep angle, the reflections become much stronger and the window starts to look almost mirror-like. This angle-dependent behavior is called the Fresnel effect, and it's one of the key characteristics that makes transparent materials look realistic.

The bending of light as it passes through transparent materials is called refraction, and it's responsible for many familiar optical effects. Refraction is why a straw appears to bend when you put it in a glass of water, why objects look distorted when viewed through a curved glass surface, and why diamonds sparkle with such brilliance. Different materials bend light by different amounts, characterized by a property called the refractive index. Air has a refractive index close to one, water is about 1.33, typical glass is around 1.5, and diamond is approximately 2.4. The higher the refractive index, the more the material bends light, and the more dramatic the optical effects become.

In this lesson, you'll learn how to implement dielectric materials, which is the term we use in computer graphics for transparent materials like glass, water, and gemstones. You'll understand the physics of refraction and how to calculate refracted ray directions using Snell's Law. You'll learn about total internal reflection, a phenomenon where light cannot exit a material and instead reflects internally, creating effects like the sparkle in diamonds or the mirror-like appearance of water viewed from below the surface. You'll also implement Schlick's approximation, an efficient method for determining when light should reflect versus refract based on the viewing angle.

By the end of this lesson, you'll have implemented all three fundamental material types used in physically based rendering: diffuse materials that scatter light, metals that reflect light, and dielectrics that both reflect and transmit light. With these three material types, your ray tracer will be capable of rendering highly realistic scenes with complex material interactions. You'll be able to create images featuring glass spheres that refract and distort the objects behind them, water surfaces that reflect the sky while revealing what's beneath, and diamond-like objects that sparkle with internal reflections.

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