Lessons and the learning game

Computer graphics from scratch

Draw it all by hand on an ESP32-C3 screen: a framebuffer, Bresenham lines, filled circles, matrices and perspective, then a spinning station.

How it works

Computer graphics from scratch uses no graphics library. Every lesson keeps a framebuffer in RAM, 160 by 160 bytes with a color number in each, draws into it with code you can read, and sends the whole buffer to a 2.4 inch ILI9341 color screen in one window.

The circuit is the one the handheld console course starts with: an ESP32-C3 and the screen on four SPI wires (GPIO 18 to 21). The boss adds the game pad. Every lesson opens a working sketch. You run it, read what the serial monitor prints (pixel counts, coordinates, timings and a checksum of the framebuffer), then change a constant and run again. The checks read those lines, so every number a lesson quotes is one the board really printed.

The lessons

  1. Framebuffers: a picture as an array of bytes, indexed color, clipping, counting pixels, a checksum that proves an exact image, and why one flush of 25,600 pixels takes 36 ms down a bit-banged wire.
  2. Bresenham lines: one pixel per step of the longer side, the error term traced pixel by pixel, a race against a float line (27 ms against 2 ms on a chip with no FPU), steep lines and a starburst.
  3. Circles and fills: the midpoint circle with eight-way symmetry, discs from horizontal spans, and a flood fill with a ring-buffer queue that paints ten thousand pixels while holding 119.
  4. Transforms: 2D matrices that turn, move and scale, why the order of multiplication matters, and a satellite outline spinning on the screen.
  5. 3D projection: divide by depth, a cube's corners projected by hand, near faces bigger than far ones, moving the camera and changing the lens.
  6. Wireframes: points and edges, the turn, project, clear, draw, flush loop at 26 frames a second, a profile of every frame, and your own model.

The boss: a spinning space station

The last lesson builds a space station in code from a few numbers (a hub, a ring, four spokes and two solar panels on masts) and flies it from the game pad. LEFT and RIGHT set the spin, UP and DOWN tilt it toward you, and A turns on depth shading so the far side reads dimmer. You finish by changing one number to rebuild the ring with more segments, and it ends with ideas: a docking ship, a second ring, a longer mast.

What the ESP32-C3 does differently

  • It has no floating-point unit. Every float is a software routine, which is why the course draws with integers and only uses floats for the few numbers per frame that need them.
  • The screen is driven by bit-banged SPI: every bit is a pin change made by the program. A full 160 by 160 flush is 409,600 bits and dominates every frame, so the profiling lesson finds the wire, not the math, is the limit.
  • It has 400 KB of SRAM, so a 25,600 byte framebuffer fits easily. A whole 240 by 320 screen at 16 bits a pixel would need 153,600 bytes, which is why the buffer holds color numbers instead.
  • The sketches run on Mokxi's own C3 runtime, with cosf, sinf, atanf and lroundf from its math library and micros() for timing.

After this course

Write a physics engine uses the same screen and framebuffer to make things move: vectors, time steps, collisions and springs, ending with marbles you tip around with a motion sensor.