Skip to content

A raytracing engine in C that renders 3D scenes with spheres, planes, cylinders, and cubes. Features multi-threading, texture mapping, bump mapping, and Phong lighting model.

Notifications You must be signed in to change notification settings

whoismtrx/42_miniRT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

miniRT

Morocco Scene

Overview

This project is about creating a simple raytracer in C using the miniLibX library. The program renders 3D scenes by simulating light rays and their interactions with various geometric objects. It implements fundamental raytracing techniques including ray-object intersection, lighting calculations, shadows, and camera projection.

The raytracer parses scene description files (.rt format) that define the camera, lighting, and objects in the scene. It then generates photorealistic images by calculating the color of each pixel based on ray intersections, light sources, and material properties. The goal is to create a functional raytracing engine capable of rendering spheres, planes, cylinders, and cubes with proper lighting, shadows, and advanced features like textures and bump mapping.

Key Features

The project is written in C and implements a fully functional raytracing renderer. Its core features include:

Scene Parsing: Reads .rt files to configure ambient lighting, camera position/orientation, light sources, and geometric objects with their properties

Geometric Objects: Supports spheres, planes, cylinders, and cubes with configurable position, orientation, size, and color

Camera System: Implements perspective projection with adjustable field of view (FOV) and camera positioning

Lighting Model: Calculates diffuse lighting based on the Phong reflection model with support for multiple light sources

Ambient Lighting: Global illumination that ensures objects are visible even without direct light

Ray-Object Intersection: Precise mathematical calculations for determining ray intersections with various geometric primitives

Color Management: RGB color system with proper color blending and intensity calculations

Multi-Threading: Parallel rendering using pthreads for improved performance

Texture Mapping: Applies 2D textures to 3D objects (spheres, planes, cylinders) for realistic surface details

Bump Mapping: Simulates surface irregularities by perturbing surface normals based on texture data

Checkerboard Patterns: Procedural pattern generation for objects without explicit textures

Shadow Calculation: Implements shadow rays to determine if points are in shadow from light sources

MLX Integration: Uses miniLibX for window management, image creation, and event handling

Getting Started

To compile and run the project:

git clone [email protected]:whoismtrx/42_miniRT.git miniRT
cd miniRT

To build the mandatory part:

make

To build the bonus part (with textures and advanced features):

make bonus

To clean object files:

make clean

To remove all compiled files:

make fclean

To recompile from scratch:

make re

Usage

After compilation, run the raytracer with a scene file:

./miniRT maps/sp.rt

Or for the bonus version:

./miniRT_bonus maps/big_room.rt

The program will open a window displaying the rendered scene. You can close the window by:

  • Clicking the close button (X)
  • Pressing ESC key

Scene File Format

Scene files use the .rt extension and follow this syntax:

Ambient Lighting

A    0.2                      255,255,255
  • Ambient lighting ratio (0.0 to 1.0)
  • RGB color (0-255)

Camera

C    0,5,75                   0,0,-1          90
  • Camera position (x,y,z coordinates)
  • Orientation vector (normalized)
  • Field of view (0-180 degrees)

Light

l    -35,60,-50               0.5             255,255,255
  • Light position (x,y,z coordinates)
  • Brightness ratio (0.0 to 1.0)
  • RGB color (0-255)

Sphere

sp   -20,0,-60                30              255,100,50
  • Center position (x,y,z coordinates)
  • Diameter
  • RGB color (0-255)

Plane

pl   0,-15,-200               0,1,0           255,255,255
  • Point on plane (x,y,z coordinates)
  • Normal vector (normalized)
  • RGB color (0-255)

Cylinder

cy   -45,-30,-70              0,1,0           15    50    255,150,200
  • Center position (x,y,z coordinates)
  • Axis orientation vector (normalized)
  • Diameter
  • Height
  • RGB color (0-255)

Cube (Bonus)

cb   0,0,-50                  0,1,0           20           100,100,255
  • Center position (x,y,z coordinates)
  • Orientation vector (normalized)
  • Edge length
  • RGB color (0-255)

Example Scenes

The maps/ directory contains various example scenes:

  • sp.rt: Simple scene with spheres, cylinders, and planes
  • big_room.rt: Large room with multiple objects and lights
  • basketball.rt: Basketball court scene
  • 8pool.rt: Pool table scene
  • android.rt: Android mascot
  • pillars.rt: Scene with cylindrical pillars
  • trees.rt: Forest-like scene
  • H2O.rt: Water molecule representation

Room Scene

8 Pool Table

Color Scene

Basketball Court

Implementation

The implementation is divided into several key modules:

Parsing

The parser (src/pars/) reads .rt scene files and validates their content. It processes each line to identify element types (ambient, camera, lights, objects) and extracts their properties using helper functions for type conversion.

Vector Operations

The vector module (src/operation/) implements essential 3D math including vector creation, addition/subtraction, dot/cross products, normalization, and reference frame creation for coordinate transformations.

Camera System

The camera module generates direction vectors for each pixel based on the camera position, orientation, and field of view, creating a 720x720 array of ray directions.

Ray-Object Intersection

The intersection module (src/intersection/) calculates where rays intersect with geometric objects (spheres, planes, cylinders, cubes) using quadratic equations and geometric formulas, returning intersection distance, point, and surface normal.

Lighting

The lighting system implements the Phong model with ambient and diffuse components. For each pixel, it casts rays, finds intersections, and calculates lighting by considering light direction, surface normals, and shadow occlusion.

Texture & Bump Mapping

Textures are applied by mapping 3D surface coordinates to 2D texture space using spherical or planar projections. Bump mapping perturbs surface normals based on texture data to simulate surface detail.

Multi-Threading

The renderer uses pthreads to divide the image into horizontal strips, with each thread rendering its portion independently for improved performance.

Project Structure

miniRT/
├── Makefile              # Build configuration
├── README.md            # Project documentation
│
├── include/             # Header files
│   ├── minirt.h         # Main header with function declarations
│   ├── structs.h        # Structure definitions
│   ├── utils.h          # Utility function declarations
│   └── get_next_line.h  # GNL header for file reading
│
├── src/                 # Source files
│   ├── main.c           # Mandatory main entry point
│   ├── main_bonus.c     # Bonus main with multi-threading
│   ├── mlx_events.c     # MLX event handling
│   ├── threads.c        # Multi-threading implementation
│   ├── add_front.c      # Linked list utilities
│   │
│   ├── get_next_line/   # File reading
│   │   ├── get_next_line.c
│   │   └── get_next_line_utils.c
│   │
│   ├── pars/            # Scene file parsing
│   │   ├── pars.c       # Main parser logic
│   │   ├── ambient.c    # Ambient light parsing
│   │   ├── camera.c     # Camera configuration
│   │   ├── light.c      # Light source parsing
│   │   ├── sphere.c     # Sphere object parsing
│   │   ├── plane.c      # Plane object parsing
│   │   ├── cylindre.c   # Cylinder parsing
│   │   ├── cube.c       # Cube parsing (bonus)
│   │   ├── get_v_cam.c  # Camera ray generation
│   │   └── get_img.c    # Texture image loading
│   │
│   ├── operation/       # Vector and matrix operations
│   │   ├── vector.c     # Vector creation and basic ops
│   │   ├── vector_operation.c  # Advanced vector math
│   │   ├── equal.c      # Floating-point comparison
│   │   ├── sd_equation.c       # Quadratic equation solver
│   │   ├── creat_referance.c   # Reference frame creation
│   │   └── Matrice_prod.c      # Matrix operations
│   │
│   ├── intersection/    # Ray-object intersection
│   │   ├── sphere.c     # Sphere intersection & texturing
│   │   ├── plane.c      # Plane intersection & texturing
│   │   ├── cylindre.c   # Cylinder intersection & texturing
│   │   ├── circle.c     # Circle (cylinder cap) intersection
│   │   ├── cube.c       # Cube intersection (bonus)
│   │   ├── get_pixel.c  # Main pixel color calculation
│   │   ├── get_pixel2.c # Lighting calculations
│   │   └── get_pixel3.c # Shadow and additional effects
│   │
│   └── utils/           # Utility functions
│       ├── ft_atoi.c    # String to integer
│       ├── ft_atof.c    # String to float
│       ├── ft_atoc.c    # String to coordinates
│       ├── ft_atocolor.c # String to color
│       ├── ft_split.c   # String splitting
│       ├── ft_strlen.c  # String length
│       ├── ft_strdup.c  # String duplication
│       ├── ft_strcmp.c  # String comparison
│       ├── ft_substr.c  # Substring extraction
│       ├── ft_strtrim.c # String trimming
│       ├── ft_calloc.c  # Memory allocation
│       ├── ft_memset.c  # Memory initialization
│       ├── error.c      # Error handling
│       └── ...          # Other utility functions
│
├── maps/                # Example scene files
│   ├── sp.rt           # Simple sphere scene
│   ├── big_room.rt     # Large room with multiple objects
│   ├── basketball.rt   # Basketball court
│   ├── 8pool.rt        # Pool table
│   ├── android.rt      # Android mascot
│   ├── pillars.rt      # Cylindrical pillars
│   ├── trees.rt        # Forest scene
│   └── ...             # More example scenes
│
├── img/                 # README images
│   ├── morocco.png      # Scene renders
│   ├── room404.png
│   ├── 8pool.png
│   ├── basketball.png
│   └── color.png
│
└── texture/            # Texture images directory

About

miniRT is a 42 school project that involves creating a raytracing engine from scratch. It focuses on 3D computer graphics, linear algebra, geometric algorithms, and optimization techniques to render photorealistic images.

The project demonstrates fundamental concepts in ray tracing algorithms, vector mathematics, geometric intersection calculations, lighting models, and performance optimization through parallelization.


Note: This project uses the miniLibX graphics library, which is available on macOS and Linux. The Makefile is configured for macOS (using -framework OpenGL -framework AppKit). For Linux, you'll need to adjust the linking flags accordingly.

About

A raytracing engine in C that renders 3D scenes with spheres, planes, cylinders, and cubes. Features multi-threading, texture mapping, bump mapping, and Phong lighting model.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published