A CPU-side 3D renderer that takes a mesh through transformations, clipping, projection, and triangle rasterization. The graphics pipeline is implemented in C; SDL2 handles the window, input, and presentation of the final color buffer.
C99 · SDL2 · Linear algebra · Software rendering
OBJ mesh → World transform → Camera space → Back-face culling
→ Frustum clipping → Perspective projection
→ Triangle rasterization → Color / depth buffers → SDL2
The interesting work is inside the renderer: matrix and vector math, clipping geometry against the view frustum, interpolating texture coordinates, and resolving visibility per pixel.
- Scale, rotation, and translation matrices, plus a movable camera.
- Perspective projection and view-frustum clipping.
- Optional back-face culling.
- Wireframe, filled, and textured triangle modes.
- Perspective-correct texture interpolation and a depth buffer.
- Directional face lighting in the filled-triangle path.
- A small OBJ loader and PNG texture loading through uPNG.
Requires a C compiler, Make, SDL2 development headers/libraries, and a graphical desktop.
git clone https://github.com/coganka/3d-rasterizer.git
cd 3d-rasterizer
make buildScene assets are not included in the repository. Before running, provide the OBJ/PNG pairs referenced by setup() in src/main.c:
assets/runway.obj assets/runway.png
assets/f22.obj assets/f22.png
assets/efa.obj assets/efa.png
assets/f117.obj assets/f117.png
Alternatively, replace the load_mesh(...) calls with your own available files. The loader expects triangulated OBJ faces using positive vertex/texture/normal indices. Run from the repository root so relative asset paths resolve:
make runThe Makefile assumes SDL2 is on the compiler's default search path. If it is installed elsewhere, supply the flags from your SDL2 installation, for example:
cc -Wall -std=c99 -D_DEFAULT_SOURCE $(sdl2-config --cflags) \
src/*.c $(sdl2-config --libs) -lm -o renderer| Key | Action |
|---|---|
1 / 2 |
Wireframe with vertices / wireframe |
3 / 4 |
Filled triangles / filled triangles with wireframe |
5 / 6 |
Textured triangles / textured triangles with wireframe |
C / X |
Enable / disable back-face culling |
W / S |
Move forward / backward |
A / D |
Rotate camera |
| Arrow keys | Move camera horizontally or vertically |
Esc |
Exit |
| Source | Focus |
|---|---|
| main.c | Pipeline stages, scene setup, input, frame loop |
| triangle.c | Rasterization, texture interpolation, depth testing |
| clipping.c | Frustum clipping and polygon triangulation |
| matrix.c, vector.c | Transform and camera math |
| mesh.c | Mesh storage and OBJ/PNG loading |
This is a graphics fundamentals project with a fixed demo scene, not a general-purpose asset pipeline. Mesh and triangle buffers have fixed limits, and asset loading assumes valid input; missing files must be resolved before launching. SDL2 presents the output, while uPNG decodes textures—the PNG decoder is a third-party component, not part of the custom renderer.
The next useful improvements are bundled sample assets, explicit loader errors, and repeatable image-based rendering tests.
src/upng.c and src/upng.h contain uPNG, derived from LodePNG. Original copyright and license notices are retained in those files.