Polygon tutorial materials

Note: Some of these files are in-progress versions from the production of this video series. This is why some code may appear different compared to what was shown on video.
include files
rasterize.hh RasterizeTriangle(), the generic triangle rasterizer that will remain unmodified for the entire series; updated in episode 3 to also include RasterizePolygon()
math.hh Vector mathematics, used since episode 2 (examples 05 onwards)
view.hh class View, used since episode 3 (refactoring)
slope.hh class Slope, used since episode 3 (refactoring)
polygon_clip.hh ClipPolygon(), used since episode 3 (refactoring)
polygon_draw.hh DrawPolygon(), used since episode 3 (refactoring)
polygon_tesselate.hh TesselateAndDraw(), used and also obsoleted since episode 3 (refactoring)
example programs
00-test-singlecolor.cc Example 00: (Episode 1 or P1) Single-color polygon renderer, proof of concept
01-test-singlecolor.cc Example 01: (Episode 1 or P1) Single-color polygon renderer, after refactoring
02-test-rgb-scanlines.cc Example 02: (Episode 1 or P1) Gouraud shading, before fixing
03-test-rgb-full.cc Example 03: (Episode 1 or P1) Gouraud shading, after fixing
04-generic.cc Example 04: (Episode 1 or P1) Gouraud shading, after refactoring
05-texture-flat.cc Example 05: (Episode 2 or P2) Texture mapping (affine, 2D demo)
06-scene-flat.cc Example 06: (Episode 2 or P2) Texture mapping (affine, 3D demo)
07-scene-perspective-corrected.cc Example 07: (Episode 2 or P2) Texture mapping (perspective corrected, 3D demo)
08-scene-zbuffer.cc Example 08: (Episode 2 or P2) Depth map (z-buffer)
09-scene-antialias-dither.cc Example 09: (Episode 2 or P2) Bilinear filtering (approximated using quick dithering)
10-scene-antialias-interp.cc Example 10: (Episode 2 or P2) Bilinear filtering (real, using linear interpolation)
11-scene-polygon.cc Example 11: (Episode 2 or P2) Tesselation, so scene can be constructed from arbitrary convex polygons
12-scene-clip.cc Example 12: (Episode 2 or P2) With clip planes
13-bonus.cc Example 13: Higher resolution, the rounding error fix patch applied, backface culling enabled, dithering enabled
14-refactor.cc Example 14: (Episode 3 or R) Refactored version
15-thatscene.cc Example 15: (Episode 4 or L1) That scene (various versions, edit Plot() to enable/disable things like fog, edit LightCoord to enable moving lights)
16-lightmap.cc Example 16: (Episode 5 or L2) Lightmap with RGB24 rendering
17-lightmap-rgb.cc Example 17: (Episode 5 or L2) Lightmap with RGB96f rendering
off-screen include files
mesh.hh 00—04 Off-screen code: CreateTriangleMesh(), creates the screen-filling mesh seen in first demonstrations
mousemanip-part1.hh 06 Off-screen code: Mouse manipulation, used in episode 2
mousemanip-part2.hh 06 Off-screen code: Mouse manipulation, used in episode 2
bugfixes and feature additions
patch-fix-rounding-error.txt 04—13 (Bugfix) A patch for fixing rounding errors that cause staircased textures even when bilinear filtering is used
patch-fix-rounding-error2.txt 14—17 (Bugfix) A patch that corrects the earlier fix for rounding errors, now also fixing the lightmap camera position, together with other patches fixes the spurious streak artifacts
patch-fix-diagonal-beams.txt 17 (Bugfix) A patch for fixing the “diagonal beams” in lightmap, which is caused by the rectilinear projection without compensating for said projection. Backstory in separate article
patch-fix-clipping-seams.txt 12—17 (Bugfix) A patch for fixing seams between clipped polygons, addressing a bug that causes glitches in lightmaps and jitter to appear when rendering at low resolution
patch-fix-view-edge.txt 08—17 (Bugfix) A patch for fixing the right and bottom edge of the viewport, addressing a bug that causes glitches in lightmaps and jitter in low resolution views
patch-fix-view-nearclip.txt 08—17 (Bugfix) A patch for pulling the near clipping plane closer to camera, addressing a bug that causes glitches in lightmaps
patch-lightmap-speed-hack.txt 17 (Feature) A patch that makes (most of) the lightmaps converge faster by calculating the first few iterations at a significantly reduced resolution and bilinearly interpolating to hide the fact
patch-fix-lightmap-camera-positioning.txt 17 (OBSOLETE Bugfix) A patch that fixes the camera positioning for the lightmap calculator by solving it exactly — together with the other patches this fixes the spurious streak artifacts (apply on example 17).
If you are not using the speed-hack patch, you need to add float half=.5f; right before the line where the compiler complains about the missing identifier.
patch-add-bilinear-filtering.txt 17 (Feature) A patch that adds bilinear filtering in example 17.
patch-fix-clang.txt 17 (Feature) A patch that formats some code differently to enable compilation on Clang 11. Not sure if those are bugs in my code or shortcomings in the compiler. Some of them seemed strange.
git clone https://iki.fi/bisqwit/jkp/polytut/.git
You can use this command to fetch the exact copy of the source code shown in the video at ~25 different phases. The repository contains about a hundred branches, which are numbered (biggest number=latest). This is not an accident. My workflow automatically generates a new branch every time I re-generate the video, and in each of these branches it creates a commit whenever the footage pauses. With some effort, you can thus get an impression of one aspect of the development progress of this series (since April 2020, when I added this feature).

To compile:

To run:

Note: You will need GCC version 10.0.1 or Clang++ version 11 or newer to compile it. You also need libSDL 2. (e.g. apt-get install libsdl2-dev) The pkg-config part inserts into the commandline all the necessary options for compiling and linking, even if your SDL2 is installed in an unconventional path. If you use csh, replace $() with backticks.

GCC version 8 or 9 may also be used, provided that you change the following (these instructions are only valid for examples 04 and earlier):

GCC version 7 may also be used, provided you change the following in addition to the changes listed above: Clang++ 9 may also be used, provided that you change the following in addition to the changes listed above: There are no workarounds that enable compilation on Clang++ 8 or earlier. (No support for lambda templates, required for the compile-time for loop)
There are no workarounds that enable compilation on GCC 6 or earlier. (No support for C++17, such as structured binding declarations or std::apply)


Copyright © 2020 Joel Yliluoma — https://iki.fi/bisqwit/