Skip to content

Tunnel Effect

You'll precompute a distance table and an angle table once, then use them to pull a texture down an endless tunnel every frame without doing the math again.

About

The tunnel taught the scene its best habit: do the expensive work once, store it, and look it up forever after. Each pixel on screen maps to one point on the inside of a pipe, and that mapping never changes, so you compute it at startup into two tables and the frame loop becomes two lookups and a copy. What counts is your own code in any language with a pixel buffer, a texture you drew by code, and tables you built. A shader does not count here, because on the GPU the tunnel is a one-liner and the lesson disappears. The thing nobody tells beginners is that the exact centre of the screen is a divide by zero, so clamp the distance or put the eye a pixel off centre.

At a glance

Per requirement
15 XP
Completion bonus
75 XP
Requirements
5
Takes
One evening
Skill level
Beginner (done it a few times)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own

Requirements

  1. 1
    Draw a 64 by 64 texture by code that tiles in both directions. An XOR pattern (x xor y) or a checkerboard is enough, and a few bands of colour from your palette help you see the motion later. Test the tiling by drawing it four times in a two by two block, because a seam between the copies becomes a seam down the whole tunnel. Take a screenshot of the block. Photo
  2. 2
    Build the distance and angle tables and draw them as greyscale. For every pixel take dx and dy from the centre. Distance is a constant (try 32 times the texture height) divided by the square root of dx squared plus dy squared, and angle is atan2(dy, dx) scaled so a full turn runs from 0 to the texture width. Store both as integers and clamp the centre pixel. Draw each table to the screen as grey and take a screenshot of the two side by side; the distance one should look like a glowing hole and the angle one like a sliced pie. Photo
  3. 3
    Map the texture through the tables and make it move. Each frame, for each pixel, read u from the angle table and v from the distance table, add a time offset to v so the tunnel comes toward you, and copy the texel at (u mod 64, v mod 64) into the buffer. Add a second offset to u and the tunnel twists. If it runs slowly, you are doing the math per frame instead of per table. Film ten seconds. Video
  4. 4
    Bend the tunnel by sliding a window over oversized tables. Compute the tables at twice the screen size, then each frame read from a window whose top left corner moves on a sine of time. The centre swings around the screen and the pipe appears to curve, for the cost of two additions per frame. Film it bending. Video
  5. 5
    Shade the far end to black, record 30 seconds and post the source. Darken each pixel by its distance table value so the hole in the middle goes dark, either through a shade lookup or by scaling the colour. Record 30 seconds, link the source, and write how long the tables take to build at startup, because on a 386 that number was the loading screen. Video