#extension GL_ARB_texture_rectangle : enable

//uniform sampler2DRect floatMap;
uniform sampler2DRect imgMap;
//uniform sampler2D imgMap;

uniform sampler3D wave3D;
uniform float slice_nr;

uniform vec2 imgMapWH;

void main() 
{
  vec4 color;

  float sn = -slice_nr/16.0;

  vec4 at_tc = texture3D(wave3D, vec3(gl_TexCoord[6].ts, 12.0*sn));
  vec2 hdiff_base = (at_tc.gb*2.0 - 1.0)*25.0;

  //  float depth = texture2DRect(imgMap, gl_TexCoord[0].st).z*0.5;

  //  float depth = texture2DRect(imgMap, gl_TexCoord[0].st).z;
  float depth = texture2DRect(imgMap, gl_TexCoord[0].st).w;

  vec2 vdx = vec2(1.0, 0.0);
  vec2 vdy = vec2(0.0, 1.0);

  
  depth += texture2DRect(imgMap, vec2(gl_TexCoord[0].st+vdx.st)).w;
  depth += texture2DRect(imgMap, vec2(gl_TexCoord[0].st-vdx.st)).w;
  depth += texture2DRect(imgMap, vec2(gl_TexCoord[0].st+vdy.st)).w;
  depth += texture2DRect(imgMap, vec2(gl_TexCoord[0].st-vdy.st)).w;

 depth *= 0.2;
  
  /*
  depth = min(depth, texture2DRect(imgMap, vec2(gl_TexCoord[0].st+vdx.st)).w);
  depth = min(depth, texture2DRect(imgMap, vec2(gl_TexCoord[0].st-vdx.st)).w);
  depth = min(depth, texture2DRect(imgMap, vec2(gl_TexCoord[0].st+vdy.st)).w);
  depth = min(depth, texture2DRect(imgMap, vec2(gl_TexCoord[0].st-vdy.st)).w);
  */

 depth = clamp(depth-0.2, 0.0, 1.0);

  //  depth = (depth-0.5)*2.0;

 // limit warp amount
 // depth = min(depth, 0.7);

  /*
  depth -= 0.025;

  depth = clamp(depth, 0.0, 0.9);
  */

  //  color = texture2DRect(imgMap, gl_TexCoord[0].st+hdiff_base);
  //
  color = texture2DRect(imgMap, gl_TexCoord[0].st+depth*hdiff_base);
  //  color = texture2DRect(imgMap, gl_TexCoord[0].st+)*vec4(0.5, 1.0, 0.5, 1.0);

  vec2 dx=vec2(1.325, 0.0);
  vec2 dz=vec2(0.0, 1.325);
  
  color += texture2DRect(imgMap, gl_TexCoord[0].st+depth*dx+depth*hdiff_base);
  color += texture2DRect(imgMap, gl_TexCoord[0].st-depth*dx+depth*hdiff_base);
  color += texture2DRect(imgMap, gl_TexCoord[0].st+depth*dz+depth*hdiff_base);
  color += texture2DRect(imgMap, gl_TexCoord[0].st-depth*dz+depth*hdiff_base);
  
  /*
  color += texture2DRect(imgMap, gl_TexCoord[0].st+depth*dx+depth*1.5*hdiff_base);
  color += texture2DRect(imgMap, gl_TexCoord[0].st-depth*dx+depth*1.5*hdiff_base);
  color += texture2DRect(imgMap, gl_TexCoord[0].st+depth*dz+depth*1.5*hdiff_base);
  color += texture2DRect(imgMap, gl_TexCoord[0].st-depth*dz+depth*1.5*hdiff_base);
  */
  color *= 0.2;

  color += texture2DRect(imgMap, gl_TexCoord[0].st+0.85*(+depth*dx+depth*dz)+depth*hdiff_base);
  color += texture2DRect(imgMap, gl_TexCoord[0].st+0.85*(-depth*dx+depth*dz)+depth*hdiff_base);
  color += texture2DRect(imgMap, gl_TexCoord[0].st+0.85*(+depth*dx-depth*dz)+depth*hdiff_base);
  color += texture2DRect(imgMap, gl_TexCoord[0].st+0.85*(-depth*dx-depth*dz)+depth*hdiff_base);
  color *= 0.2;
  /*
  color = texture2DRect(floatMap, gl_TexCoord[0].st); //.gggg -> just heights


  color.xyz*=0.5;
  color.xyz+=0.5;
  */

   gl_FragColor = color;
   //   gl_FragColor = texture2DRect(imgMap, gl_TexCoord[0].st).aaaa;
   //   gl_FragColor = vec4(depth, depth, depth, depth);
}

