Examples » Ray Tracing new in 2020.06

Simple iterative CPU ray tracing.


Implementation of a simple CPU ray tracer adapted from Peter Shirley's book Ray Tracing in One Weekend. The current implementation runs on single thread and performs iterative rendering to refine the result. Typically, a high quality image can be achieved after around 100 iterations.


  • mouse drag rotates the camera
  • Shift mouse drag pans the camera
  • mouse wheel zooms in/out
  • R resets the camera to its original transformation
  • D toggles Depth-of-Field
  • M toggles marking the next rendered block by a different color
  • N generate a new random scene
  • Space pauses/resumes rendering

Additionally, various options can be set via command line:

  • --block-size PIXELS — size of a block to render at a time (default: 64)
  • --max-samples COUNT — max samples per pixel (default: 100)
  • --max-ray-depth DEPTH — max ray depth (default: 16)


This example was originally contributed by Nghia Truong.


Full source code is linked below and also available in the magnum-examples GitHub repository.

The ports branch contains additional patches for Emscripten support that aren't present in master in order to keep the example code as simple as possible.