A real-time 3D raytracing renderer built with C++ and SDL2. Create and visualize stunning 3D scenes with realistic lighting, shadows, and reflections. Interact with the scene using mouse and keyboard controls.
This raytracer renders 3D scenes in real-time with:
- 3D Rendering: Visualize complex 3D scenes with spheres, planes, and other objects
- Realistic Lighting: Accurate light simulation with shadows and reflections
- Interactive Controls: Rotate, zoom, and navigate through the scene
- Real-Time Updates: See changes instantly as you interact
- Ray-Object Intersection: Precise mathematical calculations for realistic rendering
- Color and Materials: Different materials with varying properties
- Ambient Occlusion: Soft shadows for depth perception
- Computer graphics students learning raytracing
- Game developers understanding rendering techniques
- 3D artists exploring rendering algorithms
- Programmers interested in graphics programming
- Anyone fascinated by computer-generated imagery
- C++ Compiler (g++ or clang)
- SDL2 Library - For window management and rendering
- Make - Build automation tool
macOS:
brew install sdl2Ubuntu/Debian:
sudo apt-get update
sudo apt-get install libsdl2-devFedora:
sudo dnf install SDL2-develWindows:
- Download SDL2 development libraries from SDL2 Downloads
- Extract and configure in your project
# Clone the repository
git clone https://github.com/mohammedhrima/Raytracer.git
cd Raytracer
# Build the project
make
# This creates the executable 'exec'./execA window will open displaying the rendered 3D scene.
Rotate the Scene:
- Left-click and drag: Rotate the camera around the scene
- Move mouse left/right: Rotate horizontally
- Move mouse up/down: Rotate vertically
Zoom:
- Scroll wheel up: Zoom in (get closer to objects)
- Scroll wheel down: Zoom out (move away from objects)
Navigate the Scene:
- โ (Up Arrow): Move camera upward
- โ (Down Arrow): Move camera downward
- โ (Left Arrow): Move camera left
- โ (Right Arrow): Move camera right
Exit:
- ESC: Close the application
Ray Generation:
- Generates rays from camera through each pixel
- Calculates ray direction based on camera position and orientation
Intersection Testing:
- Ray-sphere intersection
- Ray-plane intersection
- Efficient intersection algorithms
Lighting Model:
- Diffuse lighting (Lambertian reflection)
- Specular highlights
- Ambient lighting
- Shadow rays for realistic shadows
Color Calculation:
- RGB color mixing
- Material properties (color, reflectivity)
- Light intensity calculations
The default scene includes:
- Multiple spheres of different sizes and colors
- Ground plane
- Light sources
- Camera with adjustable position
- Real-time rendering at interactive frame rates
- Optimized intersection calculations
- Efficient memory management
- C++98: Core programming language
- SDL2: Simple DirectMedia Layer for graphics
- Mathematics: Vector math, ray equations, intersection algorithms
- Make: Build system
Raytracer/
โโโ Makefile # Build configuration
โโโ README.md # This file
โโโ Screenshot.png # Preview image
โโโ header.hpp # Header files and declarations
โโโ main.cpp # Entry point and main loop
โโโ math.cpp # Vector and matrix mathematics
โโโ raytracing.cpp # Core raytracing algorithms
โโโ window.cpp # SDL window management
โโโ new.cpp # Memory management
โโโ SDL/ # SDL2 header files
โโโ SDL.h
โโโ SDL_events.h
โโโ SDL_video.h
โโโ ... (other SDL headers)
# Compile the project
make
# Clean build files
make clean
# Rebuild everything
make reThe Makefile compiles these source files:
main.cpp- Application entry pointmath.cpp- Mathematical operationsraytracing.cpp- Raytracing algorithmswindow.cpp- Window and event handlingnew.cpp- Memory management
Flags used:
-std=c++98- C++98 standard-Wall -Wextra -Werror- Strict warnings-lSDL2- Link SDL2 library
- Camera Setup: Define camera position and viewing direction
- Ray Generation: For each pixel, create a ray from camera through pixel
- Intersection Testing: Check if ray hits any objects in scene
- Shading: Calculate color based on:
- Object material
- Light sources
- Shadows
- Reflections
- Pixel Coloring: Set pixel to calculated color
Ray(t) = Origin + t * Direction
Where:
Origin= Camera positionDirection= Ray direction through pixelt= Distance along ray
Solving the equation:
|Ray(t) - SphereCenter|ยฒ = SphereRadiusยฒ
Results in a quadratic equation that determines if and where the ray hits the sphere.
This project demonstrates:
- Ray-object intersection algorithms
- 3D vector mathematics
- Lighting models and shading
- Real-time graphics programming
- SDL2 for window management
- Event handling (mouse, keyboard)
- Performance optimization
Edit raytracing.cpp to:
- Add more spheres
- Change sphere positions and sizes
- Modify colors and materials
- Add different light sources
- Change background color
Edit main.cpp to:
- Change initial camera position
- Modify field of view
- Adjust movement speed
- Change rotation sensitivity
Possible improvements:
- Add more object types (cubes, cylinders)
- Implement reflections and refractions
- Add texture mapping
- Implement anti-aliasing
- Add depth of field effects
Optimize Rendering:
- Reduce window resolution for faster rendering
- Limit number of objects in scene
- Use bounding volumes for complex objects
- Implement spatial data structures (BVH, octree)
Improve Quality:
- Increase ray samples per pixel
- Add more light bounces
- Implement global illumination
- Use higher precision calculations
SDL2 not found:
# Verify SDL2 installation
sdl2-config --version
# If not found, reinstall SDL2Compilation errors:
# Clean and rebuild
make clean
makeWindow doesn't open:
- Check if SDL2 is properly installed
- Verify display is available
- Check for error messages in terminal
Slow performance:
- Reduce window size
- Simplify scene (fewer objects)
- Close other applications
- Add more primitive shapes (cubes, cylinders, cones)
- Implement reflection and refraction
- Add texture mapping
- Implement anti-aliasing (supersampling)
- Add global illumination
- Support for loading 3D models (OBJ files)
- Path tracing for photorealistic rendering
- GPU acceleration with CUDA or OpenCL
- Scene file format for easy scene creation
- Real-time scene editing
Contributions welcome! Great project for learning:
- Computer graphics
- Raytracing algorithms
- C++ programming
- SDL2 library
Learn More About Raytracing:
Mathematics:
- Vector operations
- Ray-sphere intersection
- Lighting equations (Phong, Blinn-Phong)
This project is open source and available for educational purposes.
- SDL2 team for the excellent graphics library
- Computer graphics community for raytracing resources
- Classic raytracing papers and books
