uniform sampler2D tex0; //image
uniform sampler2D tex1; //motion buffer

uniform vec2 size;
uniform float zo;
uniform float blur;

void main(void)
{
  float velocityScale = blur;

	vec2 texCoord = gl_FragCoord.xy/size;
	//vec4 color = texture2D(tex0, texCoord);
  
  vec2 velocity = texture2D(tex1, texCoord).rg * 2.0 - 1.0;
  vec2 newV = velocity;
             
  
  vec2 vMax = vec2(0);
  vec2 oMax = vec2(0);
  float radius = 8;
  {
    float lMax = 0.0;  
    vec2 o;
    vec2 v;
    float l;
            
    for (int x = -radius; x < radius; x++)
    {
      for (int y = -radius; y < radius; y++)
      {
        o = vec2(x, y) / size * 8.0;
  	    v = texture2D(tex1, texCoord + o).rg * 2.0 - 1.0;
        l = v.x * v.x + v.y * v.y;
        if (l > lMax)
        {
          lMax = l;
          vMax = v;
          oMax = o;
        }
      }
    }
  }
  
  velocity = vMax;  

  velocity *= velocityScale;
  float speed = length(velocity * size);
  float samples = clamp(speed, 2, 50);
  
  
  float lMax = 0;
  float l;
  vec2 v;
    
  	for (float i = -samples; i <= samples; i++)
  	{
       vec2 sc = texCoord.st + velocity * i / float(samples - 1);

	    v = texture2D(tex1, sc).rg * 2.0 - 1.0;

      l = v.x * v.x + v.y * v.y;
      if (l > lMax)
      {
        newV = v;
        lMax = l;
      }      
  	}
    

  //TODO: this does not work
  newV *= (1.0 - min(1.0, length(oMax) / radius));   
         
  velocity = newV;
  velocity *= velocityScale;
  speed = length(velocity * size);
  samples = clamp(speed, 2, 50);      
  
  
  
  

  /*vec4 color = texture2D(tex0, texCoord);
  float a = color.a;
  for (int i = 1; i < samples; ++i)
  {
     vec2 offset = velocity * (float(i) / float(samples - 1) - 0.5);
     color += texture2D(tex0, texCoord + offset);
  }
  color /= float(samples);
  color.a = a;*/
  
  vec4 color = vec4(0.0);
  
  if (speed < 0)
  {
    color = texture2D(tex0, texCoord); 
  }
  else
  {
    float w = 0.0;
    float a = texture2D(tex0, texCoord).a;
    float sum = 0.0;
  	for (float i = -samples; i <= samples; i++)
  	{
       vec2 sc = texCoord.st + velocity * i / float(samples - 1);
       if (sc.x <= 0 || sc.x >= 1 || sc.y <= 0 || sc.y >= 1)
       {
         continue;
       }
  		w = exp(-pow(i / samples * 1.5, 2.0));
      //w = 1.0;
  		color += texture2D(tex0, sc) * w;	
  		sum += w;
  	}
    
    color /= sum;
  }
  


/*  float w = 0.0;
  float sum = 1.0;
  color = texture2D(tex0, texCoord);
  float a = color.a;
  
  float radius = 10;
      
  for (int x = -radius; x < radius; x++)
  {
    for (int y = -radius; y < radius; y++)
    {
      vec2 op = vec2(x, y);
      vec2 o = op / size;
	    vec2 v = texture2D(tex1, texCoord + o).rg * 2.0 - 1.0;
      v *= velocityScale * size;
      float sp = clamp(length(v), 2, 50);
               
      v = -v;
      
      float l = length(op);

      if (length(o + normalize(v) * l) < 2.6)       
      {
  		  w = exp(-pow(length(o) / radius * 1.5, 2.0));
  		  color += texture2D(tex0, texCoord.st + o) * w;	
  		  sum += w;          
      }
            
    }
  }  
  color /= sum;*/
   

   /*if (zo > 0.5)    
      gl_FragColor = color;//texture2D(tex0, texCoord);
   else
      gl_FragColor = texture2D(tex0, texCoord);*/ 
   
   /*
   if (texCoord.x > 0.5)
	   gl_FragColor = color;
    else
	 gl_FragColor = texture2D(tex0, texCoord + vec2(0.0, 0.0));*/
   
   gl_FragColor = color;

  //gl_FragColor = vec4(0.5 + 0.5 * velocity * 10, 0, 1);
  //gl_FragColor = color;
}