Discovering computer graphics
I started this project during my first year of engineering school. I had only recently learned C, and I was discovering computer graphics without having taken a rendering course. I wanted to understand how a 3D scene could be transformed into an image, so I built a small software renderer.
Rather than using a 3D rendering API, the program performs the rendering calculations on the CPU. SDL2 provides the window, input handling and display of the resulting pixel buffer. The renderer loads triangulated OBJ meshes with materials and textures, and supports moving around the scene with a free camera.
Building the rasterization pipeline
I implemented the triangle rasterizer myself, using mathematical formulas and explanations I found online as references. The pipeline transforms mesh vertices relative to the camera, applies perspective projection and converts their positions into screen coordinates.
For each triangle, the rasterizer computes a bounding box and uses barycentric coordinates to determine which pixels lie inside it. Those coordinates are also used to interpolate the data needed to shade each pixel.
Visibility is handled with a Z-buffer: each pixel stores a depth value, and a new fragment replaces it only if it is closer to the camera. This allows overlapping triangles to be drawn without relying on their submission order.
Textures and near-plane clipping
The renderer loads texture coordinates and materials from OBJ/MTL files. To avoid the distortion caused by directly interpolating UV coordinates in screen space, it reconstructs texture coordinates using reciprocal-depth interpolation. This accounts for the perspective projection when mapping a 2D texture onto a 3D surface.
I also implemented near-plane clipping. When a triangle crosses the camera's near plane, the renderer computes intersections along its edges and splits the triangle so that its visible portion can still be rasterized. This was an important part of making the pipeline handle geometry close to the camera, rather than simply discarding every triangle that crosses the plane.
Demo
Circuit model: “gba peach circuit” by amogusstrikesback2.


