Skip to content
jbayonnegitPublic
forked from khawos/miniRT

About

This project is an introduction to the beautiful world of Raytracing.

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

67 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

miniRT

A CPU ray tracer written in C, combining multithreaded rendering, spatial acceleration and interactive camera controls.

Developed as part of the 42 curriculum by jbayonne and amedenec, this project explores the complete rendering pipeline: parsing a scene, generating camera rays, finding intersections, shading surfaces and displaying the result through MiniLibX.

Highlights

  • Geometry: spheres, planes, capped cylinders and triangles.
  • Lighting: ambient light, multiple point lights, diffuse and specular shading, and shadow rays.
  • Materials: recursive reflections and XPM-based albedo, normal and roughness maps, with UV mapping for spheres and planes.
  • Parallel rendering: POSIX threads split the image into horizontal bands; semaphores and a mutex coordinate rendering setup and error handling.
  • Spatial acceleration: a bounding volume hierarchy (BVH) narrows the triangle candidates tested against a ray.
  • Interactive rendering: camera movement triggers a coarse preview, followed by progressively finer pixel blocks when input stops.
  • Experimental refraction: a sphere-refraction implementation is included; see the implementation notes below.

Build and run

Requirements

The supplied Makefile targets Linux with X11. It requires a C compiler, Make, MiniLibX for Linux, X11/Xext, zlib, libbsd and POSIX threads.

On Ubuntu or Debian:

sudo apt update
sudo apt install build-essential git libx11-dev libxext-dev zlib1g-dev libbsd-dev

Installation

MiniLibX is not included in this repository. Clone it into the directory expected by the Makefile:

git clone https://github.com/jbayonnegit/miniRT.git
cd miniRT
git clone https://github.com/42Paris/minilibx-linux.git minilibx-linux
make
./miniRT scene/exemple.rt

Run the application from the repository root so relative texture paths resolve correctly. A working X11 display is required; the supplied build is not a native macOS build.

Other scenes can be selected through the single command-line argument:

./miniRT scene/test_texture.rt
./miniRT scene/cat.rt

Cleanup

make clean     # Remove project and libft object files
make fclean    # Also remove the executable and clean library builds
make re        # Clean and rebuild

Camera controls

Input Action
Middle mouse button + drag Pan the camera
Left Shift + middle mouse button + drag Rotate the camera
Mouse wheel up / down Move forward / backward along the viewing direction
Escape or window close button Exit

After camera input, the image is rendered in coarse blocks and refined after a short idle delay. This trades preview detail for responsiveness without changing the window resolution.

Scene files

Scenes are plain-text .rt files. The following example comes from scene/exemple.rt:

A  1 255,255,255
C -20,0,0 1,0,0 50
L 0,0,50 1 255,255,255
cy 10,0,0 1,0.3,1 5 10 255,0,0
sp 10,10,5 5 255,255,255
pl 0,0,-10 0,0,1 255,255,255
Identifier Basic fields
A Ambient intensity, RGB color
C Position, viewing direction, field of view
L Position, intensity, RGB color
sp Center, diameter, RGB color
pl Point on plane, normal, RGB color
cy Center, axis direction, diameter, height, RGB color

Vectors and colors use comma-separated components. RGB components range from 0 to 255. The repository also contains triangle scenes and extended material parameters; use the files in scene/ as examples of the project's extended syntax.

Texture assets are stored in texture_file/. Material parsing recognizes albedo=, normal= and roughness= texture fields.

Rendering architecture

.rt scene
    |
    v
Parse and validate scene / load materials
    |
    v
Prepare camera, geometry and triangle BVH
    |
    v
Split image into horizontal bands (POSIX threads)
    |
    v
Generate rays and find nearest intersections
    |
    v
Evaluate lighting, shadows, textures and recursive reflections
    |
    v
Write image buffer and display with MiniLibX

Geometry and acceleration

The renderer implements its own vector operations, primitive intersection routines and surface-normal calculations. Triangle queries traverse spatial bounds to collect candidate triangles before exact intersection tests. The BVH specifically accelerates triangle geometry; it is not a general acceleration structure for every primitive in the scene.

Concurrency and progressive refinement

Each render pass creates workers assigned to horizontal image bands and joins them before displaying the frame. The renderer uses POSIX synchronization primitives and shared error state. During interaction, larger pixel blocks reduce the number of rays needed for a preview; subsequent passes decrease block size toward full detail.

Materials and recursive rays

Surface shading combines ambient, diffuse and specular contributions with shadow tests. Texture maps provide surface color, perturbed normals and roughness-related material information. Reflections trace secondary rays recursively up to a configured bounce limit.

Repository layout

src/
├── bvh/                    Triangle spatial partitioning and traversal
├── checker/                Scene validation
├── geometry_intersect/     Primitive intersections and normals
├── graphic_lib/            Window, image output and camera input
├── intersect/              Intersection selection and ray evaluation
├── light/                  Lighting, shadows and specular shading
├── maths/                  Vector and color operations
├── parsing/                Scene and material loading
├── refraction_reflection/  Secondary rays
├── renderer/               Camera rays and threaded rendering
├── texture/                UV mapping and texture sampling
└── utils/                  Initialization, memory and thread helpers
libft/                      Custom C utility library
scene/                      Example scenes
texture_file/               XPM texture assets
minirt.h                    Shared types, declarations and configuration

Configuration and implementation notes

Compile-time settings are defined in minirt.h:

Setting Current value Purpose
WIDTH, HEIGHT 1920, 1080 Output resolution
N_THREAD 24 Rendering worker count
BLOCK_SIZE_MAX, BLOCK_SIZE_MIN 8, 1 Preview/refinement block sizes
BOUNCE_MAX 20 Recursive ray bounce limit
DEEPTH 8 Spatial tree depth setting

Rebuild after editing configuration. The current band partitioning uses integer division of image height by thread count: choose a worker count that divides the height evenly. Thread-related constants also appear in initialization code, so changing concurrency settings requires reviewing that code.

Refraction is experimental: it currently applies to spheres, uses a hard-coded refractive index of 1.25, and returns black for total internal reflection rather than tracing a reflected ray.

Technical concepts

This project brings together C memory management, parsing, linear algebra, recursive algorithms, tree-based spatial queries, POSIX multithreading, synchronization and event-driven input handling in a single graphics application.

Authors

  • jbayonne — jbayonnegit
  • amedenec — fellow 42 project contributor

About

This project is an introduction to the beautiful world of Raytracing.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages