Skip to content

A CPU-based ray tracer built from scratch in C++ with BVH acceleration

License

Notifications You must be signed in to change notification settings

sourav4243/rayTracer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

100 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ray Tracer

This project is a CPU-based ray tracer written in C++ with a focus on physical correctness, clean architecture, and performance optimization.


Features

  • Perspective camera with configurable field of view
  • Ray–sphere intersection
  • Material models:
    • Lambertian (diffuse)
    • Metal (specular with fuzz)
    • Dielectric (refraction / glass)
  • Recursive ray tracing with depth limiting
  • Stochastic sampling for anti-aliasing
  • Image output in PPM format
  • Spatial acceleration using AABB and BVH

Rendering & Performance

A complex test scene consisting of ~500 spheres was rendered before and after introducing acceleration structures.

Performance Comparison (Same Scene ~500 Spheres)

Configuration Acceleration Build Type Render Time
Brute Force None Release ~1h 10m
AABB + BVH Enabled Release ~12m 33s

This confirms overall render time was reduced by approximately 82% after introducing AABB and BVH, while producing identical visual output.


Render Output

Pre-BVH (Brute Force)

Ray Tracer Output - Pre BVH

Post-BVH (Accelerated)

Ray Tracer Output - Post BVH


Requirements

  • CMake (version 3.10 or higher)
  • C++17-compatible compiler (e.g., g++, clang, MSVC)

Build & Run

Windows (MSVC / Visual Studio)

cmake -B build
cmake --build build --config Debug
./build/Debug/rayTracer.exe > image.ppm

For optimized builds:

cmake --build build --config Release
./build/Release/rayTracer.exe > image.ppm

Linux / macOS (GCC or Clang)

cmake -B build
cmake --build build
./build/rayTracer > image.ppm

For optimized builds:

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/rayTracer > image.ppm

Output

The program writes a PPM image to stdout.
Redirect the output to a file and open it using any image viewer that supports PPM.

Contributing

See CONTRIBUTING.md for contribution guidelines and suggested areas of improvement.

About

A CPU-based ray tracer built from scratch in C++ with BVH acceleration

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published