Skip to content

Wireframe Cube

You'll project eight corners onto a flat canvas with your own perspective math and draw the twelve edges spinning in 3D.

About

Perspective projection is one division: things farther away get smaller. Once you've written that division yourself you'll understand what every projection matrix in every engine is doing. You'll build the 4 by 4 matrices for rotation, translation and perspective by hand, and draw the cube with the line routine from Pixels to a File or a canvas line call. No depth buffer and no filled faces yet, because the wireframe shows you the math clearly and a filled cube would hide the mistakes. The cube that looks squashed is the one where you forgot the aspect ratio.

At a glance

Per requirement
15 XP
Completion bonus
75 XP
Requirements
5
Takes
Two evenings
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own

Requirements

  1. 1
    Write a 4 by 4 matrix type with multiply, and test it. Multiplying by the identity should give the same matrix back, and rotating by 90 degrees four times should get you home. Write those two as tests, since a matrix bug anywhere later in the track starts here. Take a screenshot of the tests passing. Photo
  2. 2
    Project the eight corners and draw the twelve edges. Move the cube a few units down the z axis, divide x and y by z, scale to the canvas, and connect the corners. If you see a square inside a square, it works. Take a screenshot of the cube. Photo
  3. 3
    Rotate it around two axes at once and account for the aspect ratio. Combine a rotation around y with one around x, and multiply x by height over width so the cube stays a cube in a wide window. Film it spinning for a few seconds. Video
  4. 4
    Move the camera instead of the cube. Build a view matrix that walks the camera in a circle around a still cube, which is the inverse of moving the cube. If the cube goes the wrong way, that's the inverse you forgot. Film the orbit. Video
  5. 5
    Load a model from an OBJ file and draw it as wireframe. The Utah teapot is a free download with about six thousand triangles. Parse the v and f lines yourself, since the format is plain text, and watch for the indices that start at one rather than zero. Take a screenshot of the teapot in wireframe. Photo