Last updated: 2026-11-26
A ray tracer, from the film down
For new readers
A ray tracer written in PatLang, with its geometry in the conformal geometric algebra Cl(4,1), and a renderer for the graphics card that gives the same pictures. The pages start from the finished work, a short film of a rabbit in a meadow, and open it one box at a time: what one picture needs, what is in the scene, the shapes and surfaces those things are made of, the numerical methods underneath, and at the bottom the algebra of points and motions. Each page says which boxes below it it uses, and links to them, so a page can be read on its own and the details looked up only when they are wanted.Cl(4,1) handles spheres and planes uniformly
The project began by following Jamis Buck's The Ray Tracer Challenge (Pragmatic Bookshelf, 2019), whose chapters set the order in which the renderer was first built: tuples, canvas, matrices, transformations, rays and spheres, shading, the camera, shadows, planes, patterns, reflection and refraction, cubes, cylinders and cones, groups, triangles and CSG. These pages do not reproduce the book: the code, the tests and the text here are this project's own, and the later pages (the torus, polynomial and function surfaces, texture and bump maps, the scene language, plants, animals, fog, mirages, stereo pictures, threads, the graphics card and the film) go beyond it.
The pages
I. The goal
II. Making one picture
- The camera and the world it looks at
- Light at a point, and shadows
- Mirrors and glass
- Air: fog, mist and mirages
- Two eyes: stereo pictures
- One model, many threads, and the graphics card
- Making it fast without changing a pixel
III. What is in the scene
- A language for scenes and models
- Trees, plants and a garden
- Animals: a bird, a rabbit and a fox
- Built objects: solid geometry and a Dalek
IV. Shapes
- Rays, spheres and planes
- Boxes, cylinders and cones
- Triangles and meshes from files
- Groups and trees of boxes
- The torus and other polynomial surfaces
- Surfaces of any function
V. Surfaces
VI. Numbers underneath
VII. Foundations
- Points, directions and motions in the conformal algebra
- Matrices
- Colours, the canvas and picture files
The tests are a tree
The tests have the same shape as the pages. Every check is a node of one tree
(tests/run.patlang); a group of checks passes when all its parts do, when any one of
them does (two methods for the same job, either of which is enough), or when all of them
do and give the same answer (a plain method and a faster one, which must agree to the
last bit). A node can also need nodes elsewhere in the tree: the film needs the picture
and the scene, the scene needs its shapes and surfaces, and so down to the algebra. So
the top of the tree passes only when everything it rests on does.
> v2_interp.exe tests/run.patlang --node film --depth 1
[ ok ] The film: a rabbit in a meadow
[ ok ] Frames of the film
checks: 3 passed, 0 failed, 0 blocked, 0 skipped, 0 not run
ALL PASSED
Each page ends with its own part of the tree. To run everything, from the root of the repository (the branches run as parallel processes and are merged):
tools/test.sh # everything
tools/test.sh --quick # without the slowest checks (threads, plants, the rig, fur)
v2_interp.exe tests/run.patlang --node shapes/sphere --depth 4
Literal expected values come from three places, none of them the code under test: a
property that must hold (a round trip, an identity, a symmetry), a case worked out by
hand, or tools/oracle.py, which computes them with exact fractions and with a small
ray tracer of its own and writes them to tests/tree/oracle_values.patlang.
tools/mutation_check.sh breaks the library on purpose and checks that the tree notices.the oracle uses exact math so the test has no error
Running it
The code runs on PatLang v2: the interpreter self_hosting/build/v2_interp.exe of the
PatLang repository (named by PATLANG_V2 in the shell tools), and the compiler
self_hosting/build/v2.exe (tools/compile.sh). Each page is built from its .md.in
file by tools/mkdoc.sh, which puts in the real code and the real output of every
command shown.
Graduate Attributes
Directly developed
- Digitally Fluent: Readers use provided shell scripts to compile code and run a hierarchical test suite that validates numerical methods against exact fractions.
- Self-Directed Learner: Readers navigate a modular documentation structure that links dependent components, allowing them to look up specific details only when required.
Modelled on this page
- Researcher: The project cites its methodological origins in an existing challenge while explicitly distinguishing its own original code and tests from the source material.
- Subject Specialist: Complex mathematical concepts such as conformal geometric algebra are systematically decomposed into foundational numerical methods and algebraic primitives.
- Critical Thinker: Reliability is verified through mutation testing that deliberately breaks the library to ensure the test suite detects failures.
University of Reading Graduate Attributes framework.