Back to the Arcade

miniRT

42 · Rank 4

Mission Briefing

A ray tracer written from scratch in C. It reads a scene file (camera, lights, spheres, planes, cylinders) and renders it with ambient, diffuse and specular lighting and hard shadows. You can move the objects, the light and the camera with the keyboard.

Skills shown

  • 3D maths
  • File parsing and validation
  • Rendering
  • Team collaboration

Built with

  • C
  • MiniLibX
  • Linear algebra

With a keyboard: click the picture first. W A S D move the camera, the arrow keys and + and - move the objects, T F G H Y U move the light, Z X C V rotate, and 1 and 2 resize. Esc quits the program. Every key moves all objects of that kind at once, as in the original.

This is our original miniRT C code, compiled to WebAssembly. It runs in a background thread, so a complex scene can take a few seconds. The error messages come from my scene parser.

How it works

A .rt file describes the scene, one element per line:

A  0.2          255,255,255
C  0,0,-20      0,0,1   70
L  -40,20,-30   0.7     255,255,255
sp 0,0,20.6     12.6    10,0,255

The parser stops at the first problem with a clear error: an unknown identifier, a second camera, light or ambient line, a ratio outside 0 to 1, a field of view outside 0 to 180, a radius or height that isn’t positive, a malformed colour, and so on. Each pixel then sends a ray from the camera, finds the nearest object it hits, and adds up the ambient light, the diffuse light from the light source unless another object blocks it, and a specular highlight.

The keys act on every object of a type at once: the arrow keys move all the objects, 1 and 2 shrink or grow every sphere and cylinder, and Z, X, C and V rotate all the cylinders or all the planes.

What I learned

Vector maths becomes intuitive once you have to debug it pixel by pixel. And parsing untrusted input is a real job in its own right.

Crew
dgomez-a
My role
I built the scene parser with strict validation of `.rt` files, and the keyboard event system that moves, rotates and resizes objects and re-renders after every change. My teammate built the ray-object intersection and lighting code.