A CPU-based ray tracer written in C++ featuring Phong shading, reflections, refractions with Fresnel, shadow rays, BVH acceleration, and OBJ mesh loading.
- Geometry: Spheres, planes, cones, and triangle meshes (OBJ format)
- Shading: Phong illumination model with ambient, diffuse, and specular components
- Reflections: Recursive mirror reflections with configurable reflectivity
- Refractions: Snell's law refraction with Schlick's Fresnel approximation and total internal reflection
- Shadows: Hard shadow rays with BVH-accelerated occlusion testing
- Acceleration: Bounding Volume Hierarchy (BVH) with median-split construction
- Antialiasing: Multi-sample jittered antialiasing (configurable samples per pixel)
- Tone mapping: Gamma correction with configurable exposure
- Smooth shading: Interpolated vertex normals for triangle meshes
- Parallelism: OpenMP support for multi-core rendering
Requires a C++17 compiler and CMake 3.14+. GLM is included in src/glm/.
mkdir build && cd build
cmake ..
make -j$(nproc)OpenMP is detected and enabled automatically if available.
./raytracer # defaults: 2048x1536, 90° FOV, 1 sample
./raytracer --width 1024 --height 768 # custom resolution
./raytracer --fov 60 --samples 16 -o out.ppm # narrow FOV, 16x AA
./raytracer --helpConvert PPM to PNG with ImageMagick:
convert result.ppm result.pngCMakeLists.txt
src/
├── main.cpp # CLI, camera, render loop
├── Scene.h / .cpp # Scene ownership, Phong shading, recursive ray tracing
├── Image.h # PPM framebuffer writer
├── Material.h # Surface material properties
├── Ray.h # Ray representation
├── Hit.h # Intersection result
├── BoundingBox.h # AABB with ray intersection
├── Object.h # Abstract intersectable base class
├── Sphere.h # Sphere primitive
├── Plane.h # Infinite plane primitive
├── Cone.h # Capped cone primitive
├── Triangle.h # Triangle with smooth shading support
├── Vertex.h # Mesh vertex (position + normal)
├── Mesh.h # Generic OBJ loader
├── Primitive.h # BVH leaf wrapper
├── Node.h # BVH tree node + builder
├── Light.h # Point light source
└── glm/ # OpenGL Mathematics (header-only)
meshes/
├── armadillo.obj
├── bunny.obj
└── lucy.obj
Rays are cast from a pinhole camera through each pixel with optional jittered sub-pixel sampling for antialiasing. The BVH tree accelerates intersection tests by culling subtrees via AABB checks. On hit, the Phong model computes direct illumination with shadow rays for each light. Reflective and refractive materials recurse up to 10 bounces, blending via Schlick's Fresnel approximation.
