joriszwart.nl

I code dreams™

Math

Mandelbrot using shaders

This is a Mandelbrot zoomer done using shaders. See below for code.

No Mandelbrot for you! :-(

Ode to Mandelbrot

The shader code

Vertex shader

attribute vec3 position;

void main(void) {
    gl_Position = vec4(position, 1.0);
}

Fragment shader

precision mediump float;

uniform float width;
uniform float zoom;

void main(void) {

    float zx = 0.0;
    float zy = 0.0;

    float x;
    float y;

    gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);

    for(int i = 0; i < 50; i++) {
        x = zx * zx - zy * zy;
        y = zx * zy;

        if(zx * zx + zy * zy > 4.0) {
            gl_FragColor.r = sin(float(i) / 10.0);
            gl_FragColor.b = cos(float(i) / 10.0);
            break;
        }

        zx = x + gl_FragCoord.x / width - zoom;
        zy = 2.0 * y + gl_FragCoord.y / width;
    }

}