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

4 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 about 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
  1. 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.

    Slide text
    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
  2. 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.

    Slide text
    haqr.eu/tinyrenderer
    No graphics card. No game engine.
  3. 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.

    Slide text
    the first program
    →
    you write every step
    the finished head
    haqr.eu/tinyrenderer
  4. 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.

    Slide text
    Do it for every triangle and the outline fills into a solid surface.
  5. 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.

    Slide text
    Keep the new color only if that surface is closer.
  6. 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.

    Slide text
    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
  7. 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.

    Slide text

    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.

What happened in AI today, in your inbox

One short email each evening. Read it and you’re caught up.

Built with ❤️ in New York