3D Renderer, Written In Rust

If one can't write a 3D renderer in a language, then one cannot claim to "know" it. So, in my process to "learn Rust" I wrote a 3D renderer that can handle simple geometries. It is featured enough to have a face shader, but it never got a proper line drawing algorithm. Instead of worrying about video, raw frame data is written over STDIO and captured/displayed with mplayer. When possible, I always prefer to leverage shell features than to write my own software.

Code for this project can be downloaded here. The readme file should contain all the information needed to build and view, and was last tested working on 2019-04-13.

Models

In order to write a 3D rasterizer, it is a good idea to know some metaphors for good object orientation. If we can define through language the scene, it makes it much easier to reason about.

The program runs through a loop to rasterize each frame, according to a sixty frames-per-second target rate. A Camera object, seeing Shapes made of Points with its Sensor, is able to calculate which points are closest in Z, and shade the faces accordingly. The scene objects are implemented with structs that have transformative functions, which is how the rotations in the video above are able to happen. Through the main program loop, scene objects are transformed and translated, and at the end the program dumps the information from the camera's sensor.

My particular camera acts like a pinhole camera. It is the simplest way of achieving perspective. Scene rasterization is handled by the camera's sensor.

Go home