Lua Ray Tracer

Lua Ray Tracer
Login

I've never done much graphics programming, but I've always wanted to explore it. I think the most common approach is to go straight to OpenGL, but I tend to approach a domain from first principles, especially when I'm coding for fun, so I turned to Amazon and found Ray Tracing in One Weekend for $3. It tackles everything in C++, and I thought it would be a fun exercise to follow the algorithms laid out in the book, and I could focus on learning how ray tracing works while porting all the code to Lua.

I started work on this "one weekend" project in November 2019, and I'm still working on it a year later, primarily because I work on it only for a few days at a time, typically for an hour or so a day. But I've also set a goal to write up all my decisions as I go, which is at least double the work of simply implementing the original chapter-by-chapter. Since I started, the author, Peter Shirley, has released the entire contents of all three books in the series as CC0 on GitHub. In some ways, this is a setback for my work here, since much of my commentary here is about aspects of the original Kindle version that were missing or unclear. My plan is to switch mid-stream to the updated version, incorporating changes and merging them with my own as I go. The API and design changes I'm making as I port to Lua are likely to be retained in some form, however, so the value of my effort isn't completely erased.

Lua might seem like exactly the wrong choice for ray tracing, which requires high performance and lends itself to a language that can parallelize the computation. But I enjoy working in small languages that use few data structures to accomplish many things, so I'm curious to see how far I can get in Lua. I'm going to stick with luajit, however, as it is quite a bit faster than lua5.3. As such, the code will be lua5.1 compatible, since that's the version of the language luajit targets.

I've organized by notes by chapter, as the original does.

  1. Chapter 1 - Graphical Hello World
  2. Chapter 2 - The Vector Class
  3. Chapter 3 - Rays, Camera, Background
  4. Chapter 4 - Adding a Sphere
  5. Chapter 5 - Surface Normals and Multiple Objects
  6. Chapter 6 - Antialiasing
  7. Chapter 7 - Diffuse Materials
  8. Chapter 8 - Metal