There’s a particular joy in writing a program whose output is a picture. A ray tracer is one of the best examples: a few hundred lines of vector maths, and suddenly there are glass spheres reflecting each other on your screen.

I worked through the first book of Peter Shirley’s Ray Tracing in One Weekend series in Kotlin rather than the book’s C++. The code is on GitHub: mohaqeq/kotlin-raytracer.

The final scene of the first book

The shape of the code

The book’s structure maps nicely onto small Kotlin files:

FileWhat it holds
Ray.ktA ray: origin, direction, and the point at distance t
Hittable.kt, Sphere.ktThings a ray can hit; a sphere is the only primitive the first book needs
Collision.ktThe record of a hit: point, normal, which side of the surface, material
Material.ktHow light scatters: diffuse (Lambertian), metal and dielectric (glass)
Camera.ktField of view, position and defocus blur
Main.ktThe scene and the render loop

Kotlin was a good fit. Data classes and operator overloading make the vector maths read almost like the book’s formulas (a + b * t instead of a.add(b.mul(t))). Sealed hierarchies keep materials honest.

Output: the humble PPM

Like the book, the renderer writes a PPM file (image.ppm): a header followed by plain RGB triples. It’s the simplest image format there is, so there’s no dependency between you and the first picture. Any image viewer, or a one-line conversion, turns it into a PNG.

Making the final scene bearable

The final scene of the first book, with many small spheres, antialiasing samples per pixel and several bounces per ray, is slow when rendered on one core. Rows of pixels are independent, so the render loop hands them to a FixedThreadPool sized to the number of available CPUs, then writes the rows back in order. It is the easiest speed-up in the whole project, and a nice reminder that “embarrassingly parallel” really is a technical term.

Running it

./gradlew run      # writes image.ppm

The state at the end of the first book is tagged v1.0.

What I took away

  • Write the maths first, then the abstractions. The book’s order (rays, then spheres, then materials) keeps every step visible as a picture. You debug with your eyes.
  • Randomness everywhere. Diffuse scattering, antialiasing and defocus blur are all random sampling. Seeing noise shrink as samples go up is the best intuition for Monte Carlo methods I know.
  • Kotlin is pleasant for numeric code, as long as you keep allocations in the inner loop in mind.