Skip to content

semedooo/RayTracerCPU

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ”ฆ RayTracerCPU

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.


About

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.


Features

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

Build & Run

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
./raytracer

Clean build artifacts:

make clean

Usage

The 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.


Project Structure

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)

Core Concepts

Vec3 โ€” 3D Vector Math

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)

Ray โ€” Parametric Line

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.

Rayโ€“Sphere Intersection

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)

Phong Lighting Model

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

Technologies

  • Language: C++17
  • Build: GNU Make + g++
  • Image format: PPM (P3 text format โ€” zero dependencies)
  • Dependencies: none (standard library only)

Roadmap

See PLANO.md for the full step-by-step development plan, including theory notes for each stage.


Author

Manuel Semedo โ€” Computer Science student
Built as a hands-on study of computer graphics, from scratch.

About

๐Ÿ”ฆ A CPU-based ray tracer built from scratch in modern C++ with zero dependencies. Implements ray-sphere intersections, Phong shading, shadows, and reflections step by step โ€” a hands-on approach to learning the math and physics behind computer graphics rendering.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors