tcbrindle/raytracer.hpp logo

tcbrindle/raytracer.hpp

Free

Simple compile-time raytracer using C++17

FreeFree tier
Type
Open Source

About tcbrindle/raytracer.hpp

tcbrindle/raytracer.hpp is a C++17 compile-time ray tracer that leverages constexpr evaluation to render images entirely at compile time. Based on Microsoft's TypeScript ray tracer example, it is translated almost line-for-line into C++. The library works both at compile time and at run time (where it is thousands of times faster) without using template metaprogramming. It defines a rt::ray_tracer class that takes a Scene and Canvas as template parameters, with built-in support for spheres and planes via a std::variant-based polymorphic wrapper. The project includes example files for compile-time (compile_time.cpp) and run-time (run_time.cpp) rendering, and uses the stb_image_write library to output PNG images.

Key Features

100% compile-time image generation via constexpr evaluation
Also runs efficiently at run time (thousands of times faster)
Based on Microsoft's TypeScript ray tracer example, translated line-for-line into C++
Uses std::variant (any_thing) to avoid virtual functions and support polymorphism
Supports sphere and plane objects out of the box
Scene and Canvas are template parameters for flexibility
Includes both compile-time (compile_time.cpp) and run-time (run_time.cpp) example files
Outputs PNG images using stb_image_write
Licensed under Apache 2.0

Pros & Cons

Pros
  • Lean, straightforward implementation without template metaprogramming
  • Compile-time evaluation ensures correctness and allows use in constexpr contexts
  • Efficient run-time performance without virtual function overhead
  • Easy to extend by adding new types to the any_thing variant
  • Well-documented with example code and built-in scene
  • Open source (Apache 2.0) and compatible with Clang 4.0+, GCC 7.1+, and MSVC 2017 (run-time only)
Cons
  • Compile-time rendering with GCC requires extreme memory (tens of GB for moderate image sizes)
  • Only spheres and planes are provided; other shapes require custom implementation and variant modification
  • Compile-time evaluation is limited by the compiler's constexpr step limit (Clang needs -fconstexpr-steps parameter)
  • Requires a compiler with good C++17 support; MSVC 2017 hits an ICE for compile-time evaluation
  • Not a general-purpose AI tool; it is a specific ray tracing library for C++

Best For

Learning ray tracing and constexpr programming in C++17Generating reference images at compile time for embedded or constexpr contextsEducational demonstrations of compile-time computationPrototyping ray tracing algorithms with immediate compile-time validation

FAQ

What compilers does raytracer.hpp support?
Compile-time rendering with GCC 7.1 uses extreme amounts of memory—tens of gigabytes for even modest image sizes. The project documentation recommends sticking to very small images with GCC or using Clang instead. This behavior is believed to be due to GCC's memoization of every constexpr evaluation result.
What types of objects can be rendered?
The library includes sphere and plane objects out of the box. To add custom shapes (e.g., a box), you must implement three member functions (intersect, get_normal, get_surface) and add the new type to the any_thing variant.
Does raytracer.hpp work only at compile time?
No, the code also works at run time, where it is thousands of times faster. The example run_time.cpp uses std::vectors to deliberately prevent compile-time evaluation, demonstrating run-time usage.
What license is the project under?
The project is licensed under Apache 2.0, as indicated by the LICENSE-2.0.txt file in the repository.