Skip to content

About

A CPU software rasterizer in C++20 - OBJ loading, z-buffering, perspective projection, and Phong shading with diffuse/normal/specular maps. No graphics API, no dependencies.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

graphics-rasterizer

A software rasterizer written from scratch in C++20, with no graphics API and no third-party libraries. It loads Wavefront OBJ models, rasterizes them on the CPU, and writes the result as a TGA image.

CI

African head rendered with Phong shading Diablo 3 model rendered with Phong shading

Features

  • Pure CPU rasterization: bounding-box triangle fill with barycentric interpolation
  • Z-buffering for correct hidden-surface removal
  • Perspective projection and a lookAt camera you can position from the command line
  • Backface culling via the sign of the screen-space triangle determinant
  • Texture mapping: diffuse, object-space normal, and specular maps
  • Four shading modes: Phong, Gouraud, flat, and a depth-buffer visualization
  • OpenMP parallelism across scanline columns, used automatically when available
  • Full TGA support: reads and writes both raw and RLE-compressed 8/24/32-bit files
  • Zero dependencies beyond a C++20 compiler and CMake

Requirements

Compiler Any C++20 compiler: GCC 11+, Clang 14+, or MSVC 19.30+ (VS 2022)
Build system CMake 3.24 or newer
Optional OpenMP for multi-threaded rasterization; Python 3.8+ to convert output to PNG

Building

Linux

git clone https://github.com/raphaeldickinson/graphics-rasterizer.git
cd graphics-rasterizer
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build

Windows

From a Developer PowerShell for VS 2022:

git clone https://github.com/raphaeldickinson/graphics-rasterizer.git
cd graphics-rasterizer
cmake -S . -B build
cmake --build build --config Release

To build with MinGW-w64 instead (CMake and Ninja on your PATH):

cmake -S . -B build -G Ninja -DCMAKE_CXX_COMPILER=g++
cmake --build build

Running the tests

ctest --test-dir build -C Release --output-on-failure

Usage

Run from the repository root:

./build/rasterizer obj/african_head/african_head.obj              # Linux
build\Release\rasterizer.exe obj/african_head/african_head.obj    # Windows, MSVC
build\rasterizer.exe obj/african_head/african_head.obj            # Windows, MinGW

This writes assets/output.tga. To view it, convert it to PNG:

python tools/tga2png.py assets/output.tga
Option Default Description
-o, --output <file> assets/output.tga Output image path
-w, --width <n> 800 Image width in pixels
-H, --height <n> 800 Image height in pixels
--shader <mode> phong phong, gouraud, flat, or depth
--eye x,y,z -1,0,2 Camera position
--center x,y,z 0,0,0 Point the camera looks at
--up x,y,z 0,1,0 Camera up vector
--light x,y,z 1,1,1 Light direction
-h, --help Print usage and exit

Rendering your own models

Pass the path to any Wavefront OBJ file in place of the bundled model. Textures are found by name beside the .obj: mine.obj loads mine_diffuse.tga, mine_nm.tga, and mine_spec.tga. Normal maps must be object-space. Any missing texture falls back to a sensible default, so a bare .obj still renders.

Credits

  • ssloy/tinyrenderer: the course this pipeline follows, and the source of the models and textures under obj/

License

MIT. See LICENSE. Third-party attribution for the models and textures under obj/ is in NOTICE.

About

A CPU software rasterizer in C++20 - OBJ loading, z-buffering, perspective projection, and Phong shading with diffuse/normal/specular maps. No graphics API, no dependencies.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages