Software Teapot
You'll fill triangles into a pixel buffer with your own rasterizer, add a depth buffer and a light, and shade a classic test model without touching the GPU.
About
The GPU is a very fast version of about two hundred lines of code, and once you've written those lines you know what it's doing. This is the software rasterizer, in the style of the free tinyrenderer course: barycentric coordinates to fill a triangle, a z-buffer to sort out what's in front, and a dot product with a light to shade it. The first teapot will have holes along shared edges, which is the top-left fill rule you haven't written yet, and nobody believes the rule matters until they've seen the holes. Write it in whatever language you like, but a compiled one will give you frames instead of seconds.
At a glance
- Per requirement
- 25 XP
- Completion bonus
- 150 XP
- Requirements
- 5
- Takes
- One weekend
- Skill level
- Intermediate (months of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Fill a single triangle using barycentric coordinates. For each pixel in the triangle's bounding box, work out the three barycentric weights and color the pixel if all three are at least zero. Interpolate three corner colors with the same weights. Take a screenshot of the filled triangle with the colors blending. Photo
-
2
Draw the teapot with random flat colors, then add a z-buffer. Without a depth buffer, triangles from the back draw over the front and the model looks inside out. Keep one float per pixel, write the depth, and skip any fragment that's farther away. Take a screenshot of the model before and after the z-buffer, side by side. Photo
-
3
Light it with a single directional light and the face normal. Brightness is the dot product of the normal and the light direction, clamped at zero. Faces turned away go black, which is correct for now. Take a screenshot of the lit model. Photo
-
4
Smooth the shading with per-vertex normals. Average the normals of the faces around each vertex, or read the vn lines from the file, and interpolate them across each triangle before lighting. The faceted look goes away. Take a screenshot of flat and smooth shading side by side. Photo
-
5
Spin it in a window at a frame rate you measured. Copy the buffer to a window each frame and print the milliseconds per frame on screen. A six thousand triangle model should manage 30 frames a second at 800 by 600 in a compiled language, and a few frames a second in Python is honest too. Film a few seconds with the counter visible. Video