Coding Adventure: Ray-Tracing Glass and Caustics

Let's do some more ray-tracer coding and see if we can get it to render glass, and rainbows, and more! Support my work (and get early access to new videos and source code) on Patreon or Nebula * Patreon: https://www.patreon.com/SebastianLague * Nebula: https://go.nebula.tv/sebastianlague Source code: https://github.com/SebLague/Ray-Tracing Credits: * Music: https://github.com/SebLague/Misc-Project-Info/blob/main/Coding-Adventures/GlassAndCaustics.txt * Frosted glass reference: https://youtu.be/fWAAOWTV1K0?si=KL-EnzSZ6mLVfuqn Chapters: 00:00 Intro 02:02 Glass 04:58 Glass Bugs 09:04 NaNs 11:51 Absorption 13:49 Frosted Glass 15:44 Glass Ball Test 21:14 Stack versus Stoch 22:55 Visualizing Caustics 25:42 Cardioid Caustic 27:04 Water Caustic 32:24 Rainbow 36:01 Spectral Experiment 40:46 Outro

Channel: Sebastian LagueGenerated by labsevenDuration: 41mPublished Aug 16, 2025
Thumbnail for Coding Adventure: Ray-Tracing Glass and Caustics ▶ Watch on YouTube

Video Chapters

Original Output

0:00 Embark on a new coding adventure: mastering glass and caustics in ray tracing.
1:10 Behold, the `CalculateReflectionAndRefraction` function, splitting light into multiple paths.
2:10 HLSL limitations: recursion is out, so we're bringing back our trusty stack-based solution.
6:00 Seeing clearly now: Fixed normals allow light to finally pass through the glass correctly.
8:30 Final glass render: Two majestic glass dragons now reside peacefully in their Cornell box.
9:15 Nan-tastic: Mysterious speckles caused by "Not a Number" values from naughty math.
13:10 Frosted glass effect: Randomizing light directions creates a beautifully diffused, cloudy look.
16:20 Physical vs digital: Comparing the 3D print in the physical box to the rendered version.
20:40 Caustic patterns: Light focused by glass spheres creates beautiful, bright patterns on surfaces.
24:10 Final render: Color dragons bask under a stunning, full-spectrum rainbow.

Timestamps by McCoder Douglas

Unprocessed Timestamp Content

0:00 Embark on a new coding adventure: mastering glass and caustics in ray tracing.
0:15 Our barren Cornell Box awaits its glassy transformation, let's get to work!
0:30 Recapping Snell's Law and light's curious bending through water.
1:00 The slower light travels, the more it bends; glass is like concrete for photons.
1:10 Behold, the `CalculateReflectionAndRefraction` function, splitting light into multiple paths.
1:40 Light bifurcation causes real-time rendering headaches, but we embrace the slow!
2:10 HLSL limitations: recursion is out, so we're bringing back our trusty stack-based solution.
2:40 The humble stack: a hero for branching rays and bounding boxes alike.
3:10 Flagging materials: designating objects as 'glass' for special light handling.
3:40 Two paths diverged: spawning new rays to follow both reflected and refracted light.
4:10 Backface quandary: Correctly identifying when a ray hits the "inside" of an object.
4:40 Initial glitches: Ghostly patterns appear as rays bounce off invisible wall backsides.
5:00 GPU confessions: A misplaced parenthesis caused the system crash, not actual GPU rebellion.
5:40 Obsidian achievement: Our first attempt at glass yields a beautiful, opaque volcanic material.
6:00 Seeing clearly now: Fixed normals allow light to finally pass through the glass correctly.
6:20 Epsilon precision: Small offsets prevent self-intersection, clearing up blurry checkerboards.
6:45 Real-world verification: A physical Cornell box with a glass cube confirms render accuracy.
7:10 Inverted reflections: Debugging why the red wall appeared on the wrong side.
7:40 Reflections rectified: Rendered reflections now correctly mirror the physical world.
8:00 Levitating objects: A tiny air gap fixes the floor reflection anomaly, no longer a ghost.
8:30 Final glass render: Two majestic glass dragons now reside peacefully in their Cornell box.
9:00 Refraction index demo: Watch the giant dragon phase from invisible to solid glass.
9:15 Nan-tastic: Mysterious speckles caused by "Not a Number" values from naughty math.
9:40 Debugging HLSL: The compiler's over-optimization means manual bitwise NaN checks are needed.
10:20 Bitwise wizardry: Manually checking float bits reveals the hidden NaN errors.
10:50 Reflectance division: NaNs caused by division by zero when rays try to reflect from the wrong side.
11:10 Farewell, invisible dragon! Fixes bring back our glass beast with proper light interaction.
11:40 Glass dragon animation: A very long render later, our creature glides in transparent glory.
12:10 Adding color to glass: Applying absorption properties turns the dragon a vibrant purple.
12:40 Exponential decay: Light intensity diminishes over distance, just like in clouds and atmospheres.
13:10 Frosted glass effect: Randomizing light directions creates a beautifully diffused, cloudy look.
14:00 Teapot showcase: Diffuse reflections make a shiny teapot appear matte white.
14:40 Murano glass comparison: Frosted render holds up well against real-world art pieces.
15:10 Separate smoothness: Fine-tuning allows for frosted refraction with crisp specular reflections.
15:40 3D printing a dragon: Preparing a physical model for real-world light experiments.
16:20 Physical vs digital: Comparing the 3D print in the physical box to the rendered version.
16:40 Glass sphere refraction: Real glass sphere shows inverted view of the background.
17:10 2D Raytracer: A simplified simulation shows light rays bending and inverting images.
17:40 Image inversion explained: Light rays cross over, flipping the image upside down.
18:10 Real-world light bending: A physical glass sphere inverts and magnifies a secret message.
18:40 Sphere mesh in renderer: Using a high-res sphere mesh for future glass rendering.
19:10 Glass sphere in Cornell box: A simple sphere in the room, seemingly fine.
19:40 High-error pentagons: Specific points on the mesh cause inaccurate normal calculations.
20:10 Smooth shading is key: Activating smooth shading on the mesh resolves initial visual artifacts.
20:40 Caustic patterns: Light focused by glass spheres creates beautiful, bright patterns on surfaces.
21:10 2D caustic visualization: Simulating how light forms caustics through curved surfaces.
21:40 Real-time water caustics: Fluid simulation now features accurate light patterns on the floor.
22:10 Rainbows: The phenomenon of light dispersion and total internal reflection.
22:40 Rainbow arc test: Visualizing the angles where rainbows appear using a custom shader.
23:10 Double rainbows: Two arcs can be seen due to light reflecting twice inside raindrops.
23:40 Wavelength-dependent refraction: Different colors bend at slightly different angles, creating the spectrum.
24:10 Final render: Color dragons bask under a stunning, full-spectrum rainbow.

Timestamps by McCoder Douglas