Software · 2024

C++ Ray Tracer

A from-scratch ray tracer built in pure C++17 with no external libraries — recursive ray tracing, Blinn-Phong shading, shadow casting, mirror reflections, and 4× anti-aliasing.

C++173D RenderingZero Dependencies

Rendered Output

Interactive Demo

Drag to orbit the camera. This real-time ray tracer runs entirely in your browser, implementing the same algorithms as the C++ version above.

Renders at 480×270 with progressive refinement. Reflections limited to 3 bounces for real-time performance.

Features

Blinn-Phong Lighting

Ambient, diffuse, and specular components with configurable materials.

Shadow Casting

Shadow rays to each light source with bias to prevent surface acne.

Recursive Reflections

Recursive ray tracing with configurable depth and per-material reflectivity.

Primitives

Spheres and infinite planes, with checkerboard patterns.

Anti-Aliasing

4× supersampling for smooth, clean edges.

Configurable Camera

FOV, position, and look-at targeting with viewport projection.

Architecture

renderer.h — core shading loop
// For each pixel, cast a ray into the scene and shade what it hits.
Vec3 trace(const Ray& ray, int depth) {
    if (depth >= MAX_DEPTH) return background(ray);

    HitRecord rec;
    if (!scene.hit(ray, BIAS, 1e9, rec)) return background(ray);

    Vec3 color = rec.material.color * ambient;

    for (const auto& light : lights) {
        // Shadow ray — skip this light if the point is occluded.
        Ray shadow(rec.point, light.dir);
        if (scene.hit(shadow, BIAS, light.dist)) continue;

        // Blinn-Phong: diffuse + specular contribution.
        color += diffuse(rec, light) + specular(rec, light);
    }

    // Recursive mirror reflection.
    if (rec.material.reflectivity > 0.0f) {
        Ray refl(rec.point, reflect(ray.dir, rec.normal));
        color += trace(refl, depth + 1) * rec.material.reflectivity;
    }
    return color;
}