Skip to content

Weekend Ray Tracer

You'll write a renderer from the math of light by hand, with spheres, matte, metal and glass materials, shadows and a camera with focus blur, in about five hundred lines.

About

A ray tracer asks, for every pixel, what the light coming through it would have bounced off. It's about five hundred lines, no GPU, and no libraries except a way to write a file. Peter Shirley's free book Ray Tracing in One Weekend is the standard route and you should follow it, but type every line yourself and stop to understand why the sphere intersection is a quadratic. The classic trap is shadow acne, the speckled surface you get when a bounced ray hits the surface it just left. Nudge the start point by a tiny amount and it goes away.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
6
Takes
One weekend
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
Nothing special
Where
Around town
Other people
With other people
Locked for now
Earn two more Common badges in Graphics Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Shoot one ray per pixel and hit a sphere. Solve the ray-sphere quadratic yourself and color the pixel by the surface normal, which gives the sphere a rainbow look that tells you the normals are right. Take a screenshot of the normal-colored sphere on a gradient sky. Photo
  2. 2
    Add a matte material with random bounces and show the noise falling with more samples. Bounce each ray in a random direction off the surface, average several samples per pixel, and render the same scene at 1, 10 and 100 samples. Take a screenshot of the three renders side by side with the sample counts labeled. Photo
  3. 3
    Add metal and glass, with fuzz on the metal and refraction in the glass. Metal reflects around the normal, and glass refracts by Snell's law and sometimes reflects instead. The glass sphere that looks like a black hole has the refraction ratio upside down. Take a screenshot of three spheres, matte, metal and glass, in one row. Photo
  4. 4
    Build a positionable camera with depth of field. Pick a point to look from, a point to look at, a field of view and an aperture, and start rays from a disc rather than a point so anything off the focus distance blurs. Take a screenshot with one sphere sharp and the rest soft. Photo
  5. 5
    Render the cover scene with a hundred random spheres and time it. Lay out small spheres on a plane with random materials and three big ones in the middle. At 1200 pixels wide and 100 samples it takes an hour or more in a compiled language and days in Python, which is the problem the Rare badges solve. Take a screenshot of the final image with the render time written in the post. Photo
  6. 6
    Set it as someone's wallpaper, or print it for them. Render it at their screen size and put it on their desktop or phone, or print it and hand it over. Take a photo of the image on the other person's screen or in their hands. Photo