Standalone C++ implementation of raytracing and rasterization, without external libraries or frameworks.
- The demos come from "Computer Graphics from Scratch" (book), which provides Javascript demos.
- Additionally implements features from "Extending the Raytracer" that weren't implemented in the original source code.
These demos are all written in FP32, as opposed to the original Javascript demos that use FP64 by default. This results in shadow acne in all raytracing demos after Raytracer 04. To fix this, I added an epsilon reflection ray bias (e.g., start the reflection ray at an epsilon offset along the surface normal). The previews below include this fix.
Raytracer 01 - Basic | Raytracer 02 - Diffuse | Raytracer 03 - Specular |
---|---|---|
Raytracer 04 - Shadow | Raytracer 05 - Reflection | Raytracer 06 - Camera |
---|---|---|
These implement various features from "Extending the Raytracer", which didn't previously have implementations.
Raytracer 07 - Triangle | Raytracer 08 - CSG | Raytracer 09 - Optimization |
---|---|---|
The C++ implementations below differ slightly from their Javascript counterparts to improve efficiency.
Raster 01 - Lines | Raster 02 - Lines (Fixed) | Raster 03 - Triangle |
---|---|---|
Raster 04 - Shading | Raster 05 - Perspective | Raster 06 - Cube |
---|---|---|
Raster 07 - Instances | Raster 08 - Homogenous | Raster 09 - Clipping |
---|---|---|
Raster 10 - Surface | Raster 11 - Shading | Raster 12 - Texture |
---|---|---|
Note: The instructions below assume you're in the repository root.
To get started, pick any demo script to build. For example, the first demo for
raytracing is in demos/raytracer-01-basic.cpp
. To build and run it, use the following
g++ demos/raytracer-01-basic.cpp -o main.out -std=c++20
./main.out
open output.bmp
This gives the following output, which matches the output from the original.
This Repo | Original Reference on the author's website |
---|---|
Before running raster-12-texture.cpp
, download and convert the texture map
using the provided Python script.
pip install Pillow requests
python tools/download_texture.py
Build and run all demos using the following, after installing Python dependencies above:
bash tools/run.sh
All scripts in this repository were tested using clang version 15.0.0.
❯ g++ --version
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: x86_64-apple-darwin23.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin