Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Glide shading

The world renderer calculates lighting at polygon corners and combines the interpolated lighting colors with textures, fog and framebuffer color precision.

Flat and smooth shading

Lighting produces an RGB color at each source corner before clipping and rasterization. The rasterizer interpolates these colors across each submitted triangle, a process called Gouraud shading. It interpolates lighting colors rather than normals; it does not evaluate the lighting formula anew per pixel.

Normal selection is a separate step. A face normal shared by all corners gives the same directional-light contribution at each corner. Different corner normals can give different contributions and a smoother appearance across neighboring faces. Local lights also depend on corner positions, so a shared face normal does not require identical corner colors or a uniformly lit face.

Terrain constructs a shading normal at each grid vertex from the normalized normals of its incident triangles, then normalizes the sum. These operations use the engine’s quantized reciprocal square root. Models instead consume supplied normals, retaining their magnitude. The model format describes face normals, vertex normals, corner indirection and face-normal fallback.

Color pipeline

  1. Prepare corner lighting from ambient RGB, directional light and, for models, face brightness and admitted local lights. Retained instance prelighting can replace part of the model calculation; actor eligibility depends on the runtime model kind and attributes, not simply whether an actor is stationary.
  2. Clip polygons while carrying their corner RGB and texture coordinates, then project and submit the surviving polygons as triangle fans.
  3. Interpolate lighting RGB linearly in screen space. Texture coordinates use perspective correction. Sampling uses the selected palette, allocation and address rules, and the filtering mode selected by the drawing path.
  4. Modulate sampled texture RGB by the interpolated lighting. Solid-color model faces incorporate their resolved palette color into corner lighting instead. Texture filtering and shading are independent: a smoothly lit surface can still use point-sampled textures.
  5. Apply fog on encoded RGB channels and the framebuffer’s RGB565 quantization and dithering rules. These are separate from the lighting calculation. The world lighting path has no scene exposure or filmic tone-mapping stage.

This describes the ordinary model and terrain color paths. Placed flats have their own constant-alpha blending and ordering rules. Sky and sun use separate drawing and blending paths. Water changes terrain geometry and shading normals; its displacement and normal-update rules are specified separately.

Detailed rules

TopicReference
Model normals and hard edgesModel shading selection
Model brightness, directional light and local lightsModel lighting
Retained map-light colorsWorld prelighting and actor prelighting
Terrain lighting and averaged normalsTerrain vertex calculation and shading normals
Clipping, projection and color gradientsModel clipping and projection, terrain preparation, screen-space color setup and view projection
Texture coordinates, filtering and modulationModel texture addressing, model draw flags, terrain texture setup and texture combination
World palette conversionCOL palette precision and model palette ownership
Fog, framebuffer precision and ditheringFog lookup and integer precision
Camera-facing flats and transparencyPlaced flat drawing and framebuffer blending
Sky and sunSky plane and sun framebuffer blending
Animated terrain and texturesWater waves and texture animation