A CPU-based ray tracer built from scratch in modern C++, with zero external dependencies.
This is a personal learning project focused on understanding the mathematics and physics behind computer graphics rendering.
Ray tracing is one of the most elegant algorithms in computer graphics โ it simulates how light travels by casting rays from a virtual camera into a 3D scene and computing the color of each pixel based on what those rays hit.
This project implements a ray tracer step by step, starting from first principles:
- ๐งฎ Linear algebra โ 3D vectors, dot/cross products, normalization
- ๐ Geometric intersections โ rayโsphere, rayโplane equations
- ๐ก Phong lighting model โ ambient, diffuse, and specular components
- ๐ Shadows โ shadow ray casting with epsilon bias
- ๐ช Reflections โ recursive ray tracing with depth limiting
- ๐ผ๏ธ PPM image output โ zero-dependency image format, readable by any viewer
The goal is to deeply understand the rendering pipeline by writing every piece of it by hand, in C++, without relying on graphics libraries.
| Feature | Status |
|---|---|
Vec3 class โ operators, dot, cross, length, normalize |
โ Done |
Ray class โ P(t) = origin + tยทdirection |
โ Done |
Image โ PPM output (P3 format) |
โ Done |
| Gradient sky render | โ Done |
| Interactive Vec3 test menu | โ Done |
Camera โ viewport and ray generation per pixel |
๐ง In progress |
Sphere โ rayโsphere intersection |
๐ง In progress |
Hittable / HittableList โ polymorphic scene objects |
๐ง In progress |
Phong shading (Light, Material) |
๐ Planned |
| Shadow rays | ๐ Planned |
| Recursive reflections | ๐ Planned |
| Multi-object scene composition | ๐ Planned |
Requirements: g++ with C++17 support (no other dependencies needed).
# Clone the repository
git clone https://github.com/semedooo/RayTracerCPU.git
cd RayTracerCPU
# Build
make
# Run
make run
# or
./raytracerClean build artifacts:
make cleanThe program starts an interactive menu for testing vector operations and rendering:
=== Ray Tracer CPU - Tests ===
1. Sum (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Dot Product (dot)
6. Cross Product (cross)
7. Length (length)
8. Normalize (normalized)
9. Generate test PPM image
0. Exit
Selecting option 9 generates a gradient test image at output/test.ppm.
RayTracerCPU/
โโโ Makefile # Build rules (g++, C++17)
โโโ README.md
โโโ PLANO.md # Development roadmap (Portuguese)
โโโ include/
โ โโโ Vec3.h # โ
3D vector math
โ โโโ Point.h # โ
3D point (P3)
โ โโโ Ray.h # โ
Ray: origin + tยทdirection
โ โโโ Image.h # โ
PPM image output
โ โโโ Camera.h # ๐ง Viewport & ray generation
โ โโโ Hittable.h # ๐ง Abstract hittable interface
โ โโโ Sphere.h # ๐ง Sphere geometry
โ โโโ Light.h # ๐ Point light source
โ โโโ Material.h # ๐ Phong material properties
โโโ src/
โ โโโ main.cpp # Entry point & interactive test menu
โโโ output/
โโโ *.ppm # Rendered images (generated at runtime)
All geometry lives in Vec3, a zero-overhead class implementing the full set of operations needed for ray tracing:
- Component-wise
+,-,*,/ - Dot product:
aยทb = axยทbx + ayยทby + azยทbz(used in lighting and intersection tests) - Cross product:
aรb(used for constructing coordinate frames) - Normalization:
vฬ = v / โvโ(directions must always be unit vectors)
A ray is defined as P(t) = origin + t ร direction, where t โฅ 0.
For every pixel on screen, a ray is cast from the camera's origin through that pixel's viewport position.
Substituting the ray equation into the sphere equation yields a quadratic in t.
The discriminant ฮ = bยฒ โ 4ac tells us:
ฮ < 0โ no intersectionฮ = 0โ tangent (one point)ฮ > 0โ two intersections (entry and exit)
Each visible surface point is shaded using three components:
- Ambient โ constant base light (
Ia) - Diffuse โ
Id = kd ยท I ยท max(0, nฬ ยท lฬ)โ brighter surfaces face the light (kd= diffuse reflectance,I= light intensity) - Specular โ
(rฬ ยท vฬ)^ฮฑโ mirror-like highlight based on shininess
- Language: C++17
- Build: GNU Make + g++
- Image format: PPM (P3 text format โ zero dependencies)
- Dependencies: none (standard library only)
See PLANO.md for the full step-by-step development plan, including theory notes for each stage.
Manuel Semedo โ Computer Science student
Built as a hands-on study of computer graphics, from scratch.