I spent a weekend with a path tracer

This is definitely not how I was planning to spending my weekend (don’t get me wrong, I was going to work on some other things. What else would I do? Go outside and play with the other kids?)


It was Friday evening and I was doing a mandatory email check before leaving work when I saw it: one email from Amazon with some book recommendations, including the following one:

Go ahead, click the image and check out the book. I’ll wait

The goal of this book by Peter Shirley is to implement a basic path tracer in C++ in just a couple of days. I didn’t pay too much attention at the moment, other than it was quite cheap and got several goods reviews. But it stuck in my head for the rest of the night and I bought it as soon as I got home.

And that, kids, is how I meet you mother spent the weekend: in between math and a lot of waiting for low-resolution images to be rendered. I mean, A LOT of waiting. What an amazing weekend!

Honestly, it was an enlightening experience. The book is well written and a lot of fun to read. I’ve worked with ray tracers before, but a path tracer is a bit different. And, even when I follow most of the code examples from the book, implementing it on top of Crimild was a challenge on its own.

My implementation still needs a lot of work, but it produce pretty good images. Take a look:

As it is right now, the path tracer supports diffuse illumination for metallic, lambertian and dielectric materials. I ended up not implementing depth of field, since I’m not interested on that at the moment.

What’s next? Well, it does need some heavy optimizations, of course. The image at the top of this post took about 3.5 hours to render (no, that wasn’t a typo). The good news is that parallelizing the algorithm should’t require too much work. On the other hand, it would be great to render some actual triangles, not just spheres.

The other books in the series look promising and continue to improve the path tracer, so I’ll be giving them a look in the future. Check out Peter Shirley’s blog for more about his books.

As a side effect, being a math-based project, it allowed me to revisit Crimild’s math classes, making them even more robust. That’s pretty old code and somehow there’s always something to fix or improve.

Finally, if you need a good introduction to what a path tracer does, take a look at this excellent video from Disney’s labs

The code is in one of the examples projects. At the time of this writing I haven’t migrated the reusable code to the core library since I still need to do some review and cleanup, but that will have to do for now.