Skip to content
Copied!

Introduction

This is a self-study document covering the following three books by Peter Shirley et al.:

  1. Ray Tracing in One Weekend (v3.2.3)(v.4.0.2)
  2. Ray Tracing: The Next Week (v3.2.3)(v.4.0.2)
  3. Ray Tracing: The Rest of Your Life (v3.2.3)(v.4.0.2)

Ray Tracing in One Weekend is a well-known resource for hands-on programming practice. The original is written in a solid, not particularly modern style of C++11 (using std::shared_ptr). It is widely referenced as a learning resource for both ray tracing and C++, and also serves as a convenient base for porting to other languages. Many people have attempted implementations in popular languages such as Go and Python, and a fair number of brave souls have tackled Rust — a systems programming language notorious for its steep learning curve.

I decided to re-implement it in Rust as well. On top of that, I compile the code to WebAssembly so it runs client-side in the browser, and I display the resulting PPM images inside VitePress using a Vue component. Despite the "One Weekend" in the title, I was nowhere near finished in one weekend — it took me about 7 months (30 weeks) to produce the final scene shown in the banner.

This project required overcoming several hurdles. I have therefore written a separate section explaining how to work with WebAssembly inside VitePress. Starting from scratch with Vue and Vite, I designed a system that displays output to a Canvas via a WebAssembly binary, and I still remember the excitement of seeing that first gradient image appear.

One pleasant side effect of choosing Rust is the compact WASM binary size. raytracing_demos_bg.wasm, which contains all Week 1 demos, weighs in at just 66 KB uncompressed. Rust's zero-cost abstractions and wasm-pack's release build aggressively eliminate dead code, which neatly illustrates how well Rust and WebAssembly complement each other. At this size, loading the binary has no meaningful impact on page load time with today's network conditions.

Structure of This Document

0. Technical Chapters

Two pages cover the technology needed to call raytracing_demos from VitePress. The first page addresses the three-layer Cargo workspace structure (common, r1XX-*, raytracing-demos), crate naming conventions, the procedure for adding a new demo, the build process, and the design rationale behind passing PPM strings from Rust to JavaScript. The second page covers the Vue component PPMRenderer, which receives those PPM strings and draws them to a Canvas inside VitePress, including how the conflict with Vue's reactivity system was resolved. [Read]

1. Ray Tracing in One Weekend

All 13 chapters of Week 1 are covered in order. The original book is written in C++11, and understanding the differences between C++ and Rust is one of the key learning axes of this document. Topics include: representing return values with Option<T> instead of output parameters; using Arc<dyn Trait> instead of shared_ptr; defining pure virtual functions as traits; operator overloading; expression-oriented control flow; and Rust's ownership and borrowing rules — all explained alongside comparisons with their C++ equivalents. [Read]