<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ibukun Sanni</title>
    <description>The latest articles on DEV Community by Ibukun Sanni (@ibukunsanni).</description>
    <link>https://dev.to/ibukunsanni</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2231564%2F2c8a87df-55af-425a-9639-a9a545247ad6.jpg</url>
      <title>DEV Community: Ibukun Sanni</title>
      <link>https://dev.to/ibukunsanni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ibukunsanni"/>
    <language>en</language>
    <item>
      <title>Making it fast: time for some optimization</title>
      <dc:creator>Ibukun Sanni</dc:creator>
      <pubDate>Tue, 24 Nov 2026 22:45:04 +0000</pubDate>
      <link>https://dev.to/ibukunsanni/making-it-fast-time-for-some-optimization-2n42</link>
      <guid>https://dev.to/ibukunsanni/making-it-fast-time-for-some-optimization-2n42</guid>
      <description>&lt;div class="table-wrapper-paragraph"&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;

  &lt;td width="50%"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F809ff74f0dafd0c6f809a95f5e54780273e2bd34%2Fdocs%2Fimages%2Fbvh%2Fspeed-cornell-blocks.png" alt="A square room with a green wall on the left, a red wall on the right and white walls, floor and ceiling, lit by a square lamp in the ceiling. A tall white block stands at the back left and a short white cube at the front right." width="600" height="600"&gt;&lt;/td&gt;

  &lt;td width="50%"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F809ff74f0dafd0c6f809a95f5e54780273e2bd34%2Fdocs%2Fimages%2Fbvh%2Fspeed-cornell-vehicles.png" alt="A square room with a green wall on the left and a red wall on the right. A chrome spaceship hangs near the ceiling at the upper left, a small dark drone hovers at the right, a red sports car sits on the floor at the front left, and a tall grey block and a clear glass ball stand in the middle, the ball focusing the lamp into a bright spot on the floor." width="800" height="800"&gt;&lt;/td&gt;

  &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;br&gt;&lt;em&gt;Figure 1: The Cornell box with two blocks, and the one with vehicles. Car: "&lt;a href="https://poly.pizza/m/5zUWP5UsLg-" rel="noopener noreferrer"&gt;CAR Model&lt;/a&gt;" by Ignition Labs, &lt;a href="https://creativecommons.org/licenses/by/3.0/" rel="noopener noreferrer"&gt;CC BY 3.0&lt;/a&gt;. Drone: "&lt;a href="https://poly.pizza/m/DNbUoMtG3H" rel="noopener noreferrer"&gt;Drone&lt;/a&gt;" by NateGazzard, &lt;a href="https://creativecommons.org/licenses/by/3.0/" rel="noopener noreferrer"&gt;CC BY 3.0&lt;/a&gt;. Spaceship: "&lt;a href="https://poly.pizza/m/u105mYHLHU" rel="noopener noreferrer"&gt;Spaceship&lt;/a&gt;" by Quaternius, &lt;a href="https://creativecommons.org/publicdomain/zero/1.0/" rel="noopener noreferrer"&gt;CC0&lt;/a&gt;. Both CC BY models are rendered with plain materials in place of their own.&lt;/em&gt;
  

&lt;p&gt;The images above show two different Cornell boxes that I want to speed up. In my experience, premature optimization can be a problem: you keep shaving off time here and there and never get anything done. Don't optimize until you can see it's a problem. Well, it officially became one: I'm trying to shoot 20,000 samples per pixel. Now I need to make it fast.&lt;/p&gt;

&lt;h2&gt;
  
  
  The result
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F809ff74f0dafd0c6f809a95f5e54780273e2bd34%2Fdocs%2Fimages%2Fbvh%2Fspeed-overlay.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F809ff74f0dafd0c6f809a95f5e54780273e2bd34%2Fdocs%2Fimages%2Fbvh%2Fspeed-overlay.png" alt="A line chart on a log axis with two lines over ten steps. Both start together near 0.1× at one thread and meet at 1× at twenty threads. The vehicles' line jumps to about 320× at the mesh BVH step, rises to about 600× at the cull step and stays nearly flat to the end, where a ring marks 705×. The blocks' line stays at 1× through the two mesh steps, rises to about 3× at the cull step, dips at the top-level tree and ends near 3×, where a ring marks 3.1×." width="800" height="528"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Figure 2: Every speed-up on the two Cornell boxes, showing every optimization from one thread to the small-node scan.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;cornell_blocks&lt;/code&gt; this step&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;cornell_blocks&lt;/code&gt; total&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;cornell_vehicles&lt;/code&gt; this step&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;cornell_vehicles&lt;/code&gt; total&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1 thread&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;0.107×&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;0.0986×&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20 threads&lt;/td&gt;
&lt;td&gt;9.37×&lt;/td&gt;
&lt;td&gt;1×&lt;/td&gt;
&lt;td&gt;10.1×&lt;/td&gt;
&lt;td&gt;1×&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;mesh BVH&lt;/td&gt;
&lt;td&gt;1.01×&lt;/td&gt;
&lt;td&gt;1.01×&lt;/td&gt;
&lt;td&gt;323×&lt;/td&gt;
&lt;td&gt;323×&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SAH&lt;/td&gt;
&lt;td&gt;1.02×&lt;/td&gt;
&lt;td&gt;≈1.02×&lt;/td&gt;
&lt;td&gt;1.06×&lt;/td&gt;
&lt;td&gt;≈342×&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;cull&lt;/td&gt;
&lt;td&gt;2.7×&lt;/td&gt;
&lt;td&gt;≈2.77×&lt;/td&gt;
&lt;td&gt;1.74×&lt;/td&gt;
&lt;td&gt;≈593×&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;identity skip&lt;/td&gt;
&lt;td&gt;1.17×&lt;/td&gt;
&lt;td&gt;≈3.24×&lt;/td&gt;
&lt;td&gt;1.08×&lt;/td&gt;
&lt;td&gt;≈638×&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;top-level tree&lt;/td&gt;
&lt;td&gt;0.842×&lt;/td&gt;
&lt;td&gt;≈2.72×&lt;/td&gt;
&lt;td&gt;0.96×&lt;/td&gt;
&lt;td&gt;≈612×&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tree at 16+ only&lt;/td&gt;
&lt;td&gt;1.09×&lt;/td&gt;
&lt;td&gt;≈2.98×&lt;/td&gt;
&lt;td&gt;0.986×&lt;/td&gt;
&lt;td&gt;≈604×&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;box primitive&lt;/td&gt;
&lt;td&gt;0.966×&lt;/td&gt;
&lt;td&gt;≈2.88×&lt;/td&gt;
&lt;td&gt;1.08×&lt;/td&gt;
&lt;td&gt;≈650×&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;small-node scan&lt;/td&gt;
&lt;td&gt;1.08×&lt;/td&gt;
&lt;td&gt;≈3.12×&lt;/td&gt;
&lt;td&gt;0.999×&lt;/td&gt;
&lt;td&gt;≈649×&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;last step, timed directly against the start&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;3.1×&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;705×&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Table 1: Each step's own gain, and the running total measured against 20 threads with no other optimizations.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  A few things stand out straight away
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Threads help both scenes about equally:&lt;/strong&gt; 9.37× for the blocks and 10.1× for the vehicles.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The mesh BVH only matters where there are meshes.&lt;/strong&gt; It speeds &lt;code&gt;cornell_vehicles&lt;/code&gt; up 323×, because the vehicles are built from triangles, and leaves &lt;code&gt;cornell_blocks&lt;/code&gt; unchanged at 1.01×, because the blocks have no triangles to search.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Culling is the other clear gain:&lt;/strong&gt; 2.7× for the blocks and 1.74× for the vehicles.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Every other step moves things by less than 20%,&lt;/strong&gt; and the top-level tree actually slows both scenes down. The rest of the article looks at why.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The cost
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F809ff74f0dafd0c6f809a95f5e54780273e2bd34%2Fdocs%2Fimages%2Fbvh%2Fspeed-cornell-blocks.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F809ff74f0dafd0c6f809a95f5e54780273e2bd34%2Fdocs%2Fimages%2Fbvh%2Fspeed-cornell-blocks.png" alt="A square room with a green wall on the left, a red wall on the right and white walls, floor and ceiling, lit by a square lamp in the ceiling. A tall white block stands at the back left and a short white cube at the front right." width="600" height="600"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Figure 3: The Cornell box with its two blocks, the book's scene: five walls, a ceiling lamp and two boxes of six quads each. 600², 20,000 samples per pixel.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F809ff74f0dafd0c6f809a95f5e54780273e2bd34%2Fdocs%2Fimages%2Fbvh%2Fspeed-cornell-vehicles.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F809ff74f0dafd0c6f809a95f5e54780273e2bd34%2Fdocs%2Fimages%2Fbvh%2Fspeed-cornell-vehicles.png" alt="A square room with a green wall on the left and a red wall on the right. A chrome spaceship hangs near the ceiling at the upper left, a small dark drone hovers at the right, a red sports car sits on the floor at the front left, and a tall grey block and a clear glass ball stand in the middle, the ball focusing the lamp into a bright spot on the floor." width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Figure 4: The Cornell box with three vehicles, all triangle meshes: a car on the floor, a drone hovering, a spaceship near the ceiling, beside the tall block and a glass ball. 1200², 20,000 samples per pixel. Car: "&lt;a href="https://poly.pizza/m/5zUWP5UsLg-" rel="noopener noreferrer"&gt;CAR Model&lt;/a&gt;" by Ignition Labs, &lt;a href="https://creativecommons.org/licenses/by/3.0/" rel="noopener noreferrer"&gt;CC BY 3.0&lt;/a&gt;. Drone: "&lt;a href="https://poly.pizza/m/DNbUoMtG3H" rel="noopener noreferrer"&gt;Drone&lt;/a&gt;" by NateGazzard, &lt;a href="https://creativecommons.org/licenses/by/3.0/" rel="noopener noreferrer"&gt;CC BY 3.0&lt;/a&gt;. Spaceship: "&lt;a href="https://poly.pizza/m/u105mYHLHU" rel="noopener noreferrer"&gt;Spaceship&lt;/a&gt;" by Quaternius, &lt;a href="https://creativecommons.org/publicdomain/zero/1.0/" rel="noopener noreferrer"&gt;CC0&lt;/a&gt;. Both CC BY models are rendered with plain materials in place of their own.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;A render's time is the number of pixels, times the samples per pixel, times what each sample costs. The two final images above push the first two factors as far as I took them: 20,000 samples per pixel, at 600² for the blocks and 1200² for the vehicles. Here is what they took with every optimization, and what they would have taken with none:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;cornell_blocks&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;cornell_vehicles&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Image&lt;/td&gt;
&lt;td&gt;600², 20,000 spp&lt;/td&gt;
&lt;td&gt;1200², 20,000 spp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Samples traced&lt;/td&gt;
&lt;td&gt;7.2 billion&lt;/td&gt;
&lt;td&gt;28.8 billion&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No optimizations, 1 thread (estimate)&lt;/td&gt;
&lt;td&gt;≈2.9 hours&lt;/td&gt;
&lt;td&gt;≈250 days&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;All optimizations, 20 threads&lt;/td&gt;
&lt;td&gt;6 min 41 s&lt;/td&gt;
&lt;td&gt;≈57 min&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gain&lt;/td&gt;
&lt;td&gt;≈26×&lt;/td&gt;
&lt;td&gt;≈6,400×&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Table 2: What the two final renders took with every optimization, against an estimate for one thread with none.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The gains in Figure 2 come from small 300² renders, which keep each run to seconds so I can repeat it many times. Because time grows with pixels × samples, a step that makes the small image 2× faster makes the big one 2× faster too. So once each step had proven itself on the small images, I spent the gains on the big ones and rendered as much as I could. For a CPU, these numbers aren't bad, but the next big jump is probably porting to a GPU.&lt;/p&gt;

&lt;p&gt;From the estimates, I'd say the optimizations came at the right time. Nobody waits 250 days for a picture, but 57 minutes is a lunch break. For the rest of the article, I'll explore what each optimization is and how much time it saved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Threads
&lt;/h2&gt;

&lt;p&gt;The first thing I can change is the number of threads. Until now, everything ran on one thread, so one core did all the work while the rest of the CPU sat idle. My laptop can run up to 20 threads at once.&lt;/p&gt;

&lt;p&gt;A ray tracer is about as easy to split as work gets. Every pixel is computed on its own: no pixel waits for its neighbour. So I cut the image into horizontal bands, one band of rows per thread, and let every thread work on its own band at the same time. Think of Jason (yes, I have played some GTA 6) painting the walls for Lucia. By himself, he gets it done eventually. With 20 Jasons (lucky Lucia), each with their own stretch of wall, the job finishes far sooner, as long as nobody has to wait for anyone else.&lt;/p&gt;

&lt;p&gt;However, some Jasons finish earlier than others. That's another opportunity for optimization, but it's easy to fall into the never-done trap, so it will have to wait until much later.&lt;/p&gt;

&lt;p&gt;I rendered both boxes on 1 thread, then 2, 4, 8, 12, 14 and 20, and timed each one. These runs use the final build, with every optimization in this article turned on, so the 20-thread numbers below differ from the 9.37× and 10.1× in Table 1, which were measured on the build with none:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Threads&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;12&lt;/th&gt;
&lt;th&gt;14&lt;/th&gt;
&lt;th&gt;20&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;cornell_blocks&lt;/code&gt; (64 spp)&lt;/td&gt;
&lt;td&gt;1×&lt;/td&gt;
&lt;td&gt;≈2.10×&lt;/td&gt;
&lt;td&gt;≈3.76×&lt;/td&gt;
&lt;td&gt;≈5.71×&lt;/td&gt;
&lt;td&gt;≈6.97×&lt;/td&gt;
&lt;td&gt;≈7.88×&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;8.85×&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;cornell_vehicles&lt;/code&gt; (16 spp)&lt;/td&gt;
&lt;td&gt;1×&lt;/td&gt;
&lt;td&gt;≈1.81×&lt;/td&gt;
&lt;td&gt;≈3.45×&lt;/td&gt;
&lt;td&gt;≈5.09×&lt;/td&gt;
&lt;td&gt;≈5.35×&lt;/td&gt;
&lt;td&gt;≈6.67×&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;7.79×&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Table 3: The final build's speed-up over one thread, at each thread count.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F809ff74f0dafd0c6f809a95f5e54780273e2bd34%2Fdocs%2Fimages%2Fbvh%2Fspeed-threads.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F809ff74f0dafd0c6f809a95f5e54780273e2bd34%2Fdocs%2Fimages%2Fbvh%2Fspeed-threads.png" alt="A line chart of speed-up against thread count. A dashed ideal line rises straight to 20× at 20 threads. Two coloured lines follow it closely to 4 threads, then bend away and end near 9× and 8×." width="800" height="558"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Figure 5: The final build on 1 to 20 threads, against ideal scaling.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;Notice the sample counts: 64 and 16 per pixel on a 300² image, nowhere near the 20,000 per pixel at 600² and 1200² in Figure 3 and Figure 4. As earlier mentioned, I kept both samples and pixels small because the gain will translate and indeed it did.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why it is not ideal
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Not all 20 threads are equal.&lt;/strong&gt; My CPU is an i7-12700H, with 6 fast performance cores and 8 slower efficiency cores. Each performance core runs two threads, while each efficiency core runs one. That gives 12 threads across 6 cores and 8 threads across 8 cores. So 20 threads is really 14 cores of two different speeds, and the threads I add last are the weakest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One core alone runs faster.&lt;/strong&gt; With only one core busy, the chip can push that core to its top speed. With every core busy, the power and heat are shared across all of them, so each core runs slower, even though together they get more done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The laptop throttles.&lt;/strong&gt; When the chip gets too hot, it lowers its own clock speed to protect itself, so every core does less work each second. With all 20 threads busy, it heats up faster and slows down sooner. I also had other applications open, which take a share of the cores and add their own heat.&lt;/p&gt;

&lt;p&gt;Ultimately, being about 8 times faster may not be ideal, but it is an overall improvement.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mesh BVH
&lt;/h2&gt;

&lt;p&gt;This gave us the biggest gain of the whole article: 323× on &lt;code&gt;cornell_vehicles&lt;/code&gt;, and 1.01× on &lt;code&gt;cornell_blocks&lt;/code&gt;, which is no change at all (Table 1). On 20 threads, one pass over the small vehicles image went from 6.7 s to about 21 ms. That one step is most of the ≈6,400× in Table 2.&lt;/p&gt;

&lt;p&gt;That isn't surprising once you see what a BVH does.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What a BVH is.&lt;/strong&gt; To find what a ray hits, the simplest approach is to test it against everything in the scene, one by one. A bounding volume hierarchy (BVH) avoids that by putting things in boxes. Nearby objects share a box, nearby boxes share a bigger box, and so on, up to one box around the whole scene. A ray tests the big box first. If it misses, everything inside is skipped with a single test. If it hits, the ray checks the smaller boxes inside and only goes further into the ones it hits. It works like looking up a word in a dictionary: you jump to the right letter instead of reading every page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it needs to reach inside meshes.&lt;/strong&gt; &lt;em&gt;Ray Tracing: The Next Week&lt;/em&gt; builds its BVH over whole objects. A mesh like the car in Figure 4 counts as one object, even though it is made of thousands of triangles. So the tree could lead a ray to the car, but then the ray still tested every triangle in it, one by one. The mesh BVH builds a second tree inside each mesh, so a ray that reaches the car only tests the few triangles near where it lands.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why the vehicles gained so much and the blocks so little.&lt;/strong&gt; The vehicles are made of meshes. Without the mesh BVH, each ray tested 21,024 triangles; with it, about one. The frame got 323× faster rather than thousands of times, because once the triangle tests were gone, shading and bouncing took up most of the time. The blocks have no meshes, only flat quads, so the mesh BVH had nothing to speed up.&lt;/p&gt;

&lt;h3&gt;
  
  
  SAH
&lt;/h3&gt;

&lt;p&gt;The first tree split each box's triangles at the middle one, so each half got the same number. That's simple, but it ignores how big the boxes end up. A ray is more likely to hit a big box than a small one, so a split that leaves one huge box and one tiny one makes rays do more work. The surface area heuristic (SAH) weighs that: it tries several places to cut and keeps the one where each side's surface area times its number of triangles is lowest. That lowers the overall cost of walking the BVH.&lt;/p&gt;

&lt;p&gt;On the vehicles it cut triangle tests per ray by a third, but the frame only got 1.06× faster (Table 1). That's why I stopped there. After the mesh BVH a ray tests about one triangle, so making that one test rarer can't save much. The time had moved somewhere else: the very next step, culling, gave the vehicles 1.74×, far more than SAH did.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the rest were small
&lt;/h2&gt;

&lt;p&gt;After the mesh BVH, most of what was left wasn't triangles. So each step after it could only speed up a small slice of the time, and a big gain on a small slice is a small gain overall. Culling is the one exception, because it went after where the time actually was: the scene graph.&lt;/p&gt;

&lt;h3&gt;
  
  
  Culling
&lt;/h3&gt;

&lt;p&gt;A scene in my ray tracer is a tree of nodes. Each node has its own position, rotation and scale (a matrix), and holds shapes or more nodes. Each block in &lt;code&gt;cornell_blocks&lt;/code&gt; is one node with six quads under it. Before culling, a ray reaching a node was moved into that node's space with the matrix and then tested against every shape inside, whether it came anywhere near them or not.&lt;/p&gt;

&lt;p&gt;Culling puts a box around each child and tests the ray against that box first. If the ray misses the box, it skips the child entirely, so the matrix and everything inside are never touched. For a block, one box test now replaces a matrix and six quads for every ray that misses it, and most rays miss most blocks.&lt;/p&gt;

&lt;p&gt;That gave both scenes a big jump: 2.7× for the blocks and 1.74× for the vehicles.&lt;/p&gt;

&lt;h3&gt;
  
  
  The rest
&lt;/h3&gt;

&lt;p&gt;The other steps each moved things by less than 20%. Skipping the matrix maths for nodes that aren't moved gave a little. A tree over each node's children made the blocks slower, since a handful of children is quicker to test in order, so it now only kicks in at 16 or more. The rest landed within noise: on these two scenes there just wasn't much time left to save.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;There is still plenty left to try on the CPU:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Better work sharing between threads.&lt;/strong&gt; Some threads finish their band early and sit idle. Cutting the image into small tiles instead of bands should keep every thread busy to the end.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A tighter BVH layout.&lt;/strong&gt; Smaller nodes that fit the CPU's cache better, and wider nodes with four or eight children tested at once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fewer samples for the same picture.&lt;/strong&gt; Sampling the lights directly and adding a denoiser would get a clean image with far fewer samples per pixel.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The GPU.&lt;/strong&gt; Once the CPU work is done, porting to the GPU is the next big jump.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I do now
&lt;/h2&gt;

&lt;p&gt;Making it fast taught me as much about measuring as about optimizing. These are the habits I keep:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Measure the before and the after together,&lt;/strong&gt; in the same session, alternating the two, because the laptop's speed drifts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The picture must not change.&lt;/strong&gt; A speed-only change has to produce a byte-identical image, or it isn't a speed-only change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count as well as time.&lt;/strong&gt; Counts like triangle tests per ray come out the same on every run, so they catch changes the clock is too noisy to see.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write the prediction down first.&lt;/strong&gt; I was wrong several times, and each time that was the most useful thing I learned.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep runs short.&lt;/strong&gt; Small images and few samples, so every timing can be repeated and the laptop doesn't heat up mid-run.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Peter Shirley, Trevor David Black, Steve Hollasch, &lt;em&gt;Ray Tracing: The Next Week&lt;/em&gt;, &lt;a href="https://raytracing.github.io/" rel="noopener noreferrer"&gt;https://raytracing.github.io/&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Matt Pharr, Wenzel Jakob, Greg Humphreys, &lt;em&gt;Physically Based Rendering&lt;/em&gt;, 4th ed., the BVH chapter (binned SAH, &lt;code&gt;LinearBVHNode&lt;/code&gt;),
&lt;a href="https://pbr-book.org/" rel="noopener noreferrer"&gt;https://pbr-book.org/&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>graphics</category>
      <category>cpp</category>
      <category>raytracing</category>
      <category>performance</category>
    </item>
    <item>
      <title>My Renderer and Blender Painted the Same Gun</title>
      <dc:creator>Ibukun Sanni</dc:creator>
      <pubDate>Fri, 30 Oct 2026 00:51:53 +0000</pubDate>
      <link>https://dev.to/ibukunsanni/my-renderer-and-blender-painted-the-same-gun-32af</link>
      <guid>https://dev.to/ibukunsanni/my-renderer-and-blender-painted-the-same-gun-32af</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-07-smg-gun.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-07-smg-gun.png" alt="A yellow, grey and black stylised gun floating over a green and white checkered floor under a pale blue sky." width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;The SMG, textured from its own image map, in my renderer. Model: "SMG GUN Remastered" by ROHIT3DMODELS, CC-BY-4.0.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a texture?
&lt;/h2&gt;

&lt;p&gt;In my earlier explorations, every object in my renders had one material with a single color. A texture allows multiple colors across the surface of an object. It simply tells us what color is supposed to be at the ray's hit location. It can be as simple as a checkered pattern like a chessboard, like the floors in all of these images, or as complicated as the texture on the SMG above.&lt;/p&gt;

&lt;p&gt;To wrap an image, each point on the surface needs to know where it sits on that image. Since we are already using 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;z&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 coordinates for the model, we adopt 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 coordinates: two numbers from 0 to 1 that map a 2D image onto a 3D object.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Side bar: it keeps coming back to 2D&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rendering is already a 2D image made from a 3D scene. Then the &lt;a href="https://dev.to/ibukunsanni/apparently-bland-2d-grey-squares-are-important-to-my-3d-renderer-21fo"&gt;white furnace test&lt;/a&gt; turned correctness into a flat grey square. &lt;a href="https://dev.to/ibukunsanni/a-hexagonal-aperture-would-break-every-diffuse-surface-in-my-renderer-3874"&gt;Malley's method&lt;/a&gt; picks 3D bounce directions by lifting points straight up off a flat 2D disk. Now a texture runs the other way, taking a 2D image and mapping it to a 3D surface. I love it, and I am sure there's more to come.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We have to figure out a few things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;where the 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 comes from, for spheres and for meshes&lt;/li&gt;
&lt;li&gt;which way up the image is&lt;/li&gt;
&lt;li&gt;what color space it's stored in&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Painting from space, then painting the surface
&lt;/h2&gt;

&lt;p&gt;Following &lt;em&gt;Ray Tracing: The Next Week&lt;/em&gt;, we start with a checkered cut from space. For a checkered cut from space it is simple: take the floor of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;z&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and add them up. If the sum is even, use one color; if it's odd, use the other. In my case, yin and yang.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt; &lt;span class="nf"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x_int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inv_scale_&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y_int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inv_scale_&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;z_int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inv_scale_&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="c1"&gt;// Yin fills the cubes whose index sum is even, yang the odd ones.&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;is_even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x_int&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y_int&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z_int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;is_even&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;yin_&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;yang_&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the floor under the SMG, from its scene file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lua"&gt;&lt;code&gt;&lt;span class="kd"&gt;local&lt;/span&gt; &lt;span class="n"&gt;floor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;checkered&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                            &lt;span class="n"&gt;yin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;yang&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cut from space, the color depends only on where the hit point is, no 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 needed. It's perfect for a floor and for flat-sided shapes like a cube. On a sphere, it's not as glamorous: the curve slices the cells unevenly.&lt;/p&gt;


  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-02-uv-checker-cube.png" alt="Three checkered spheres and a small cube on a checkered floor. Two blue spheres have neat cells following the sphere; the red one on the right has cells cut at odd angles, and the red and white cube in front of it has square cells on each face." width="640" height="360"&gt;&lt;em&gt;Left and middle: a checkered pattern painted in each sphere's (u, v), so the cells follow longitude and latitude. Right: the checkered pattern cut from space, sliced unevenly by the sphere's curve, and square on the flat faces of the cube in front.&lt;/em&gt;
  


&lt;p&gt;The fix is to give every hit its 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. For a sphere, that's longitude and latitude, which gives the checkered look that narrows at the poles in the image above.&lt;/p&gt;
&lt;h2&gt;
  
  
  Reading an image
&lt;/h2&gt;

&lt;p&gt;An image texture is a lookup: 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 in, color out. Reading it right came down to two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Which way is up.&lt;/strong&gt; Image rows run top to bottom; 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 runs bottom to top. A test image with a different color in each quadrant pins that down, and a unit test holds it there.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linear light.&lt;/strong&gt; A PNG stores sRGB, which is brightness curved for screens. My renderer works in linear light, so each texel (a pixel of the texture) is decoded once, on load.&lt;/li&gt;
&lt;/ul&gt;


  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-03-image-textures.png" alt="Two spheres: one with a four-colored test grid, one showing Jupiter's banded clouds." width="640" height="360"&gt;&lt;em&gt;A test grid the right way up, and a Jupiter map wrapped onto a sphere. Jupiter map: NASA / JPL-Caltech / SSI / Southwest Research Institute / Malin Space Science Systems / Italian Space Agency (ASI) / Italian National Institute for Astrophysics (INAF) / JIRAM / Björn Jónsson, CC BY 3.0.&lt;/em&gt;
  


&lt;p&gt;The Jupiter map is 170 MB, far too big for git, so a fresh clone doesn't have it. Instead of crashing, a texture that can't be loaded logs a warning and renders as a grey and white checkered pattern. A missing file is obvious, but the rest of the scene still renders.&lt;/p&gt;


  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-03-missing-texture-placeholder.png" alt="The four-colored test grid sphere beside a sphere in a faint grey and white checker." width="640" height="360"&gt;&lt;em&gt;The same scene on a fresh clone: Jupiter's file is missing, so its sphere gets the placeholder.&lt;/em&gt;
  

&lt;h2&gt;
  
  
  Meshes
&lt;/h2&gt;

&lt;p&gt;A sphere can compute its 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. A mesh can't. The artist decided it, and it's stored in the OBJ file. A fine arrangement, because I was not writing those myself.&lt;/p&gt;

&lt;p&gt;The file doesn't store a 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 for every point on the surface, though. It stores one per corner: each &lt;code&gt;vt&lt;/code&gt; line is a 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, and each corner of a face names one alongside its position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vt 0.25 0.50
f 1/1 2/2 3/3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A ray almost never lands exactly on a corner. It lands somewhere inside the triangle, so its 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 has to be a blend of the three corners' values, weighted by how close the hit is to each corner. Those weights are the triangle's barycentric coordinates.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-scratchapixel-barycentric.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-scratchapixel-barycentric.png" alt="A triangle ABC split at an inner point P into a red, a green and a blue sub-triangle, labelled u, v and w." width="800" height="518"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Barycentric coordinates as areas: each weight is the sub-triangle opposite a corner over the whole triangle. The u, v and w here are those weights, not texture coordinates. Figure: &lt;a href="https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/barycentric-coordinates.html" rel="noopener noreferrer"&gt;Scratchapixel&lt;/a&gt;, CC BY-NC-ND 4.0.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;Barycentric coordinates describe a point inside a triangle as three weights that sum to 1, one per corner, each the area of the sub-triangle opposite it. A hit on a corner gives that corner all the weight while a hit in the middle shares it equally.&lt;/p&gt;

&lt;p&gt;The best part: the intersection test already computes these weights to decide whether the ray hit the triangle at all. So the hit's 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is just the three corners' 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 blended by the same weights that placed it on the triangle.&lt;/p&gt;

&lt;p&gt;Paint each piece of the result its own color and you see the artist's unwrap: the model cut into pieces, called islands, and laid flat on the image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-04-mesh-uvs-smg-unwrap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-04-mesh-uvs-smg-unwrap.png" alt="Two panels: the gun in flat patches of many distinct colors, above a square of flat islands, each a single color that matches one patch on the gun." width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Top: the SMG with each UV island painted its own color. Bottom: the artist's unwrap, every island in the same color it has on the gun, so a patch on the gun and its island match. Model: "SMG GUN Remastered" by ROHIT3DMODELS, CC-BY-4.0.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Matching Cycles
&lt;/h2&gt;

&lt;p&gt;I am proud of how far my renderer has come, but looking right is not the same as being right. So I checked it against my favorite renderer, or more specifically the easiest to access: Blender's Cycles.&lt;/p&gt;

&lt;p&gt;Blender rebuilds the same scene from a script, and my tone map is applied to its output. Then a script compares the two.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-06-smg-gun-vs-blender.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-06-smg-gun-vs-blender.png" alt="Two near-identical renders of the gun, one above the other, and a mostly black difference image with faint grid lines." width="640" height="1152"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Top: my renderer. Middle: Cycles. Bottom: their difference, four times brighter. Model: "SMG GUN Remastered" by ROHIT3DMODELS, CC-BY-4.0.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;256 samples per pixel&lt;/th&gt;
&lt;th&gt;Mean color (R, G, B)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;My renderer&lt;/td&gt;
&lt;td&gt;116.28, 138.90, 142.72&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cycles&lt;/td&gt;
&lt;td&gt;116.30, 138.92, 142.73&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Mean absolute difference: 2.36 out of 255, and I can assume that is just noise. I will take it that my renderer matches Blender Cycles. Take it or leave it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; this is extremely simplified. Blender was instructed to use the same simplifications as mine, so not all the artist's changes are present. It is not the artist's full look, but I will still take the win.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where did I mess up? Because that always happens
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Rings on every floor.&lt;/strong&gt; A debug view of surface color alone showed two clean colors, so the rings were &lt;em&gt;shading&lt;/em&gt;. The floor is a sphere of radius 1000, and in single precision a hit that far out rounds to just inside it, so bounce rays shadowed the floor they started on. Double precision fixed it. In hindsight, it should have just been a plane.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-wrong-floor-rings.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-wrong-floor-rings.png" alt="A checkered car on a checkered floor covered in fine concentric rings." width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Wrong: concentric rings on the floor, from rays shadowing the surface they started on. Car: "CAR Model" by Ignition Labs, CC BY 3.0.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Blender painted one part out of six.&lt;/strong&gt; My first comparison model was a cartoon spaceship, and the comparison had whole parts missing. My solution was very elegant: I used a different model immediately. That's the reality of software development. It's a bug I will fix, but for now the main focus is getting a properly rendered image. Trade-offs had to be made and prioritized.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-wrong-blender-one-of-six-parts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FIbukunSanni%2FRaytracer%2F8dd1550466e49663566f89b18888b37be753157e%2Fdocs%2Fimages%2Ftextures%2Fstep9-wrong-blender-one-of-six-parts.png" alt="Two renders of a cartoon spaceship and their difference, which shows large bright shapes where parts are missing." width="640" height="1152"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Wrong: the Blender side painting one of the ship's six parts. Model: "Cartoon spaceship" by pinguinoconpulgares, CC-BY-4.0.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I do now
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Check against a renderer I trust.&lt;/strong&gt; Looking right is not being right. My initial hunch was right, but I will emphasize it. Cycles just made it quantifiable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build a debug view for every new input.&lt;/strong&gt; The 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 view showed the unwrap, and the surface-color view showed the rings were not the texture.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"SMG GUN Remastered" by &lt;a href="https://sketchfab.com/Rohit3Dasset" rel="noopener noreferrer"&gt;ROHIT3DMODELS&lt;/a&gt;, from &lt;a href="https://sketchfab.com/3d-models/smg-gun-remastered-2e9bf758703847c88d854fa204384203" rel="noopener noreferrer"&gt;Sketchfab&lt;/a&gt;, &lt;a href="http://creativecommons.org/licenses/by/4.0/" rel="noopener noreferrer"&gt;CC-BY-4.0&lt;/a&gt;. Rendered with its base-color map only.&lt;/li&gt;
&lt;li&gt;"Cartoon spaceship" by &lt;a href="https://sketchfab.com/pinguinoconpulgares" rel="noopener noreferrer"&gt;pinguinoconpulgares&lt;/a&gt;, from &lt;a href="https://sketchfab.com/3d-models/cartoon-spaceship-a377d653202f45a68d441a987aa6dbda" rel="noopener noreferrer"&gt;Sketchfab&lt;/a&gt;, &lt;a href="http://creativecommons.org/licenses/by/4.0/" rel="noopener noreferrer"&gt;CC-BY-4.0&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;"Merged Cassini and Juno global map of Jupiter", processed by Björn Jónsson. Credit: NASA / JPL-Caltech / SSI / Southwest Research Institute / Malin Space Science Systems / Italian Space Agency (ASI) / Italian National Institute for Astrophysics (INAF) / JIRAM / Björn Jónsson, &lt;a href="https://creativecommons.org/licenses/by/3.0/" rel="noopener noreferrer"&gt;CC BY 3.0&lt;/a&gt;. Wrapped onto a sphere and rendered. &lt;a href="https://www.planetary.org/space-images/merged-cassini-and-juno" rel="noopener noreferrer"&gt;Source&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;"CAR Model" by Ignition Labs, from &lt;a href="https://poly.pizza/m/5zUWP5UsLg-" rel="noopener noreferrer"&gt;Poly Pizza&lt;/a&gt;, &lt;a href="https://creativecommons.org/licenses/by/3.0/" rel="noopener noreferrer"&gt;CC BY 3.0&lt;/a&gt;. Rendered with a checkered pattern in place of its own materials.&lt;/li&gt;
&lt;li&gt;Barycentric coordinates figure from &lt;a href="https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/barycentric-coordinates.html" rel="noopener noreferrer"&gt;Scratchapixel&lt;/a&gt;, "Ray-Tracing: Rendering a Triangle", &lt;a href="https://creativecommons.org/licenses/by-nc-nd/4.0/" rel="noopener noreferrer"&gt;CC BY-NC-ND 4.0&lt;/a&gt;. Used unmodified.&lt;/li&gt;
&lt;li&gt;Peter Shirley, Trevor David Black and Steve Hollasch, &lt;a href="https://raytracing.github.io/books/RayTracingTheNextWeek.html" rel="noopener noreferrer"&gt;&lt;em&gt;Ray Tracing: The Next Week&lt;/em&gt;&lt;/a&gt;, chapter 4.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;From a path tracer I'm building in C++. Previously: &lt;a href="https://dev.to/ibukunsanni/a-hexagonal-aperture-would-break-every-diffuse-surface-in-my-renderer-3874"&gt;a hexagonal aperture&lt;/a&gt; and &lt;a href="https://dev.to/ibukunsanni/apparently-bland-2d-grey-squares-are-important-to-my-3d-renderer-21fo"&gt;the white furnace test&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>graphics</category>
      <category>cpp</category>
      <category>raytracing</category>
      <category>blender</category>
    </item>
    <item>
      <title>Apparently, Bland 2D Grey Squares Are Important to My 3D Renderer</title>
      <dc:creator>Ibukun Sanni</dc:creator>
      <pubDate>Thu, 22 Oct 2026 14:38:50 +0000</pubDate>
      <link>https://dev.to/ibukunsanni/apparently-bland-2d-grey-squares-are-important-to-my-3d-renderer-21fo</link>
      <guid>https://dev.to/ibukunsanni/apparently-bland-2d-grey-squares-are-important-to-my-3d-renderer-21fo</guid>
      <description>&lt;p&gt;&lt;em&gt;The white furnace test: like the Chūnin exams, handing in a blank page is how you pass.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqttv4q9grjay4ks4gsca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqttv4q9grjay4ks4gsca.png" alt="A flat grey square." width="256" height="256"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;A furnace render: an albedo-1 sphere inside a uniform environment of radiance 0.5.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;There’s a sphere in that picture. Can you see it? No, you can’t. If you could, my renderer would be wrong, and I wouldn’t like that.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the furnace test?
&lt;/h2&gt;

&lt;p&gt;Put a sphere of albedo 1 (a surface that absorbs nothing) inside an environment of radiance 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 in every direction. Every camera ray either misses the sphere or hits it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;P&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
: a miss reads 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. It escapes straight to the environment.&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;Q&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
: a hit bounces until it misses, so it reads 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 too.&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;R&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
: the square is flat grey. Every pixel reads 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, so the sphere has no edge.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;P&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;∧&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;Q&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;⇒&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;R&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqz5x7hu44lnypg6b0har.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqz5x7hu44lnypg6b0har.png" alt="A diagram: a camera on the left, a grey sphere on the right with light arriving from all around it, and two orange rays, one missing the sphere and one bouncing off it." width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Two camera rays in the furnace. One misses the sphere and reads the environment, L. The other hits the sphere, keeps all of its energy, bounces, and escapes to the same environment: 1 × L = L. Both pixels read L, so the sphere has no edge.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;The test runs it backwards. If I can see the sphere, 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;R&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is false, so 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;P&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 or 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;Q&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is. 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;P&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 can't be, so 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;Q&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is: the renderer is losing energy if the sphere is darker, and making it if the sphere is brighter. If the furnace test is working, you see nothing. Naruto would be proud.&lt;/p&gt;

&lt;p&gt;To see a real-world example of this, I highly recommend Julien Guertault's &lt;a href="https://lousodrome.net/blog/light/2023/10/21/the-white-furnace-test/" rel="noopener noreferrer"&gt;"The white furnace test"&lt;/a&gt;. It opens with a ski-slope photo that matches what 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;R&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 predicts.&lt;/p&gt;
&lt;h2&gt;
  
  
  One place to look
&lt;/h2&gt;

&lt;p&gt;In my renderer, every material has the same three functions, the same split as pbrt's &lt;code&gt;BxDF&lt;/code&gt; (&lt;code&gt;f&lt;/code&gt;, &lt;code&gt;PDF&lt;/code&gt;, &lt;code&gt;Sample_f&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// How much light goes from `out` to `view_dir`: the BRDF, f_r.&lt;/span&gt;
&lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt; &lt;span class="n"&gt;Eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;view_dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;HitRecord&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// The density Sample() draws `out` with.&lt;/span&gt;
&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;Pdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;view_dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;HitRecord&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Draw a direction, and report its pdf and BRDF.&lt;/span&gt;
&lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt; &lt;span class="n"&gt;Sample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Rng&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;view_dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;HitRecord&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                 &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;pdf_out&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;brdf_out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Each bounce, the renderer multiplies the ray's energy by one number built from them:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;w&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;θ&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;w&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
: the weight, the fraction of the ray's energy that survives the bounce.&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
: the BRDF, &lt;code&gt;Eval()&lt;/code&gt;. How much light the surface sends from the new direction toward the old one.&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;θ&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
: the cosine of the angle between the new direction and the surface normal. Light arriving at a slant is spread over more surface.&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
: the pdf, &lt;code&gt;Pdf()&lt;/code&gt;. How likely &lt;code&gt;Sample()&lt;/code&gt; was to pick that direction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's the only way a material touches a ray, so it's where every energy bug lives. For a white diffuse surface, 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;w&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 should be exactly 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  What failing looks like
&lt;/h2&gt;

&lt;p&gt;Seeing a correct test isn't enough, because a correct test shows literally nothing. So I broke the diffuse material to see what failure looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0vfqomundqbjq6w5p17w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0vfqomundqbjq6w5p17w.png" alt="Four grey squares. The first is flat. The second has a darker disc, the third a white disc. The fourth is solid white." width="800" height="239"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;The furnace with a correct material, and with two classic bugs. Counting the cosine twice makes the weight ρ·cos, which averages 2/3, so the sphere drops from 128 to 85. Forgetting the 1/π makes the weight πρ, and the sphere clips to white. At radiance 1, that same bug is invisible.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A cosine counted twice&lt;/strong&gt; makes 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;w&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 average 2/3, so the sphere should read 128 × 2/3 = 85. The render says 85.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A forgotten 1/π&lt;/strong&gt; makes 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;w&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;π&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, so the sphere should read &amp;gt; 255. A byte stops at 255, so it clips to white.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The last panel&lt;/strong&gt; is the same bug at full brightness. The background is already 255, the sphere clips to 255 too, and the bug hides. That's why I render at half brightness, where the target is 128 with room on both sides. &lt;em&gt;Bland 2D Grey Squares Are Important to My 3D Renderer&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Every material gets the same test
&lt;/h2&gt;

&lt;p&gt;Nothing in 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;P&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 or 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;Q&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 says what the sphere is made of, so the same scene checks every material.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu62299sth8np7lewszy2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu62299sth8np7lewszy2.png" alt="Three grey squares. The first is flat, the second has a faint dark ring, the third a barely visible speckled ring." width="800" height="318"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;The same furnace with three other materials. The mirror vanishes exactly. The rough metal leaves a dark ring at its silhouette. The glass vanishes on average, with a few noisy pixels at its rim.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mirror:&lt;/strong&gt; vanishes exactly. It sends all the light one way, and every way looks the same.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rough metal:&lt;/strong&gt; a dark rim, and that's correct. At grazing angles some reflections get nudged into the surface and are absorbed. It can lose energy but never make any, so its test is "nothing brighter than 128".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Glass:&lt;/strong&gt; vanishes on average (127.9). Its radiance goes up by η² going in and back down coming out, so the two cancel out.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Limits of the furnace
&lt;/h2&gt;

&lt;p&gt;It only measures how much light comes back, not where it went. A material that scatters the right amount in the wrong directions still vanishes. I found that out with a hexagon-shaped sampler in &lt;a href="https://dev.to/ibukunsanni/a-hexagonal-aperture-would-break-every-diffuse-surface-in-my-renderer-3874"&gt;a previous post&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I do now
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Render the furnace for every new material. &lt;/li&gt;
&lt;li&gt;Predict the failure. If the math says 85, the render should too.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Just for fun: the Byakugan (because of all the Naruto references)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmn3nrrzq0c8kp92bya0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmn3nrrzq0c8kp92bya0b.png" alt="A pale lavender eye with no clear pupil and faint veins, almost vanishing into a white background." width="512" height="512"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;The furnace at full brightness with an eye painted on the sphere.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Julien Guertault, &lt;a href="https://lousodrome.net/blog/light/2023/10/21/the-white-furnace-test/" rel="noopener noreferrer"&gt;"The white furnace test"&lt;/a&gt;, 21 October 2023.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;From a path tracer I'm building in C++.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>graphics</category>
      <category>cpp</category>
      <category>testing</category>
      <category>math</category>
    </item>
    <item>
      <title>A Hexagonal Aperture Would Break Every Diffuse Surface in My Renderer</title>
      <dc:creator>Ibukun Sanni</dc:creator>
      <pubDate>Sat, 26 Sep 2026 16:28:46 +0000</pubDate>
      <link>https://dev.to/ibukunsanni/a-hexagonal-aperture-would-break-every-diffuse-surface-in-my-renderer-3874</link>
      <guid>https://dev.to/ibukunsanni/a-hexagonal-aperture-would-break-every-diffuse-surface-in-my-renderer-3874</guid>
      <description>&lt;p&gt;I have experimented with a lot of features on my raytracing journey. I played with reflections, implemented shadows, and lost braincells getting scenes to render. Finally, I implemented defocus blur.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fffxsylhbu05uxhw6jxzw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fffxsylhbu05uxhw6jxzw.png" alt="Three spheres receding from the camera. The near red one is sharp; the middle green and far blue ones are soft." width="400" height="225"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;My defocus blur, focused on the near plane&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;




&lt;p&gt;The image reminded me of the bokeh you see in photos. Sometimes they aren't always round.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2m8sk2tqs08fs6dmonvs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2m8sk2tqs08fs6dmonvs.png" alt="A photograph of out-of-focus night lights in orange, green and blue, each one blurred into a soft overlapping disc." width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Not a render. Photo from &lt;a href="https://digital-photography-school.com/how-to-achieve-nice-bokeh-in-plain-english/" rel="noopener noreferrer"&gt;"Bokeh Effect in Photography: A Guide to Gorgeous Background Blur"&lt;/a&gt; by Annie Tao, Digital Photography School.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;So I tried it with what I already had: two passes of the same scene, one circular and one hexagonal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsi7q7vx6fqofgpuf88d6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsi7q7vx6fqofgpuf88d6.png" alt="Two renders side by side, labelled Disk aperture and Hexagonal aperture: the same scattered out-of-focus highlights, round on the left and six-sided on the right." width="800" height="259"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Same scene, different sampler&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;At that point I felt like I could turn anything into bokeh. I could have hearts, stars, concentric circles. Why not push it further?&lt;/p&gt;

&lt;p&gt;Then I noticed something. Every diffuse surface in my renderer would suffer.&lt;/p&gt;




&lt;p&gt;Here is the primitive that both my lens sampler and diffuse surface lean on. It rejection-samples a square until a point lands inside the unit disk, and that test is the only reason the shape comes out round.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec2&lt;/span&gt; &lt;span class="nf"&gt;SampleUnitDisk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Rng&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And here is what the hexagonal change looked like:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Regular hexagon inscribed in the unit circle: corners at radius 1, flat top&lt;/span&gt;
&lt;span class="c1"&gt;// and bottom, flat edges at the apothem sqrt(3)/2 ~= 0.866.&lt;/span&gt;
&lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;kApothem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.8660254f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec2&lt;/span&gt; &lt;span class="nf"&gt;SampleUnitDisk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Rng&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// WAS: if (x * x + y * y &amp;lt;= 1.0f) return glm::vec2(x, y);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fabs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;kApothem&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fabs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kApothem&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.5f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;kApothem&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fabs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kApothem&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;0.5f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;kApothem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The two separate call sites:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The camera: a physical aperture.&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec2&lt;/span&gt; &lt;span class="n"&gt;lens_uv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SampleUnitDisk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;cfg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;aperture_radius&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// The diffuse BSDF wrapper&lt;/span&gt;
&lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt; &lt;span class="nf"&gt;CosineWeightedHemiSphereSurface&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Rng&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                                 &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                                 &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;tangent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                                 &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;binormal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec2&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SampleUnitDisk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0f&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;tangent&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;binormal&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why do my BSDF and my lens share a disk sampler?
&lt;/h2&gt;

&lt;p&gt;Fair question. If testing, or more fittingly, playing with my lens can break my diffuse surfaces, why the coupling?&lt;/p&gt;

&lt;p&gt;The BSDF side is &lt;strong&gt;Malley's method&lt;/strong&gt;. Sample a point inside a unit disk, then project it along the normal onto a hemisphere originating from the ray's intersection with the surface. That is how you implement a diffuse surface.&lt;/p&gt;


  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnr41zecj7obvcwqe9u5q.png" alt="Points scattered on a flat disk, each joined by a vertical line to the point directly above it on a hemisphere resting on the same plane." width="799" height="198"&gt;&lt;em&gt;The lift, drawn. Every point on the disk rises straight up to the dome above it. Figure A.10 from &lt;a href="https://www.pbr-book.org/4ed/Sampling_Algorithms/Sampling_Multidimensional_Functions" rel="noopener noreferrer"&gt;Physically Based Rendering: From Theory to Implementation&lt;/a&gt;, 4th ed., by Matt Pharr, Wenzel Jakob and Greg Humphreys.&lt;/em&gt;
  


&lt;p&gt;The lens side never heard of Malley. Randomizing around the origin allows the ray to focus on a particular plane. Near and far objects blur out, while whatever sits on that plane stays in focus. Guess how we randomize around the origin. Exactly!! We sample within a unit disk.&lt;/p&gt;


  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F239qhzwdjf24vs8nzm1k.jpg" alt="Hand-drawn diagram: three rays leave three separate points on a lens and converge on a single point of a virtual film plane standing at the focus plane." width="800" height="365"&gt;&lt;em&gt;"Camera focus plane", Figure 1.22 of &lt;a href="https://raytracing.github.io/books/RayTracingInOneWeekend.html#defocusblur" rel="noopener noreferrer"&gt;Ray Tracing in One Weekend&lt;/a&gt; by Peter Shirley, Trevor David Black and Steve Hollasch, released under CC0.&lt;/em&gt;
  




&lt;p&gt;So both sides have to sample a unit disk, and voila; one function, two callers. Apparently PBRT does it the same way, so I will take that as great minds thinking alike.&lt;/p&gt;


&lt;h2&gt;
  
  
  I only caught it by reading the code
&lt;/h2&gt;

&lt;p&gt;So how did I catch it? I read the code, because I wrote it. Nonetheless, I cannot rely on that catch. I could easily have missed it, someone opening the file a year from now could miss it too, and neither of us would know the diffuse was off until much later.&lt;/p&gt;

&lt;p&gt;Looking at the render does not help either. I can make the hexagon, sure, but the diffuse barely moves.&lt;/p&gt;


  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdvzf82gi4vq5e3p01z94.png" alt="Two renders of the same all-diffuse Cornell box side by side, labelled Disk sampler and Hexagon sampler, and indistinguishable by eye." width="799" height="438"&gt;&lt;em&gt;Every surface in that box is Lambertian, so there is nothing else to look at.&lt;/em&gt;
  


&lt;p&gt;Do you notice the difference? Do you see the shadows shift? Yeah, neither do I. Without the two frames side by side I would have walked straight past this.&lt;/p&gt;

&lt;p&gt;So I said to myself, "What can I actually check for?"&lt;/p&gt;

&lt;p&gt;Check that it is still a circle? I can only answer that by looking at what the sampler &lt;em&gt;draws&lt;/em&gt; rather than the lines that produce it.&lt;/p&gt;


&lt;h2&gt;
  
  
  Any sampler is correct as long as the &lt;code&gt;Pdf()&lt;/code&gt; tells the truth
&lt;/h2&gt;

&lt;p&gt;So start with the shape mine draws, and why it works at all. It works because solid angle projects onto the disk with a factor of cosine, 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;d&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;θ&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;d&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ω&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, so a uniform disk sample lifted to the hemisphere has density&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;p&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ω&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;π&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;θ&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;



&lt;p&gt;For a Lambertian surface, whose BRDF is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ρ&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;π&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, the estimator then cancels completely:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;p&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ω&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;θ&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;θ&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;π&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ρ&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;π&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;θ&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ρ&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;Each bounce multiplies output by exactly the &lt;em&gt;albedo&lt;/em&gt;, &lt;code&gt;ρ&lt;/code&gt; (the fraction of light a surface sends back, [0,1]).&lt;/p&gt;

&lt;p&gt;Read that derivation again, though, and notice what it never asks for. It never asks for a disk.&lt;/p&gt;

&lt;p&gt;Which means I can build the same lobe without one. Here is the road &lt;em&gt;Ray Tracing in One Weekend&lt;/em&gt; takes: stand at the tip of the normal, push out by a random unit vector, and normalise where you land.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vec3&lt;/span&gt; &lt;span class="n"&gt;dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;glm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;normal&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;RandomUnitVector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That one draws from a ball. It never touches a disk, and it arrives at the same cosine lobe anyway. Two roads, one distribution and I can compare without knowing in advance where the distribution is supposed to be. Get it memorized.&lt;/p&gt;
&lt;h3&gt;
  
  
  The lift, and what the hexagon does to it
&lt;/h3&gt;

&lt;p&gt;Malley's method is one picture. Put the shape underneath the hemisphere like a floor plan, and let every point on it rise straight up to the dome. The height it lands at is cos θ. The cloud of directions you end up with is the &lt;strong&gt;lobe&lt;/strong&gt;: the fat dome.&lt;/p&gt;


  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fke63miz3aj2dllve6i28.png" alt="Two oblique drawings of a hemisphere. Under the left one a disk, under the right a hexagon, with vertical lines lifting floor points up onto the dome." width="800" height="344"&gt;&lt;em&gt;Same dome, same straight-up lift, same horizon, and in both the rise is cos θ. The only thing that differs is the rim. The disk's sits on the horizon the whole way round; the hexagon's plunges there at six corners and arches up to cos θ = 0.5 midway along each edge.&lt;/em&gt;
  


&lt;p&gt;Look at the hexagon in that diagram and notice how little moved. Every point still lands above the horizon, the centre still maps to the normal, and it is still a fat lobe crowding around it.&lt;/p&gt;

&lt;p&gt;What moves is the rim, and only the rim. A hexagon's corners reach r = 1, but its edges cut in to the &lt;em&gt;apothem&lt;/em&gt; (as close as an edge ever gets to the centre) = √3/2 ≈ 0.866, and √3/2 lifts to cos θ = 0.5. It's SOH CAH TOA again. So the grazing directions survive in six chunks and are gone the rest of the way round.&lt;/p&gt;

&lt;p&gt;Which is enough to price the damage before anything runs, because the lift hands every question about the lobe back to the floor plan. Malley over a region 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;R&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 of area 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 gives density 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;θ&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, so every moment is an integral over the shape:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathbb"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;θ&lt;/span&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop op-symbol large-op"&gt;∫&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;R&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size1"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose delimcenter"&gt;&lt;span class="delimsizing size1"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;span class="mord mtight"&gt;/2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;d&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;



&lt;p&gt;Read 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathbb"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;⋅&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 as "&lt;em&gt;the average of&lt;/em&gt;". At 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;k&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 we lose the square root entirely:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathbb"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;θ&lt;/span&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathbb"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;so the second moment is the floor plan's mean squared radius. At 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;k&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, one polar integral per edge, and for a hexagon it closes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Floor plan&lt;/th&gt;
&lt;th&gt;Area&lt;/th&gt;
&lt;th&gt;&lt;code&gt;E[cos]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;E[cos²]&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;disk &lt;em&gt;(correct)&lt;/em&gt;
&lt;/td&gt;
&lt;td&gt;π ≈ 3.142&lt;/td&gt;
&lt;td&gt;2/3 ≈ 0.6667&lt;/td&gt;
&lt;td&gt;1/2 = 0.5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;hexagon&lt;/td&gt;
&lt;td&gt;3√3/2 ≈ 2.598&lt;/td&gt;
&lt;td&gt;3π/4 − 8√3π/27 ≈ &lt;strong&gt;0.7439&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;7/12 ≈ 0.5833&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;hexagon, off by&lt;/td&gt;
&lt;td&gt;−17.3% of the area&lt;/td&gt;
&lt;td&gt;+0.077&lt;/td&gt;
&lt;td&gt;+1/12 ≈ 0.083&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Nothing is specific to hexagons. Hand it a square, a heart, a five-pointed star, and the same two integrals price that sampler too. And the other road has no floor plan at all, which is the point of it: built out of &lt;code&gt;SampleUnitBall&lt;/code&gt;, it sits on the disk's row, 2/3 and 1/2, exactly, whatever shape the lens becomes.&lt;/p&gt;

&lt;p&gt;So we currently have a few predictions in before a single test run: 0.744 against 0.667 with a gap of 0.077.&lt;/p&gt;

&lt;p&gt;Get that memorized, also.&lt;/p&gt;




&lt;h2&gt;
  
  
  So I built the bug on purpose
&lt;/h2&gt;

&lt;p&gt;I reverted to a hexagon in &lt;code&gt;SampleUnitDisk&lt;/code&gt; and ran my tests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2bc0b0kwcrbf5b2udnhq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2bc0b0kwcrbf5b2udnhq.png" alt="Left: the unit disk with a hexagon inscribed in it, the six slivers between them shaded. Right: the density of cos θ after the lift, the disk tracking the exact cosine lobe and the hexagon stepping up at cos θ = 0.5." width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;The disk (blue) lands on the exact cosine lobe (grey); the hexagon (orange) does not. Its step at cos θ = 0.5 is the apothem, lifted, and the two markers on the axis are the means: 2/3 and 0.744.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Check&lt;/th&gt;
&lt;th&gt;Compares&lt;/th&gt;
&lt;th&gt;Real sampler&lt;/th&gt;
&lt;th&gt;Hexagon&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;10 render tests (6 furnace)&lt;/td&gt;
&lt;td&gt;pixels vs environment&lt;/td&gt;
&lt;td&gt;pass&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;pass&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mean cosine&lt;/td&gt;
&lt;td&gt;vs 2/3&lt;/td&gt;
&lt;td&gt;0.6663&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.7437 -- fail&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;cos²/pdf&lt;/code&gt; integral&lt;/td&gt;
&lt;td&gt;vs 2π/3 ≈ 2.0944&lt;/td&gt;
&lt;td&gt;2.0943&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;2.3373 -- fail&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The furnace passed&lt;/strong&gt;, as expected. All 10 render tests stayed green, the 6 furnace tests among them. The furnace check essentially checks the radiance, and that was virtually no different.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg8zkyqkcdav6bnt8wro1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg8zkyqkcdav6bnt8wro1.png" alt="Three panels: the furnace rendered with the disk sampler, the same furnace with the hexagon sampler, and a dark field with rings marking the nine pixels that differ between them." width="799" height="327"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Correct looks like this. So does broken. The third panel is every pixel that moved between them → 9 of 65,536, one byte each.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;The two below it check shape (for a Lambertian the second is π times the first on average; the two tests draw separate samples) and both failed. In line with our prior prediction. Remember the 0.744 number. Yeah, we are in business now.&lt;/p&gt;




&lt;h2&gt;
  
  
  The test that caught it
&lt;/h2&gt;

&lt;p&gt;Let's dive deeper into that test. Before making the change I had already written a test that builds the cosine lobe that way, and it draws from &lt;code&gt;SampleUnitBall&lt;/code&gt;, not &lt;code&gt;SampleUnitDisk&lt;/code&gt;, so it cannot move when the aperture does. Two constructions of one distribution have to agree on every moment, which makes them a &lt;strong&gt;differential test&lt;/strong&gt;: no reference renderer, no known answer needed. Over 2 million samples each:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;E[cos]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;E[cos²]&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Malley (disk lift)&lt;/td&gt;
&lt;td&gt;0.66634&lt;/td&gt;
&lt;td&gt;0.49964&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;normalize(n + RandomUnitVector())&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0.66662&lt;/td&gt;
&lt;td&gt;0.49999&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;hexagon lift&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.74374&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.58309&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;exact, cosine lobe&lt;/td&gt;
&lt;td&gt;0.66667&lt;/td&gt;
&lt;td&gt;0.50000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;exact, hexagon lobe&lt;/td&gt;
&lt;td&gt;0.74393&lt;/td&gt;
&lt;td&gt;0.58333&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We can see from the table above that we were right to 3 significant figures, and the gap that is left is within sampling noise. In other words, we were still right. The earlier predictions for both &lt;code&gt;E[cos]&lt;/code&gt; and &lt;code&gt;E[cos²]&lt;/code&gt; match up.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I do differently now
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Share on mechanism, never on coincidence.&lt;/strong&gt; I will still share primitives. The ideal move is creating new primitives for specific cases. I already knew that. Utilizing a proper separation of concerns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the shape, not just the total.&lt;/strong&gt; The furnace passed, and it measures totals. I still needed to test the shape.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build the bug on purpose.&lt;/strong&gt; You just might learn something.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The shapes I wanted in the first place
&lt;/h2&gt;

&lt;p&gt;The lesson was never "don't shape the aperture." It was "give the lens its own sampler first." So that is what I did, and then I spent an evening on the part that has no excuse.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffdfwedc6mre9s1ezwliq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffdfwedc6mre9s1ezwliq.png" alt="Four renders in a 2x2 grid, labelled disk, star, heart and crown: the same field of out-of-focus highlights, each one blurred into the shape its label names." width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;em&gt;Same field, same seeds, four openings. Only the rejection test moved.&lt;/em&gt;
  &lt;p&gt;&lt;/p&gt;

&lt;p&gt;Look at all those images. "chef kiss." This was really all I wanted and it could have broken such a key section of my ray tracer. Sometimes that is the price of having one.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;From a path tracer I'm building in C++. Next: getting total internal reflection wrong in four different ways before getting it right.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>graphics</category>
      <category>cpp</category>
      <category>testing</category>
      <category>math</category>
    </item>
  </channel>
</rss>
