#version 120

uniform sampler2D mVelocity;
uniform sampler2D mNoise;
uniform sampler2D mTransfer;
uniform vec3 mExtends;

uniform float mAlpha;
uniform int mSteps;
uniform int mStepSize;

void main()
{
	vec2 velcoord = gl_TexCoord[0].xy;
	vec2 velocity = texture2D(mVelocity, velcoord).xy;
	velocity.x *= -1.0f;
	velocity = normalize(velocity);
	velocity /= (mExtends.xy * mStepSize);
	

	vec4 color = texture2D(mNoise, velcoord);
	
	for (int i = 0; i < mSteps; i++)
	{
		velcoord -= velocity;
		velcoord = clamp(velcoord, 0.0, 1.0);
		color += texture2D(mNoise, velcoord);
		velocity = texture2D(mVelocity, velcoord).xy;
		velocity.x *= -1.0f;
		velocity = normalize(velocity);
		velocity /= (mExtends.xy * mStepSize);
		
	}
	
	velcoord = gl_TexCoord[0].xy;
	velocity = texture2D(mVelocity, velcoord).xy;
	velocity.x *= -1.0f;
	velocity = normalize(velocity);
	velocity /= (mExtends.xy * mStepSize);
	

	for (int i = 0; i < mSteps; i++)
	{
		velcoord += velocity;
		velcoord = clamp(velcoord, 0.0, 1.0);
		color += texture2D(mNoise, velcoord);
		velocity = texture2D(mVelocity, velcoord).xy;
		velocity.x *= -1.0f;
		velocity = normalize(velocity);
		velocity /= (mExtends.xy * mStepSize);
		
	}
	
	color /= float(mSteps) * 2.0f;
	vec4 finalcolor = texture2D(mTransfer, vec2(color.x, 0.5));	
	gl_FragColor = finalcolor;
}