听说 Mandelbrot set 是 CG 界的 hello world,突然想起我还没写过,就写了一个。

// Author: idelem
// Title: Mandelbrot

#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main() {
    vec2 st = (gl_FragCoord.xy)/u_resolution.xy - 0.5;
    vec2 curr = st * 1.;
    vec2 c = curr - vec2(1.,0.);
    float iter;
    float im;
    float re;
    float r2;
    for (int i = 0; i < 24; i++){
      re = curr.x * curr.x - curr.y * curr.y + c.x;
		im = 2.0 * curr.x * curr.y + c.y;
      r2 = re * re + im * im;
      if (r2 >= 4.0){
        iter = float(i);
        break;
      }
      curr.x = re;
      curr.y = im;
    }

    gl_FragColor = vec4(iter/24.);
}

mandelbrot.png