A high-performance C++ ray tracing engine that generates photorealistic images using Monte Carlo path tracing
This project implements a CPU-based ray tracer with support for various materials, realistic lighting, and geometric primitives, producing stunning rendered images with physically-based accuracy.
- Monte Carlo Path Tracing: High-quality rendering with configurable samples per pixel
- Physically-Based Materials: Support for multiple material types with realistic light interaction
- Anti-aliasing: Multi-sampling for smooth, high-quality images
- Recursive Ray Bouncing: Configurable maximum ray depth for realistic lighting
- π Lambertian: Diffuse materials for matte surfaces
- πͺ Metal: Reflective surfaces with configurable fuzziness
- π Dielectric: Glass-like materials with refraction and reflection
- π‘ Emissive: Light-emitting materials for illumination
- βͺ Spheres: Primary geometric primitive with material assignment
- π HittableList: Scene management for multiple objects
- π― Hit Testing: Efficient ray-geometry intersection calculations
- π Perspective Camera: Configurable field of view and aspect ratio
- π― Flexible Positioning: Look-at system with customizable up vector
- π‘ Ray Generation: Generates camera rays for each pixel
- πΌοΈ PPM Image Output: Standard image format for rendered results
- β±οΈ Performance Timing: Built-in render time measurement
- π Configurable Resolution: Customizable image dimensions
- π Background Control: Adjustable background colors
RayTracing/
βββ src/
β βββ main.cpp # Main application entry point
β βββ core/ # Core ray tracing components
β β βββ Vec3.* # 3D vector mathematics
β β βββ Ray.* # Ray class for ray casting
β β βββ Camera.* # Camera system
β βββ geometry/ # Geometric primitives
β β βββ Hittable.h # Base class for hittable objects
β β βββ HitRecord.h # Ray-object intersection data
β β βββ HittableList.* # Scene object container
β β βββ Sphere.* # Sphere primitive implementation
β βββ materials/ # Material system
β β βββ Material.h # Base material interface
β β βββ Lambertian.* # Diffuse material
β β βββ Metal.* # Metallic material
β β βββ Dielectric.* # Glass/transparent material
β β βββ Emissive.* # Light-emitting material
β βββ render/ # Rendering system
β β βββ Renderer.* # Main rendering engine
β β βββ Image.* # Image handling
β βββ gpu/ # GPU rendering (placeholder)
β β βββ raytracer.cu # CUDA implementation (empty)
β βββ utils/ # Utility functions
βββ tests/ # Unit tests
βββ include/ # Additional headers
βββ scenes/ # Scene configuration files
βββ docs/ # Documentation
βββ build/ # Build output directory
- CMake 3.29+: Build system
- C++20 Compatible Compiler: GCC, Clang, or MSVC
- SFML 3.0.1: Graphics library (automatically downloaded)
- SFML Libraries: Graphics, Window, and System modules
Make sure you have the following installed:
- CMake 3.29 or higher
- A C++20 compatible compiler
- Git (for dependency fetching)
-
π₯ Clone the repository:
git clone https://github.com/RadaanMadhan/RayTracing.git cd RayTracing -
π Create and navigate to build directory:
mkdir build cd build -
βοΈ Configure with CMake:
cmake ..
-
π¨ Build the project:
cmake --build .
raytracer: Main executable for ray tracingraytracer_lib: Static library with core functionality- Test executables:
Vec3_tests,Ray_tests,Camera_tests,Sphere_tests
After building, run the main executable from the build directory:
./src/raytracer # On Linux/Mac
.\src\raytracer.exe # On WindowsThe program will:
- Initialize a scene with spheres and materials
- Set up a camera with configurable parameters
- Render the scene using Monte Carlo sampling
- Output the result as
render.ppm - Display render time statistics
The main rendering parameters can be modified in src/main.cpp:
constexpr int imageWidth = 800; // Image width in pixels
constexpr int imageHeight = 400; // Image height in pixels
constexpr int samplesPerPixel = 200; // Anti-aliasing samples
constexpr int maxDepth = 50; // Maximum ray bounce depthconst Vec3 origin(0, 0, -5); // Camera position
const Vec3 lookAt(0, 0, 0.5); // Look at point
const Vec3 up(0, 1, 0); // Up vector
float fov = 40 * M_PI / 180; // Field of view in radiansThe project includes comprehensive unit tests using Google Test:
cd build
ctest # Run all tests
./tests/Vec3_tests # Run specific test suite- Vec3 Tests: Vector mathematics operations
- Ray Tests: Ray creation and manipulation
- Camera Tests: Camera ray generation
- Sphere Tests: Sphere-ray intersection
The ray tracer generates images in PPM format (render.ppm). This format can be:
- Viewed with image viewers that support PPM
- Converted to other formats using tools like ImageMagick:
convert render.ppm render.png
- CPU-based rendering: Single-threaded Monte Carlo path tracing
- Typical render times: Varies based on resolution and sample count
- Memory usage: Minimal, suitable for complex scenes
- GPU acceleration using CUDA (framework in place)
- Multi-threading support
- Acceleration structures (BVH, etc.)
- Additional geometric primitives
- π΄ Fork the repository
- πΏ Create a feature branch
- π§ͺ Add tests for new functionality
- β Ensure all tests pass
- π Submit a pull request
This project is open source. Please check the repository for license details.
This ray tracing implementation is inspired by:
- π "Ray Tracing in One Weekend" series by Peter Shirley
- π "Physically Based Rendering" by Pharr, Jakob, and Humphreys
- π¬ Real-time rendering techniques and Monte Carlo methods
