Skip to content

Martulens/raytracer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Raytracer

A CPU-based ray tracer written in C++ featuring Phong shading, reflections, refractions with Fresnel, shadow rays, BVH acceleration, and OBJ mesh loading.

Features

  • 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

Build

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.

Usage

./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 --help

Convert PPM to PNG with ImageMagick:

convert result.ppm result.png

Project Structure

CMakeLists.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

Architecture

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.

About

A raytracer written in C++ featuring BVH acceleration, recursive reflections and refractions with Fresnel, Phong shading, shadow rays, and OBJ mesh loading.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages