A high-performance ray tracing renderer implemented in Haskell, inspired by Peter Shirley's "Ray Tracing In One Weekend". This project demonstrates advanced functional programming concepts while producing stunning photorealistic images.
- Path Tracing: Physically-based rendering with global illumination
- Multiple Ray Bounces: Configurable depth for realistic light transport
- Anti-aliasing: Multi-sample per pixel for smooth edges
- Depth of Field: Realistic camera effects with defocus blur
- Spheres: Static and moving spheres with smooth motion blur
- Quads: Planar surfaces for walls, floors, and ceilings
- Triangles: Arbitrary triangular surfaces
- Ellipses: Elliptical shapes
- Axis-Aligned Bounding Boxes (AABB): Efficient collision detection
- Lambertian: Diffuse materials with realistic light scattering
- Metal: Reflective surfaces with configurable roughness
- Dielectric: Transparent materials (glass, water) with refraction
- Diffuse Light: Emissive materials for light sources
- Checker Pattern: Procedural checkerboard textures
- Perlin Noise: Procedural noise textures
- Image Textures: Realistic textures from image files
- Solid Colors: Simple uniform color materials
- Bounding Volume Hierarchy (BVH): Fast spatial partitioning for ray-object intersections
- Multi-threading: Parallel rendering for improved performance
- Progress Tracking: Real-time rendering progress display
- Memory Efficient: Optimized data structures and algorithms
- Final Scene: Complex scene with multiple materials and objects
- Perlin Spheres: Procedural noise-based scene
- Quads Scene: Architectural scene with planar surfaces
- Simple Light: Basic lighting demonstration
- Cornell Box: Classic computer graphics test scene
- Earth Scene: Realistic Earth texture mapping
- GHC (Glasgow Haskell Compiler): Version 8.10 or later
- Cabal: Version 3.0 or later
- System Libraries: Standard C libraries for image processing
-
Install GHC and Cabal:
- Download from Haskell Platform or
- Use GHCup:
ghcup install ghc cabal
-
Verify Installation:
ghc --version cabal --version
-
Clone the repository:
git clone https://github.com/Slowyn/haskell-raytracing.git cd haskell-raytracing -
Build the project:
cabal build
-
Run the renderer:
cabal run
The renderer supports various configuration options in app/Main.hs:
-- Image settings
width = 512 -- Image width in pixels
aspectRatio = 16.0 / 9.0 -- Aspect ratio
samplesPerPixel = 200 -- Anti-aliasing samples
maxDepth = 50 -- Maximum ray bounce depth
-- Scene selection (0-5)
selectedScene = 4 -- Choose from available scenes0: Final Scene - Complex multi-object scene1: Perlin Spheres - Procedural noise scene2: Quads Scene - Architectural scene3: Simple Light - Basic lighting demo4: Cornell Box - Classic test scene5: Earth Scene - Earth texture mapping
haskell-raytracing/
βββ app/
β βββ Main.hs # Main application entry point
β βββ Lib.hs # Core library module
β βββ Vec3.hs # 3D vector mathematics
β βββ Ray.hs # Ray representation and operations
β βββ Camera.hs # Camera and projection system
β βββ Hittable.hs # Ray-object intersection interface
β βββ HitRecord.hs # Intersection data structure
β βββ Material.hs # Material system interface
β βββ Texture.hs # Texture system interface
β βββ Bvh.hs # Bounding Volume Hierarchy
β βββ Scene.hs # Scene management
β βββ Materials/ # Material implementations
β β βββ Lambertian.hs # Diffuse materials
β β βββ Metal.hs # Reflective materials
β β βββ Dielectric.hs # Transparent materials
β β βββ DiffuseLight.hs # Emissive materials
β βββ Shapes/ # Geometric primitives
β β βββ Sphere.hs # Spherical objects
β β βββ Quad.hs # Planar surfaces
β β βββ Tri.hs # Triangular surfaces
β β βββ Ellipse.hs # Elliptical shapes
β βββ Textures/ # Texture implementations
β βββ SolidColor.hs # Uniform color textures
β βββ Checker.hs # Checkerboard patterns
β βββ Perlin.hs # Noise textures
β βββ Image.hs # Image-based textures
βββ assets/ # Texture assets
β βββ earthmap.jpg # Earth texture map
βββ haskell-raytracing.cabal # Project configuration
βββ README.md # This file
The project uses the following key Haskell libraries:
- JuicyPixels: Image processing and format support
- random: High-quality random number generation
- mtl: Monad transformers for state management
- vector: Efficient array operations
- massiv: Multi-dimensional arrays
- terminal-progress-bar: Progress tracking during rendering
The renderer produces high-quality images with realistic lighting, materials, and effects. Each scene demonstrates different aspects of the ray tracing engine:
- Realistic Materials: Metal, glass, and diffuse surfaces
- Global Illumination: Light bouncing between objects
- Soft Shadows: Natural shadow falloff
- Motion Blur: Realistic camera effects
- Depth of Field: Cinematic focus effects
The renderer is optimized for performance with:
- Multi-threaded rendering for CPU utilization
- BVH acceleration for fast ray-object intersections
- Memory-efficient algorithms for large scenes
- Progress tracking for long renders
Typical render times vary by scene complexity and settings:
- Simple scenes: 1-5 minutes
- Complex scenes: 10-30 minutes
- High-quality renders: 1-2 hours
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes and test thoroughly
- Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Peter Shirley for the excellent "Ray Tracing In One Weekend" series
- Haskell Community for the amazing ecosystem of libraries
- Contributors who have helped improve this project
- Author: Konstantin Iakushin
- Email: [email protected]
- GitHub: @Slowyn
Happy Raytracing! π¨β¨
