Skip to content

mohammedhrima/Raytracer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

47 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Raytracer - 3D Scene Renderer ๐ŸŽจโœจ

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.

Raytracing Preview

๐ŸŽฏ What Does It Do?

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

๐Ÿ‘ค Who Is It For?

  • 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

๐Ÿš€ How to Use

Prerequisites

  • C++ Compiler (g++ or clang)
  • SDL2 Library - For window management and rendering
  • Make - Build automation tool

Installation

1. Install SDL2

macOS:

brew install sdl2

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install libsdl2-dev

Fedora:

sudo dnf install SDL2-devel

Windows:

  • Download SDL2 development libraries from SDL2 Downloads
  • Extract and configure in your project

2. Clone and Build

# Clone the repository
git clone https://github.com/mohammedhrima/Raytracer.git
cd Raytracer

# Build the project
make

# This creates the executable 'exec'

Running the Raytracer

./exec

A window will open displaying the rendered 3D scene.

๐ŸŽฎ Interactive Controls

Mouse Controls

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)

Keyboard Controls

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

โœจ Features

Raytracing Techniques

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

Scene Elements

The default scene includes:

  • Multiple spheres of different sizes and colors
  • Ground plane
  • Light sources
  • Camera with adjustable position

Performance

  • Real-time rendering at interactive frame rates
  • Optimized intersection calculations
  • Efficient memory management

๐Ÿ› ๏ธ Technical Stack

  • C++98: Core programming language
  • SDL2: Simple DirectMedia Layer for graphics
  • Mathematics: Vector math, ray equations, intersection algorithms
  • Make: Build system

๐Ÿ“ Project Structure

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)

๐Ÿ”ง Building and Compilation

Build Commands

# Compile the project
make

# Clean build files
make clean

# Rebuild everything
make re

Compilation Details

The Makefile compiles these source files:

  • main.cpp - Application entry point
  • math.cpp - Mathematical operations
  • raytracing.cpp - Raytracing algorithms
  • window.cpp - Window and event handling
  • new.cpp - Memory management

Flags used:

  • -std=c++98 - C++98 standard
  • -Wall -Wextra -Werror - Strict warnings
  • -lSDL2 - Link SDL2 library

๐ŸŽจ Understanding Raytracing

How Raytracing Works

  1. Camera Setup: Define camera position and viewing direction
  2. Ray Generation: For each pixel, create a ray from camera through pixel
  3. Intersection Testing: Check if ray hits any objects in scene
  4. Shading: Calculate color based on:
    • Object material
    • Light sources
    • Shadows
    • Reflections
  5. Pixel Coloring: Set pixel to calculated color

Ray Equation

Ray(t) = Origin + t * Direction

Where:

  • Origin = Camera position
  • Direction = Ray direction through pixel
  • t = Distance along ray

Sphere Intersection

Solving the equation:

|Ray(t) - SphereCenter|ยฒ = SphereRadiusยฒ

Results in a quadratic equation that determines if and where the ray hits the sphere.

๐ŸŽ“ Learning Outcomes

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

๐Ÿ”ง Customization

Modify the Scene

Edit raytracing.cpp to:

  • Add more spheres
  • Change sphere positions and sizes
  • Modify colors and materials
  • Add different light sources
  • Change background color

Adjust Camera

Edit main.cpp to:

  • Change initial camera position
  • Modify field of view
  • Adjust movement speed
  • Change rotation sensitivity

Enhance Rendering

Possible improvements:

  • Add more object types (cubes, cylinders)
  • Implement reflections and refractions
  • Add texture mapping
  • Implement anti-aliasing
  • Add depth of field effects

๐Ÿ“Š Performance Tips

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

๐Ÿ› Troubleshooting

SDL2 not found:

# Verify SDL2 installation
sdl2-config --version

# If not found, reinstall SDL2

Compilation errors:

# Clean and rebuild
make clean
make

Window 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

๐Ÿ“ˆ Future Enhancements

  • 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

๐Ÿค Contributing

Contributions welcome! Great project for learning:

  • Computer graphics
  • Raytracing algorithms
  • C++ programming
  • SDL2 library

๐Ÿ“š Resources

Learn More About Raytracing:

Mathematics:

  • Vector operations
  • Ray-sphere intersection
  • Lighting equations (Phong, Blinn-Phong)

๐Ÿ“„ License

This project is open source and available for educational purposes.

๐Ÿ™ Acknowledgments

  • SDL2 team for the excellent graphics library
  • Computer graphics community for raytracing resources
  • Classic raytracing papers and books

About

Raytracer built using C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages