A CPU ray tracer in rust
README.md

Ray tracer #

An implementation of Ray Tracing In One Weekend in Rust as an exercise. It largely follows the structure from the book with a completely original implementation. It includes several modifications to better fit my programming style with Rust and also some new features or changes that I thought would be interesting, which are detailed below.

Modifications #

  • Realistic lens model: While it is equivalent to the model from the book this more accurately reflects the behaviour of camera lenses and enables using those parameters more easily, e.g. focal length instead of field of view. While these could be translated between models I do like emergent behaviour so I did this instead.
  • Multithreading: A couple of multithreaded renderer implementations are provided using the basic standard library primitives. I'm also looking into other crates (tokio and rayon) to see if they offer any benefit for this implementation.
  • TUI application: You can render on the terminal, because it's fun!

Dependencies #

While the project aims to be as dependency-free as possible (after all this is an exercise) I have ended up some dependencies to handle features that I'm not prioritising yet so I can focus better on rendering right now. The current dependencies are:

  • Image: for loading images in a variety of formats and eventually writing them too.
  • Ratatui + crossterm: for implementing the terminal interface.
  • Clap: for parsing commands and parameters.
  • Random: for random value generation.

Some of these dependencies aren't critical and I'll try to remove them when it's convenient to do so. All the ray tracing related features remain dependency-free. These include:

  • Linear algebra.
  • Geometric primitives.
  • Material behaviour.
  • BVH data structure.
  • Ray tracing algorithm itself.

Usage #

The compiled binary operates in one of two modes: render or tui, one of which must be passed as the first argument. Tui mode renders directly to the terminal, and (eventually) provides an interface to change settings. Render mode outputs in ppm format to stdout and can be piped to a file to save the result to disk; it also accepts a number of parameters which can be checked by using the --help flag.

Future additions #

Some ideas for features to add in the future, in no particular order:

  • Scene editing (and render config editing) in tui mode.
  • Import 3D meshes
  • Serialize scenes to text files and import them
  • Use light wavelengths instead of rgb colours
  • GPU implementation
  • Add lenses with non linear rays
  • Related to above: simulate photons instead of rays that can be affected by forces