
// Uniform variables for texturing
uniform sampler2D 	aaa_tex2d[4];

// 0 = time
// 1 = mouse X
// 2 = mouse Y
// 3 = line weight
uniform float		aaa_fu_float[8];

// 0 = iterations
uniform int			aaa_fu_int[4];


// balance between texture and effect
uniform float		aaa_fu_src;
uniform float		aaa_fu_out;


// Colors factors
// 0 = src colors
// 1 = out color
uniform vec4		aaa_fu_vec4[8];


vec3 shape( in vec2 p )
{
	p *= 2.0;

	vec3 s = vec3( 0.0 );
	vec2 z = p;
	for( int i=0; i<8; i++ )
	{
        // transform
		z += cos( z.yx + cos(z.yx + cos(z.yx+0.5*aaa_fu_float[0]) ) );

        // orbit traps
		float d = dot( z-p, z-p );
		s.x += 1.0/(1.0+d);
		s.y += d;
		s.z += cos( atan(z.y-p.y,z.x-p.x) );

	}

	return s / 8.0;
}

void main( void )
{
	vec2 pc = gl_TexCoord[0].st + 0.5;

	vec2 pa = pc + vec2(0.04,0.0);
	vec2 pb = pc + vec2(0.0,0.04);

    // shape (3 times for diferentials)
	vec3 sc = shape( pc );
	vec3 sa = shape( pa );
	vec3 sb = shape( pb );

    // color
	vec3 col = mix( vec3(0.2, 0.2, 0.2), vec3(0.7, 0.7, 0.7), sc.x );
	col = mix( col, col.zxy, smoothstep(-0.5,0.5,cos(0.5*aaa_fu_float[0])) );
	col *= 0.15*sc.y;
	col += 0.4*abs(sc.z) - 0.1;

    // light
	vec3 nor = normalize( vec3( sa.x-sc.x, 0.01, sb.x-sc.x ) );
	float dif = clamp(0.5 + 0.5*dot( nor,vec3(0.5773) ),0.0,1.0);
	col *= 1.0 + 0.7*dif*col;
	col += 0.3 * pow(nor.y,128.0);

    // vignetting
	col *= 1.0 - 0.1*length(pc);

	vec4 color = vec4( col, 1.0 ) ;

	vec4 src = (texture2D(aaa_tex2d[0], gl_TexCoord[0].st) * aaa_fu_src) * aaa_fu_vec4[0];
	vec4 fx  = (color * aaa_fu_out) * aaa_fu_vec4[1];

	gl_FragColor = vec4( (src + fx) );
}




