Contents

Naive ray tracer implementation in Python

During my first Recurse Center retreat in the fall of 2021, I started learning Python in earnest (along with a little React on the side), worked on code craftsmanship, and started to fill in the gaps of my non-CS background on any aspects of computing I happened across during study sessions or while pairing with fellow Recursers. One tangent led me to writing a ray tracing engine.

The assignment

As an RC exercise, the project was self-directed. I was aware of Ray Tracing in One Weekend, but wanted to see where my intuition would lead me independently. The goal was to use an object-oriented approach to implement a 2D representation of scene illumination based on optical ray trajectories. Extending the naive implementation to a GPU-optimized version would be a natural next step as even mildly complex images were a significant lift for my machine. As an example benchmark, a version of the below image with 0 recursion depth (no Fresnel components) took just shy of 9 minutes to generate (536 seconds), generated with:

  • 800 x 450 pixels
  • 2015 MacBookPro
    • Mojave 10.14.6
    • 2.9 GHz Intel Core i5

GPUs rule

In the course of exploring photorealistic image generation for movies and games, I discovered that the practical execution of algorithms which simulate realistic (physics-based) illumination is made possible by phenomenal advances in dedicated hardware, supported by machine learning.

No GPU, no problem… (?)

I wrote my ray tracer in Python since that was the language I was learning at the time. The logic simulates in-scene light interactions, including diffuse surface scattering (Lambertian) and reflection/refraction (Fresnel, computed recursively).

/images/example_shadowsdiffuse.png
Raytracing from scratch in Python

The code isn’t performance optimized, nor architected to leverage GPU capabilities. I nonetheless got a rapid fire intro to real world implementations of ray and path tracing - there’s some extremely cool research in that space!

… really, though - you need GPUs

Realistic images at high frame rates for games rely on innovative approaches stitched together to meet stringent technical constraints, and exquisite animations and special effects in movies are made possible by a combination of advanced hardware and software supported by dedicated computer clusters (aka render farms). And both applications depend on sophisticated GPU platforms optimized for ray tracing. In the hands of skilled artists, this incredible technology generates delightfully photorealistic images and animations.


This post was originally published in 2021 on an earlier version of this website.