A high-performance C++ ray tracing engine that renders photorealistic 3D images using ray casting algorithms.
- Ray Tracing: Accurate lighting simulation using recursive ray tracing
- Material System: Support for multiple material types:
- Lambertian (diffuse) materials
- Metal with configurable fuzz
- Dielectric (glass/transparent) materials
- Depth of Field: Camera depth of field effect with configurable defocus angle
- Anti-aliasing: Multi-sample per pixel for smooth edgescna
- Scene Generation: Procedurally generated random scenes with various objects
- C++17 or later compiler
- CMake 3.10+
mkdir build
cd build
cmake ..
cmake --build .Run the ray tracer to generate a PPM image:
./build/ray-tracer > output.ppmThe output is a PPM format image that can be viewed with any image viewer or converted to other formats.
Edit the camera settings in main.cpp to customize the rendering:
cam.aspect_ratio = 16.0 / 9.0; // Image aspect ratio
cam.image_width = 1200; // Output width in pixels
cam.samples_per_pixel = 500; // Samples per pixel (higher = less noise)
cam.max_depth = 50; // Maximum ray bounce depth
cam.vfov = 20; // Vertical field of view
cam.lookfrom = point3(13, 2, 3); // Camera position
cam.lookat = point3(0, 0, 0); // Look-at point
cam.vup = vec3(0, 1, 0); // Camera up vector
cam.defocus_angle = 0.6; // Depth of field effect
cam.focus_dist = 10.0; // Focus distancevec3.h: 3D vector math utilitiesray.h: Ray representation and operationshittable.h: Base interface for scene objectssphere.h: Sphere primitive implementationmaterial.h: Material definitions and scatter calculationscamera.h: Camera and rendering enginecolor.h: Color representation and conversion
- Reduce
samples_per_pixelfor faster preview renders - Lower
image_widthfor faster iteration - Reduce
max_depthfor simpler scenes - Increase
image_widthandsamples_per_pixelfor final high-quality renders
The renderer outputs PPM (Portable Pixmap) format images. To convert to other formats:
# Convert PPM to PNG (requires ImageMagick)
convert output.ppm output.png
# Convert PPM to JPG
convert output.ppm output.jpgSample renders demonstrating the ray tracer's capabilities:
Quick preview render with 10 samples per pixel - fast iteration
Final quality render with 500 samples per pixel - significantly reduced noise
This project is inspired by Ray Tracing in a Weekend by Peter Shirley. The book provides an excellent introduction to ray tracing concepts and serves as a great guide for understanding how light rendering works. It's a fantastic resource for anyone interested in computer graphics and rendering algorithms.
This project is open source. Feel free to modify and use it for educational or personal purposes.