A C++17 physics engine for light transport and incompressible fluids. PhotonFlow renders scenes with unbiased Monte Carlo path tracing and simulates free-surface fluids with semi-discrete optimal transport. No graphics API or engine dependency: geometry, acceleration structures, shading, power diagrams and the transport dual are implemented here; only PNG output (stb) and the L-BFGS optimizer (liblbfgs) are third-party.
- Global illumination with cosine-weighted importance sampling and next-event estimation from point lights.
- Specular mirrors and dielectrics with Schlick's Fresnel term and total internal reflection.
- Triangle meshes from Wavefront OBJ, accelerated by a median-split bounding volume hierarchy with Möller-Trumbore intersection.
- OpenMP-parallel rendering with a per-pixel random generator: no shared state between threads, and bit-identical images across runs and thread counts.
- Power diagrams built by Sutherland-Hodgman clipping, intersected with per-particle disks to model the air phase and produce a free surface.
- Transport weights from the concave dual of the semi-discrete problem, maximized with L-BFGS; the gradient is the gap between each cell's area and its target.
- Incompressibility through a stiff spring toward each cell's centroid (Gallouët and Mérigot, 2018) with semi-implicit Euler integration.
Measured on a 10-core Apple laptop, Release build with OpenMP.
| Workload | Time |
|---|---|
| 512 x 512, 64 samples/pixel, 5 bounces, 3,954-triangle mesh | 9.8 s |
| Same scene at 256 samples/pixel (images above) | 38 s |
| Fluid, 50 particles, 400 frames, transport solve every frame | 87 s |
ctest runs 15 unit tests and two end-to-end smoke tests on Ubuntu and macOS in CI:
- the BVH returns exactly the closest hit found by brute force over 500 random triangles;
- cosine-weighted sampling matches its analytic mean, E[cos θ] = 2/3;
- polygon integrals match closed forms, and Voronoi cells tile the domain exactly;
- power weights move the bisector to the analytic position;
- the transport solver gives every particle the same area to within 1e-3.
Requires CMake 3.16+ and a C++17 compiler. OpenMP is optional.
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --parallel
ctest --test-dir build --output-on-failureOn macOS with Apple Clang, add OpenMP for multithreading:
brew install libomp
cmake -S . -B build -DOpenMP_ROOT=$(brew --prefix libomp)./build/render/photonflow-render --samples 256 --output render.png
./build/render/photonflow-render --glass-sphere --mirror-sphere --output spheres.png
./build/fluid/photonflow-fluid --particles 50 --frames 400 --output-dir frames
ffmpeg -framerate 25 -i frames/frame_%04d.png -pix_fmt yuv420p fluid.mp4render/ vec3, ray, rng, shape, sphere, aabb, triangle_mesh (OBJ + BVH), scene, camera, CLI
fluid/ vec2, polygon, power_diagram, optimal_transport (L-BFGS dual), fluid, frame_writer, CLI
tests/ unit tests and CLI smoke tests (CTest)
third_party/ stb_image_write, liblbfgs
- T. Gallouët and Q. Mérigot. A Lagrangian scheme à la Brenier for the incompressible Euler equations. Foundations of Computational Mathematics, 2018.
- B. Lévy. A numerical algorithm for L2 semi-discrete optimal transport in 3D. ESAIM: M2AN, 2015.
- T. Möller and B. Trumbore. Fast, minimum storage ray-triangle intersection. Journal of Graphics Tools, 1997.
- C. Schlick. An inexpensive BRDF model for physically-based rendering. Computer Graphics Forum, 1994.
- stb_image_write (public domain or MIT).
- liblbfgs by Naoaki Okazaki (MIT), see
third_party/liblbfgs/COPYING. - The cat model was downloaded from cadnav.com.
MIT, see LICENSE.


