500 Lines of Code That Draw 3D Without a Graphics Card

2 months ago

Every 3D image you see, in a game or on a phone, starts as a list of triangles. This is the whole machine that turns them into a picture, in about five hundred lines of code.

Ask

Ask about this presentation

Answers are generated from this presentation.

Chapters

  1. 0:00500 Lines of Code That Draw 3D Without a Graphics Card
  2. 0:12Hand it triangles. It writes out a shaded 3D image.
  3. 0:23It starts as three white dots. You build every step to the face.
  4. 0:33Step one: color every screen dot a triangle covers.
  5. 0:53Step two: one number per dot decides what hides behind what.
  6. 1:13These two steps are what your graphics chip does every frame.
  7. 1:29A list of triangles. Two simple steps.
Show transcript

500 Lines of Code That Draw 3D Without a Graphics Card

A complete software renderer, built from scratch, that turns a list of triangles into a shaded image
~500 lines of code
software renderer C++ zlib-licensed

Every 3D image you see, in a game or on a phone, starts as a list of triangles. This is the whole machine that turns them into a picture, in about five hundred lines of code.

Hand it triangles. It writes out a shaded 3D image.

haqr.eu/tinyrenderer
No graphics card. No game engine.

It is a renderer: a program that turns those triangles into a picture. Hand it a face, and it writes out one image, lit and shaded. No graphics card, no game engine.

It starts as three white dots. You build every step to the face.

the first program
→
you write every step
the finished head
haqr.eu/tinyrenderer

It starts at almost nothing. The first program draws three white dots on black. Everything between those dots and that shaded face, you write yourself, step by step.

Step one: color every screen dot a triangle covers.

Do it for every triangle and the outline fills into a solid surface.

The screen is a grid of dots. To draw one triangle, the program checks each dot: is this dot inside the triangle? If the dot is inside, the program colors it, blending the triangle's three corner colors by how near the dot sits to each. Do that for every triangle, and the outline fills into a solid surface.

Step two: one number per dot decides what hides behind what.

Keep the new color only if that surface is closer.

The second step decides what hides behind what. Drawn in any order, the back of the head shows through the front. The fix is one number per dot: how far the nearest surface sits. That number is the z-buffer. Before coloring a dot, the program keeps the new color only if that surface is closer. That is why the near cheek hides the far ear.

These two steps are what your graphics chip does every frame.

The whole pipeline in plain words: triangles, cover the pixels, color each, keep the nearest, an image
This tiny program is that same machine, slow enough to read.
haqr.eu/tinyrenderer

Those two steps, cover the dots and keep the nearest, are what the graphics chip in your phone runs, for every frame you see. This tiny program is that same machine, slow enough to read. A few more steps reach soft shadows like these.

A list of triangles. Two simple steps.

How a computer draws in 3D, in about 500 lines.

haqr.eu/tinyrenderer · github.com/ssloy/tinyrenderer zlib · free for any use
by Dmitry Sokolov
A list of triangles. Two simple steps.

It is free to read and reuse. A list of triangles, two simple steps, and you can see exactly how a computer draws in three dimensions.