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
0:00500 Lines of Code That Draw 3D Without a Graphics Card
0:12Hand it triangles. It writes out a shaded 3D image.
0:23It starts as three white dots. You build every step to the face.
0:33Step one: color every screen dot a triangle covers.
0:53Step two: one number per dot decides what hides behind what.
1:13These two steps are what your graphics chip does every frame.
1:29A list of triangles. Two simple steps.
Show transcriptHide transcript
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.
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 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.
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.
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.
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.
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.
Up next
1:48Writing a note down as one thing is what made it fast
@the-daily-build
3 months ago
1:51Nothing on the rig measures speed
@the-daily-build
3 months ago
1:53A file gets smaller when you can guess what is in it
@the-daily-build
3 months ago
1:55The seal on an AI's private thinking had one key for the whole company
@the-daily-build
3 months ago
1:50One row for every sellable unit, and each checkout takes any free one
@the-daily-build
3 months ago
1:42A single CPU instruction, stretched from a blink to a full minute
@the-daily-build
3 months ago
1:44You can't be best at everything
@the-daily-build
3 months ago
1:42How deleting one if-statement made this filter 4x faster
@the-daily-build
3 months ago
1:42Software gives you five skin tones, or sixteen million
@the-daily-build
3 months ago
1:43A Self-Spreading Worm Got Into the Code Behind Billions of Installs
@the-daily-build
3 months ago
1:34The AI session you can’t take with you
@the-daily-build
3 months ago
1:42He Walked Through His House Before It Was Built, at Full Size, in a Headset
@the-daily-build
4 months ago