#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

// From Processing 2.1 and up, this line is optional
#define PROCESSING_COLOR_SHADER

// if you are using the filter() function, replace the above with
// #define PROCESSING_TEXTURE_SHADER

// ----------------------
//  SHADERTOY UNIFORMS  -
// ----------------------

uniform vec3      iResolution;           // viewport resolution (in pixels)
uniform float     iTime;                 // shader playback time (in seconds) (replaces iGlobalTime which is now obsolete)
uniform float     iTimeDelta;            // render time (in seconds)
uniform int       iFrame;                // shader playback frame

uniform vec4      iMouse;                // mouse pixel coords. xy: current (if MLB down), zw: click
uniform vec4      iDate;                 // (year, month, day, time in seconds)

//uniform float     iChannelTime[2];
//uniform vec3      iChannelResolution[2];


// Channels can be either 2D maps or Cubemaps.
// Pick the ones you need and uncomment them.


// uniform float     iChannelTime[1..4];       // channel playback time (in seconds)
// uniform vec3      iChannelResolution[1..4]; // channel resolution (in pixels)

/*
uniform sampler2D iChannel0;
uniform sampler2D iChannel1;
uniform sampler2D iChannel2;
uniform sampler2D iChannel3;
uniform samplerCube iChannel0;
uniform samplerCube iChannel1;
uniform samplerCube iChannel2;
uniform samplerCube iChannel3;
uniform vec3  iChannelResolution[4]; // channel resolution (in pixels)
uniform float iChannelTime[4]; // Channel playback time (in seconds)
*/

void mainImage( out vec4 fragColor, in vec2 fragCoord );

void main() {
    mainImage(gl_FragColor,gl_FragCoord.xy);
}

// ------------------------------
//  SHADERTOY CODE BEGINS HERE  -
// ------------------------------

vec2 cinv(in vec2 z)
{
	return (vec2(z.x, -z.y) / dot(z, z));
}

vec2 cmul(in vec2 z1, in vec2 z2)
{
	return(vec2(z1.x * z2.x - z1.y * z2.y, z1.x * z2.y + z1.y * z2.x));
}

vec2 cdiv(in vec2 z1, in vec2 z2)
{
	return (cmul(z1, cinv(z2)));
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    float myTime = iTime * 0.03;

    vec2 a0 = vec2(sin(16 * myTime) * 1.0 + 5, 0);
    vec2 a1 = vec2(sin(17 * myTime) * 2.0, 0);
    vec2 a2 = vec2(sin(18 * myTime) * 3.0, 0);
    vec2 a3 = vec2(sin(19 * myTime) * 4.0, 0);
    vec2 a4 = vec2(sin(1.1 * myTime) * 5.0, 0);
    vec2 a5 = vec2(sin(1.2 * myTime) * 6.0, 0);
    vec2 a6 = vec2(sin(1.3 * myTime) * 7.0, 0);
    vec2 a7 = vec2(sin(1.4 * myTime) * 8.0, 0);
    vec2 a8 = vec2(sin(15 * myTime) * 9.0, 0);
    vec2 a9 = vec2(sin(16 * myTime) * 10.0, sin(iTime));
    vec2 a10 = vec2(sin(17 * myTime) * 11.0,1 +  sin(myTime) * 0.2);

    vec2 a = vec2(1.0, 1 +  sin(0.16 * myTime) * 0.002);
    vec2 c = vec2(0.01, sin(iTime) / 10);
    // Normalized pixel coordinates (from 0 to 1)
    vec2 z = cmul((fragCoord/iResolution.xy - 0.5), vec2(4,0));
	vec2 init_z = z;
    z.x = abs(z.x);
	z.y = abs(z.y);
	vec2 old_z = z;
	float		i_backup;
	for (float i = 0.0; i < 16.0; i++)
	{
		z = z - cmul(a, cdiv(a0 + cmul(z, a1 + cmul(z, a2 + cmul(z, a3 + cmul(z, a4 + cmul(z, a5
                                + cmul(z, a6 + cmul(z, a7 + cmul(z, a8 + cmul(z, a9 + cmul(a10, z)))))))))),
						a1 + cmul(z, cmul(a2, vec2(2, 0)) + cmul(z, cmul(a3, vec2(3, 0))
                           + cmul(z, cmul(a4, vec2(4, 0)) + cmul(z, cmul(a5, vec2(5, 0))
                           + cmul(z, cmul(a6, vec2(6, 0)) + cmul(z, cmul(a7, vec2(7, 0))
                           + cmul(z, cmul(a8, vec2(8, 0)) + cmul(z, cmul(a9, vec2(9, 0))
                           + cmul(cmul(a10, vec2(10, 0)), z))))))))))) + c;

		if (old_z.x * old_z.x + old_z.y * old_z.y < z.x * z.x + z.y * z.y + 0.001
		&&	old_z.x * old_z.x + old_z.y * old_z.y > z.x * z.x + z.y * z.y - 0.001)
		{
			i_backup = i / 16.0;
			break;
		}
		old_z = z;
	}
	if (dot(init_z, init_z) > log(10) - log(10 - iTime) && iTime < 10)
		fragColor = vec4(0,0,0,1);
	else
    	fragColor = vec4(z.x, z.y, i_backup, 1.0);
}

// ----------------------------
//  SHADERTOY CODE ENDS HERE  -
// ----------------------------
