tcbrindle/raytracer.hpp
FreeSimple compile-time raytracer using C++17
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
Pros & Cons
- 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)
- 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++