Skip to content

LessUp/ray-tracer

Repository files navigation

CUDA Ray Tracer

CI Pages License: MIT CUDA C++

English | 简体中文

A CUDA/C++ ray tracer with Blinn-Phong shading, Monte Carlo path tracing, BVH acceleration, and an optional primary-ray sorting path for Phong single-sample renders.

Features

  • Blinn-Phong Shading — Ambient, diffuse, specular lighting with multi-light support
  • Path Tracing — Monte Carlo global illumination with Russian roulette termination
  • BVH Acceleration — Bounding Volume Hierarchy for faster sphere intersection queries
  • Ray Sorting — Group primary rays by hit object ID in Phong single-sample mode
  • Tone Mapping — Reinhard output path, plus an ACES helper available in code/tests
  • Multiple Scenes — Demo, Cornell Box, and random sphere scenes
  • Tests — Unit and property tests for math, scene construction, sorting, and helper logic

Requirements

  • CUDA Toolkit 11.0+
  • CMake 3.18+
  • C++17
  • NVIDIA GPU with Compute Capability 7.5+

Build

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j$(nproc)

Run

# Render scenes
build/bin/ray_tracer -w 800 -h 600 -s 1 --scene demo -o output.ppm
build/bin/ray_tracer -w 640 -h 480 -s 16 -d 5 -p --scene cornell -o cornell.ppm

# Ray sorting only applies to Phong + single sample
build/bin/ray_tracer --scene demo --sort -w 640 -h 480 -s 1 -o sorted.ppm

Options

Flag Description Default
-w / -h Image width/height 800 / 600
-s Samples per pixel 1
-d Max ray depth, must be > 0 5
-p Enable path tracing Off
--scene Scene name (demo / cornell / random) demo
--sort Enable ray sorting for Phong + single sample only Off
-o Output PPM file output.ppm

Test

cd build
ctest --output-on-failure

Direct test binaries are emitted under build/bin/.

CI

GitHub Actions currently verifies:

  • configure/build
  • formatting via the repository .clang-format

GPU-backed tests are not run on GitHub-hosted runners because they do not expose CUDA devices. Run ctest --output-on-failure on a CUDA-capable machine after building.

Project Structure

├── include/            # Headers (vec3, ray, camera, geometry, material, BVH, ...)
├── src/                # Main entry + scene definitions
├── tests/
│   ├── unit/           # Unit tests
│   └── property/       # Property tests
├── .github/workflows/
├── CMakeLists.txt
└── README.md

Notes

  • The path tracer no longer treats ordinary material ambient terms as emission.
  • Scene factory helpers reset the builder/output vectors on each call, so reusing a SceneBuilder is safe.
  • Renderer reports render time separately from optional sort-analysis time.

License

MIT License

About

CUDA Ray Tracer: Phong Shading, Monte Carlo Path Tracing, BVH Acceleration & Tone Mapping (C++17) | CUDA 光线追踪器:Phong 光照、蒙特卡洛路径追踪、BVH 加速结构与色调映射(C++17)

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors