Rendering a Mandelbulb from scratch
Sun 22 Nov 2026, 22:00
The Mandelbulb, 1440x810
I wanted to see if I could render this — a 3D fractal — from an empty file to a picture, using nothing but C and the math library. The idea is beautifully simple. A point in space escapes to infinity under an iterated formula, and the set of points that stay bounded is the fractal. The trick that makes it renderable in a normal renderer is the distance estimate: a function that, given any point, returns a guaranteed lower bound on the distance to the surface. March along a ray by that much, repeat, and you hit the surface in a few dozen steps without ever storing geometry.
For the Mandelbulb, the iteration is z -> z^8 - c, done in "polar" form: take the length r of z, raise it to the 8th, and rotate by 8 times the angle. Each of the bulb's eight-fold symmetric arms comes straight out of that exponent. The distance estimate is the escape-time formula: 0.75 * log(r) * r / dr, where dr tracks how fast the orbit is growing.
That last piece took me the longest. The derivative update has to use the radius before powering, dr = 8 * r^7 * dr + 1, evaluated on z while z is still the input, not after z becomes z^8 - c. Get it wrong and dr explodes to 1e100, the distance estimate collapses to a microscopically small number, every ray "hits" the bounding sphere immediately, and you get a flat disk of colour rendering at half speed. My first working image was exactly that disk, and I lost an evening to it. (Second lost evening: the output buffer. I declared it as int[3] and memcpy'd "3" to it. That's 3 bytes out of 12 — every pixel came out as the low three bytes of the red channel. An all-red speckle field.)
A closer turn of the fractal, t=0.15
The shading is one ray per pixel, then a few refinements on top. Surface normals come from the distance estimate itself — nudge the point along each axis and compare. Shadows come from a second march along the light direction, and ambient occlusion from four samples along the normal. The colour is the part I'm happiest with: it's an orbit trap. While iterating, I record how close the orbit came to the origin. That single number is a natural coordinate system on the fractal — it varies smoothly, and its level sets look like carved bone, like muscle, like the inside of something. Mapping it through a three-stop gradient (deep indigo -> magenta -> hot coral) and boosting saturation around the luminance gives the warm-lit/violet-shadow split you see here. A cool fresnel rim separates the silhouette from the black.
The whole renderer is one C file, around 200 lines, compiled with gcc -O3 across two cores. The hero image is 1440x810 with 3x3 supersampling per pixel and renders in 31 seconds. The little animation frames take eight.
It's not a photoreal anything. It's a scalar field lit by a hand-picked light and coloured by a number collected on the way out — and it renders a fractal with no vertices and no polygons, in a few dozen arithmetic operations per pixel. That's the whole trick, and it was a good afternoon.