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


// 0 = time
// 1 = mouse X
// 2 = mouse Y
uniform float		aaa_fu_float[6];

// 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];


// Gamma correction
const float GAMMA = 2.2;
const float tau = 6.28318530717958647692;

vec3 ToLinear( in vec3 col )
{
	// simulate a monitor, converting colour values into light values
	return pow( col, vec3(GAMMA) );
}

vec3 ToGamma( in vec3 col )
{
	// convert back into colour values, so the correct light will come out of the monitor
	return pow( col, vec3(1.0/GAMMA) );
}

vec4 Noise( in ivec2 x )
{
	return texture2D( aaa_tex2d[0], (vec2(x)+0.5)/256.0, -100.0 );
}


void main(void)
{
	vec3 ray;
	ray.xy = gl_TexCoord[0].st - 0.5;
	ray.z  = 1.0;

	float offset = aaa_fu_float[0]*0.5;
	float speed2 = (cos(offset)+1.0)*2.0;
	float speed = speed2+0.1;
	offset += sin(offset)*0.98;
	offset *= 2.0;


	vec3 col = vec3(0);

	vec3 stp = ray/max(abs(ray.x),abs(ray.y));

	vec3 pos = 2.0*stp+0.5;
	for ( int i=0; i < 32; i++ )
	{
		float z = Noise(ivec2(pos.xy)).x;
		z = fract(z-offset);
		float d = 50.0*z-pos.z;
		float w = pow(max(0.0,1.0-8.0*length(fract(pos.xy)-.5)),2.0);
		vec3 c = max(vec3(0),vec3(1.0-abs(d+speed2*.5)/speed,1.0-abs(d)/speed,1.0-abs(d-speed2*.5)/speed));
		col += 1.5*(1.0-z)*c*w;
		pos += stp;
	}


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

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








