Archive

CPU ray tracer

One of my first projects in ray tracing, and a good foundation for what came after.

Tech
C++
CPU ray tracer

Features

  • Geometry. Intersection with spheres, planes and triangle meshes, with front-face, back-face or no culling.
  • Materials. Lambert, Phong and Cook-Torrance.
  • Lighting. Point and directional lights, with hard shadows. Each light and its shadows can be toggled.
  • Viewing. Camera control with keyboard, or keyboard and mouse, and scene cycling.

Physically based materials

The Cook-Torrance material combines three terms, each from a common model:

  • Fresnel. Schlick's approximation, starting from 4% reflectance for dielectrics and from the albedo for metals.
  • Distribution. Trowbridge-Reitz GGX, driven by roughness.
  • Geometry. Schlick-GGX applied to both the view and light directions, with roughness remapped for direct lighting.

Dielectrics add a Lambert diffuse term weighted by whatever Fresnel doesn't reflect, so light isn't counted twice. One test scene shows the range: rough silver, matte gold and shiny zinc, against rough green, matte blue and shiny red.

Seeing each part

A render mode switch shows irradiance only, the BRDF only, or both combined, which made it much easier to tell whether a problem came from the lights or the materials.

Shadows

Each hit casts a shadow ray towards every light. A blocker only counts if it's closer than the light itself, so objects behind a point light don't shadow the scene.