Lua Ray Tracer

Chapter 1
Login

Chapter 1 - Graphical Hello World

The first step is to produce a PPM file that demonstrates we can create images at all. All it does is alter the red and green channel during iteration over the pixels to create a color gradient. Our image size is defined at the top. In the C++ code, it is locked to a 200px x 100px image, but I found that that scaling it by 3x to 600x300 is more visually engaging, so I switched in the Lua code to reflect that. PPM files are easy to create, but very inefficient. In the spirit of "make it work, make it right, make it fast", I'll worry about optimizing the file format later, and probably only if needed.

#! /usr/bin/env luajit

nx = 600
ny = 300
print(string.format("P3\n%s %s\n255\n", nx, ny))
for j = ny-1, 0, -1 do
   for i = 0, nx-1 do
      r = i/nx
      g = j/ny
      b = 0.2
      ir = math.floor(256*r)
      ig = math.floor(256*g)
      ib = math.floor(256*b)
      print(string.format("%s %s %s", ir, ig, ib))
   end
end

I keep a running Emacs shell buffer alongside my code, and I can run this command to display the image after I change the code:

./ray.lua | convert - chapter1.png && emacsclient -n chapter1.png

The result looks like this:

Next up: Chapter 2 - The Vector Class