Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

C++advancedcliAI generated

PPM Ray Tracer Renderer

Build a ray tracer in C++ from scratch, with no graphics libraries, that renders a 3D scene of spheres and planes into an image file. For every pixel, the program shoots a ray from the camera into the scene, finds the closest object it hits, and calculates the color from the lights: matte and shiny surfaces, shadows, and mirror-like reflections traced recursively. The result is written as a PPM, a simple image format you can open or convert to PNG. You'll learn 3D vector math, ray-object intersection, the Phong lighting model, recursion, and making a computation-heavy program fast with multiple threads.

Estimate
~15h
Steps
7
Completed by
0
Proposed by
codeseed.app

Standard Library (STL) · manual vector math

Project roadmap

  1. 01

    Write the vector math and PPM output

    ~2h

    Create a Vec3 class with addition, subtraction, multiplication by a number, dot product, cross product, length and normalize, used for points, directions and colors. Write a function that saves a width×height buffer of colors as a PPM file (P6 binary), clamping values to 0 to 255. Render a simple gradient to test it. Done when: the program writes a gradient image that opens correctly, and the vector tests pass.

  2. 02

    Intersect rays with objects

    ~2.5h

    Create a Ray struct (origin and direction) and an abstract Shape class with a hit function returning the distance to the hit and the surface normal. Implement Sphere (solve the quadratic equation, taking the nearest positive root) and Plane. Ignore hits very close to zero distance to avoid self-intersection. Done when: unit tests confirm rays hit and miss spheres and planes at the expected points.

  3. 03

    Build the camera and scene

    ~2h

    Create a Scene holding shapes (with a material: color, how diffuse, how shiny, how reflective) and point lights. Create a Camera with a position, a look-at point and a field of view, which produces the ray for each pixel. For each pixel, find the closest hit, and color it with the object's flat color to start. Done when: the render shows flat-colored spheres on a plane from the chosen camera angle.

  4. 04

    Add shading and shadows

    ~2.5h

    Implement Phong lighting at each hit: ambient light, diffuse light (brighter where the surface faces the light) and a specular highlight (a bright spot on shiny surfaces). For each light, send a shadow ray towards it and skip that light when something is in the way. Done when: the render shows shaded spheres with highlights and shadows on the plane.

  5. 05

    Add reflections

    ~2h

    For reflective materials, calculate the reflected direction, trace a new ray recursively, and blend its color with the surface's own color according to how reflective it is. Stop at a maximum depth, for example 5, so mirrors facing each other don't recurse forever. Done when: a mirror sphere shows reflections of the other objects.

  6. 06

    Make it faster and smoother

    ~2h

    Split the image into rows and render them on several threads with std::thread (each thread writes only its own rows). Add anti-aliasing by averaging several slightly offset rays per pixel, as an option. Measure and print the render time with std::chrono. Done when: rendering with all CPU cores is several times faster than with one thread, and edges look smoother with anti-aliasing.

  7. 07

    Load scenes and render a demo

    ~2h

    Read the scene (camera, lights, shapes and materials) from a simple text file, so scenes can be changed without recompiling, with clear errors for bad lines. Build a demo scene with several materials, shadows and reflections, and add the rendered image and the command to reproduce it to the README. Done when: the demo scene renders from its file, and the image is in the README.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~15h · 7 steps

Tech stack

Standard Library (STL)manual vector math