uniform sampler2D tex0;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform vec2 screensize;
uniform int time;

uniform float cover;
uniform float alpha;

void main(void)
{
	float fadeInAlpha = 1.0;

	if (cover < 0.1)
		fadeInAlpha = cover / 0.1;

	float period = 0.6;
	float amplitude = 0.05;
	float phase = mod(time, 2000) / 2000.0;

	float twoPi = 2.0 * 3.141592654;

	float offset = sin((gl_FragCoord.x / screensize.x) * twoPi * period + phase * twoPi)
			* screensize.y * amplitude;

	float position = mix(screensize.y * (1 + amplitude), -screensize.y * amplitude - 1, cover);

	float v = gl_FragCoord.y - position + offset;

	float s = float(time) / 150000.0;
	float mag = 2.0;

	vec4 texel = texture2D(tex0, gl_TexCoord[0].st);
	vec4 texel1 = texture2D(tex1, mix(gl_TexCoord[0].st, vec2(gl_TexCoord[0].s - 1.0/mag, gl_TexCoord[0].t), s));
	vec4 texel2 = texture2D(tex2, mix(gl_TexCoord[0].st, vec2(gl_TexCoord[0].s + 1.0/mag, gl_TexCoord[0].t), s));

	vec3 color = vec3(1.0, 0.2, 0.1);

	color = mix(color, texel.rgb, alpha);
	color = mix(color, texel1.rgb, texel1.a * alpha);
	color = mix(color, texel2.rgb, texel2.a * alpha);

	if (v > 0.0 && v < 1.0)
	{
		gl_FragData[0] = vec4(color / fadeInAlpha, abs(v) * fadeInAlpha);
	}
	else if (v >= 1.0)
	{
		gl_FragData[0] = vec4(color / fadeInAlpha, 1.0 * fadeInAlpha);
	}
	else
	{
		discard;
	}
}


