TBT – Mandelbrot

One of the last programs I wrote as an undergraduate computer science student was a Mandelbrot fractal renderer using the Escape Time algorithm for my Computer Graphics course.  The key piece is this:

//Escape Time algorithm
int mandelbrot (vec2 uv, float scale) {
	vec2 complexNumber;
	//zoom-to location:
	complexNumber.x = -1.3495-2.5*scale + (uv.x * 3.5*scale);
	complexNumber.y = -0.06489-1.0*scale + (uv.y * 2.0*scale);
	//algorithm starts here:
	vec2 z = vec2(0.0);
	vec2 zsq = vec2(0.0);
	for (int iteration = 0; iteration < MAX_ITERATIONS; iteration++) {
		if (zsq.x + zsq.y < 4.0) {
			z.y = 2.0 * z.x * z.y + complexNumber.y;
			z.x = zsq.x - zsq.y + complexNumber.x;
 
			zsq = z * z;
		}
		else {
			return iteration;
		}
	}
	return MAX_ITERATIONS;
}

In honor of Throwback Thursday, I put together a shader based on that original code, and without any fixes for things like precision issues, although I did sort of indirectly accept a challenge posted by “iq” on Shadertoy to show an aliased fractal and an antialiased fractal side by side to demonstrate the improvement.  The result was actually pretty good, and it zooms in real-time – which is a lot more than I could say for the renderer I wrote in 2003 – but the precision issues really put a damper on zooming in on things to examine them.  Pixels appear to grow as the zoom scaling gets larger as you can see here:

mandelbrot_precision_issue
Click on the image to see a larger version. Mandelbrot precision issues (visible as pixelation)

At reduced zoom levels, the fractal is sharp, even at full-screen resolution, and antialiasing seems to make it look even more appealing.

mandelbrot
Click on the image to see a larger version. As you can see, the aliasing on the left side is pretty rough.

Leave a Reply