A Ray Tracer in PatLang

A ray tracer in PatLang and conformal geometric algebra, opened top down from a short film to the algebra of points, with a graphics-card renderer checked byte for byte against it.

The film: a rabbit in a meadow

The film's own checks are about frames: windows, plates and the timeline. The node also needs the whole picture branch and the whole scene branch, so it passes only when everything the film is made from passes too.

Light at a point, and shadows

A surface normal, a reflection vector, a point light, a material, and the Phong model that combines them. Two places in this page are where geometric algebra replaces something the book has to patch: reflection, andโ€ฆ

Mirrors and glass

Mirrors and glass: the renderer starts spawning secondary rays, so color_at , shade_hit and two new colour functions recurse, with a depth limit to stop two facing mirrors bouncing forever. Both effects are geometryโ€ฆ

Air: fog, mist and mirages

Distance has been invisible so far: a sphere a hundred units away is as sharp and as bright as one at ten. Air is not clear. A ray that crosses it is dimmed and tinted, and the further it goes the more. This page givesโ€ฆ

Two eyes: stereo pictures

Two eyes a few centimetres apart see a scene from two viewpoints, and the brain turns the difference into depth. A pair of pictures, one for each eye, side by side, does the same for anyone who can look at them theโ€ฆ

One model, many threads, and the graphics card

Until now a picture was rendered by many processes : the picture was cut into bands, one process per band, and each process built the whole scene for itself before it rendered a row. The model is not changed byโ€ฆ

Making it fast without changing a pixel

The Dalek took about 90 minutes a picture on 20 processes, and this pages before it were already slow enough to wait for. This page is about the renderer, not new features, and it has one rule: no picture may change.โ€ฆ

A language for scenes and models

Until now a scene has been a PatLang program: calls to sphere() , set_transform , mat_mul , with_material_deep , a page of them. This page gives scenes and models their own small language, in a text file, with theโ€ฆ

Trees, plants and a garden

A tree is a rule used on itself: a branch is a limb, and at the tip of the limb are smaller branches; at the last of them are leaves. The scene language of A language for scenes and models can say that, becauseโ€ฆ

Animals: a bird, a rabbit and a fox

A bird is a better test of the scene language than a tree, because nobody mistakes a bad one for a good one. This page builds a robin with the tools of Surfaces of any function and A language for scenes and models andโ€ฆ

Built objects: solid geometry and a Dalek

Union, intersection and difference of solids. A CSG shape holds two operands, each of which may be a primitive, a group or another CSG shape, so trees of any size can be built from binary operations.

Rays, spheres and planes

Rays, spheres, intersection records, the hit rule, and the first picture that comes from casting rays. In the conformal algebra a sphere is a vector, and "a point lies on a sphere" is a single inner product. The book'sโ€ฆ

Boxes, cylinders and cones

The axis-aligned cube from โˆ’1 to 1 on every axis, with two jobs: intersect a ray with it, and find its normal. In the conformal algebra both are statements about plane vectors, and the cube is six of them.

Triangles and meshes from files

The last primitive, and the one that lets a ray tracer draw anything a modelling program can export: a triangle given by its three corners, a reader for Wavefront OBJ files, and smooth triangles that carry a normal atโ€ฆ

Groups and trees of boxes

A group is a shape with no surface of its own: it is the shapes it holds, carried by one transformation. Nest groups and a transformation placed on the outermost one moves the whole tree. This page also asks forโ€ฆ

The torus and other polynomial surfaces

The book's last chapter lists directions to take a ray tracer next (area lights, spotlights, focal blur, motion blur, anti-aliasing, texture maps, normal perturbation) and leaves them open. Here the next step is a newโ€ฆ

Surfaces of any function

The torus and other polynomial surfaces solved f(x, y, z) = 0 for a polynomial f : along a ray it becomes a polynomial in t , and its roots can be bracketed exactly. A function with a sin , a sqrt , an abs or aโ€ฆ

Patterns

A pattern is a rule that turns a point into a colour: stripes, a gradient, rings and checkers. The pattern has its own transformation, applied after the object's, so a pattern follows its object and can also be scaledโ€ฆ

Pictures on surfaces: texture and cube maps

The patterns of Patterns colour a point from its three coordinates: stripes by x , rings by x and z , checkers by all three. A picture needs something else. A globe wears a flat map, and the map has to know which pointโ€ฆ

Bump and normal maps

A texture map changes the colour of a surface. A bump changes how the surface faces the light: shading depends on the normal, so a picture that tilts the normal makes a smooth sphere look dimpled or a flat wall lookโ€ฆ

Surfaces typed as text

The torus and other polynomial surfaces made a surface from a polynomial, but the polynomial had to be built by calls: tm_pow , tm_sub , tm_scale , a page of them for the heart. This page reads it the way it is writtenโ€ฆ

Points, directions and motions in the conformal algebra

The book represents points and vectors as 4-tuples (x, y, z, w) with w = 1 for points and w = 0 for vectors. Here a tuple is a multivector of the conformal geometric algebra Cl(4,1) , and the book's operations fall outโ€ฆ

Matrices

The book builds 4ร—4 matrices, multiplication, transposition and inversion by cofactor expansion. The interface stays the book's: a matrix is a list of rows and M[r][c] reads an element. What changes is where theโ€ฆ

Colours, the canvas and picture files

Colours, a canvas, and a way to look at the result. The book writes plain-text PPM; this project also writes PNG , so images open anywhere without a converter.