cpp-ray-tracer
C++ Ray Tracer Team
This program draws an imaginary scene by following rays of light around it, the trick behind film-quality shadows and reflections. The showcase scene is a crystal cave of 1.29 million triangles, and testing every ray against every one is hopelessly slow; grouping them lets the program skip nearly all of them. Our founder, Salman Adnan, wrote the shading layer of this three-person project: how each surface catches light, dull, glossy, or mirror-like.
A three-person course project (CS 440 at Habib University). Our founder, Salman Adnan, wrote the shading and materials layer: the Lambertian, GlossySpecular, and PerfectSpecular BRDFs, the Matte, Phong, and Reflective materials, the Whitted tracer with its reflection math, and shadow-ray integration. Teammates contributed the BVH and sampling work, the scene generator, and the lights.
Overview
A CPU ray tracer in C++17 whose showcase scene, Crystal Cavern, is a procedurally generated cave of 1.29 million triangles rendered at 1920x1080 with Whitted recursive tracing, a reflective floor, Phong crystals, and multiple shadow-casting lights.
Roughly 3,000 lines of C++ with no external dependencies beyond a C++17 compiler. The class hierarchy derives from Kevin Suffern's Ray Tracing from the Ground Up scaffold.
Key features
- Whitted recursive ray tracing with configurable depth (default 5).
- BRDFs (Lambertian, glossy specular, perfect specular) and materials (Matte, Phong, Reflective).
- Geometry: spheres, planes, and triangles with Moller-Trumbore intersection.
- A top-down BVH (median split on the longest axis, leaf threshold 4).
- Point, directional, and shadow-casting lights; one-ray or n by n jittered antialiasing; perspective and parallel cameras.
- OpenMP parallel rendering with PNG output via bundled stb_image_write.
Results
- Brute force costs 166.6 ms per pixel on the cavern scene; the BVH drops it to 0.040 ms per pixel, roughly a 4000x per-pixel speedup on 1.29 million triangles.
- A 480x360 image renders in about 5.4 s on 8 threads; the final 1920x1080 Crystal Cavern at 4 samples per pixel takes about 9 minutes.
Tech stack
A challenge worth noting
Brute-force intersection was unusable at cavern scale, because every primary ray tested every triangle. A top-down BVH dropped per-pixel cost from 166.6 ms to 0.040 ms. The acceleration structure is the whole ballgame for a scene this size: the speedup came entirely from not touching most of the geometry, not from micro-optimizing intersection.