Skip to content

Diviini/ray-tracer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

106 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ray Tracer — C++ Photorealistic Renderer

Overview

This project implements a CPU-based ray tracer written in modern C++23.
It simulates the physics of light rays interacting with virtual 3D objects — spheres, planes, and reflective materials — to produce realistic rendered images (PNG).


How It Works (Pipeline)

Camera → Ray → Scene → Object → Material → Light → Image (PNG)

Each pixel in the image corresponds to a ray shot from the camera into the scene.
That ray may hit an object — if so, the intersection point, surface normal, and material determine the reflected color.

The rendering process repeats recursively for each reflection or bounce.


Mathematical Model

Ray Equation

A ray is represented as:

R(t) = O + tD
  • O → Origin (camera position)
  • D → Normalized direction
  • t → Distance along the ray

Sphere Intersection

The intersection between a ray and a sphere of center C and radius r is computed by solving:

|O + tD - C|² = r²

Expanded:

t²(D·D) + 2t(D·(O−C)) + (O−C)·(O−C) − r² = 0

This is a quadratic equation in t.
The smallest positive root gives the nearest intersection point.

Surface Normal

At the hit point P, the normal vector is:

N = normalize(P - C)

Diffuse Reflection (Lambertian)

Light intensity = max(dot(N, L), 0)
Final color = albedo × intensity

Specular Reflection

Reflected direction:

R = D - 2 * dot(D, N) * N

Flow Diagram

        +------------------+
        |   Camera Setup   |
        +------------------+
                 |
                 v
        +------------------+
        |  Generate Rays   |
        +------------------+
                 |
                 v
        +------------------+
        |  Intersect Scene |
        +------------------+
                 |
                 v
        +------------------+
        | Compute Lighting |
        +------------------+
                 |
                 v
        +------------------+
        | Reflect / Bounce |
        +------------------+
                 |
                 v
        +------------------+
        | Accumulate Color |
        +------------------+
                 |
                 v
        +------------------+
        |   Save Image     |
        +------------------+

Project Structure

├── include/
│   ├── core/         # Rays, hit records
│   ├── geometry/     # Spheres, planes, hittables
│   ├── materials/    # Shaders and BRDFs
│   ├── math/         # Vector operations
│   └── rendering/    # Scene, camera, raytracer
├── src/              # Implementations
├── CMakeLists.txt
└── output.png        # Final render

Build and Run

Prerequisites

  • VSCode Extension Devcontainer
  • Docker

Build

mkdir build && cd build
cmake ..
make -j$(nproc)

Run

./Raytracer

The rendered image (output.png) will be saved in the project root.


Example Scene

A simple reflective sphere over a plane lit by an ambient light.

Scene
├── Sphere(0,0,-1) radius=0.5 material=Metal
├── Plane(y=-0.5) material=Lambertian
└── Light(1,1,1) intensity=0.8

Result:
A metallic sphere reflecting the sky and the ground.


Mathematical Foundations

Concept Formula Description
Ray R(t) = O + tD Parametric representation
Intersection |O + tD − C|² = r² Find t for hit point
Reflection R = D − 2(N·D)N Perfect mirror
Diffuse I = max(0, N·L) Lambert’s cosine law
Color c = albedo × light Final pixel contribution

Architecture (Simplified UML)

+-----------------+       +----------------+
|   Raytracer     |------>|     Scene      |
+-----------------+       +----------------+
         |                        |
         v                        v
  +-------------+         +---------------+
  |   Camera    |         |  Hittable     |
  +-------------+         +---------------+
                               |
                               v
                        +-------------+
                        |   Sphere    |
                        +-------------+

Scene Configuration (scene.json)

The entire rendering setup (camera, lights, objects, materials, etc.)
is defined in a simple JSON file.

Example: scene.json

{
  "render": {
    "width": 1920,
    "height": 1080,
    "samples_per_pixel": 200,
    "max_depth": 10
  },
  "camera": {
    "look_from": [
      0.0,
      0.5,
      2.0
    ],
    "look_at": [
      0.0,
      0.0,
      -3.0
    ],
    "vup": [
      0.0,
      1.0,
      0.0
    ],
    "fov": 45.0,
    "aspect_ratio": 1.78
  },
  "lights": [
    {
      "position": [
        5.0,
        5.0,
        5.0
      ],
      "color": [
        1.0,
        1.0,
        1.0
      ],
      "intensity": 100.0
    }
  ],
  "objects": [
    {
      "type": "sphere",
      "center": [
        -1.5,
        0.0,
        -3.0
      ],
      "radius": 0.5,
      "material": {
        "type": "lambertian",
        "color": [
          0.8,
          0.3,
          0.3
        ]
      }
    },
    {
      "type": "sphere",
      "center": [
        0.0,
        0.0,
        -3.0
      ],
      "radius": 0.5,
      "material": {
        "type": "metal",
        "color": [
          0.9,
          0.9,
          0.9
        ],
        "fuzz": 0.2
      }
    },
    {
      "type": "sphere",
      "center": [
        1.5,
        0.0,
        -3.0
      ],
      "radius": 0.5,
      "material": {
        "type": "physical",
        "color": [
          1.0,
          1.0,
          1.0
        ],
        "metallic": 1.0,
        "roughness": 0.0
      }
    },
    {
      "type": "plane",
      "point": [
        0.0,
        -0.5,
        0.0
      ],
      "normal": [
        0.0,
        1.0,
        0.0
      ],
      "material": {
        "type": "physical",
        "color": [
          0.8,
          0.8,
          0.0
        ],
        "metallic": 0.0,
        "roughness": 1.0
      }
    }
  ]
}

Each section (camera, render, lights, objects) is parsed by the SceneLoader class and automatically converted into C++ objects inside the engine.


Further Improvements

Rendering & Lighting Models

  • Anti-aliasing (Monte Carlo sampling) for smoother edges
  • Depth of Field / Bokeh (simulate camera aperture)
  • Global Illumination (indirect light bounces)
  • Blinn–Phong model (specular highlights for glossy surfaces)
  • Cook–Torrance model (microfacet BRDF for realistic metals)
  • PBR Materials with roughness/metallic maps

Engine & Interface

  • Clean graphical user interface (GUI) to adjust materials and camera in real time
  • Live preview of scene updates
  • Custom scene importer/exporter
  • Configurable render settings via JSON or CLI

Performance & Extensibility

  • Multithreading (OpenMP or std::thread)
  • GPU acceleration (CUDA / OpenCL)
  • Support for texture maps (diffuse, normal, roughness)

References


Author: Hetic Master CTO & Tech Lead 2026 - Lux Aeterna Year: 2025 - 2026 Project: Ray Tracer — Photorealistic Renderer in C++23

About

Projet en C++ d'un Ray Tracer

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C++ 95.9%
  • CMake 2.6%
  • Dockerfile 1.5%