/*

File: Mandelbrot.fragment

Abstract: Mandelbrot Shader (from the "orange book")
			 
Copyright (C) 2002-2006  3Dlabs Inc. Ltd.
All rights reserved.

See 3Dlabs-License.txt for license information

*/

uniform vec2 center;
uniform float zoom;
varying vec3 Position;


void main() {
        float real    = (Position.x * zoom + center.x);
        float imag    = (Position.y * zoom + center.y);
        float Creal   = real;
        float Cimag   = imag;

        float r2      = 0.0;
        float iter;
        float tempreal;

        vec3  InnerColor  = vec3(0.0, 0.0, 0.0);
        vec3  OuterColor1 = vec3(0.0, 0.0, 0.4);
        vec3  OuterColor2 = vec3(1.0, 1.0, 1.0);

        vec3  color;

        for (iter = 0.0; (iter < 15.0) && (r2 < 4.0) ; iter += 1.0) {
                tempreal = real;

                real = (tempreal * tempreal) - (imag * imag) + Creal;
                imag = (2.0 * tempreal * imag + Cimag);
                r2   = (real * real) + (imag * imag);
        }

	tempreal = fract(iter * 0.1);

	if (r2 < 4.0)
          color = InnerColor;
     else
          color = mix(OuterColor1, OuterColor2, tempreal);

	gl_FragColor = vec4(color, 1.0);


}