uniform sampler2D Color0;   // diffuse color
uniform sampler2D Normal;   // eye space normal
uniform sampler2D Position; // eye space position
uniform sampler2D Color1;   // texture map (glass color)
uniform sampler2D Normal1;  // water bump

varying vec3 eyePos;
varying vec4 projPos;
varying vec3 normal;
varying vec3 tangent;
varying vec3 binormal;

//////////////////////////////////////////////////////////////////////////
// method to pack a f32 into an rgba8 from gamedev.net forums
//
// http://www.gamedev.net/community/forums/topic.asp?topic_id=463075
//
//////////////////////////////////////////////////////////////////////////

const vec4 bitSh = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);

const float scale = 0.01;
void main()
{
  // compute projected position
  vec2 texCoord = projPos.xy / projPos.w * .5 + .5;
    
  // normalize eye vector
  vec3 e = normalize( eyePos );
  
  // want eye linear depth
  float pos = 1.0 - eyePos.z / 10000.0;
  float depth = ( dot( bitSh, texture2D( Position, texCoord ) ) );
  depth = 1.0 - abs( depth - pos );
  depth *= 2000.0;
  // clamp  
  depth = clamp( depth, 0.0, 1.0 );

  // unpack normals
  vec3 surfaceNormal = texture2D( Normal1, gl_TexCoord[0].st ).rgb;
  float surfaceBump  = surfaceNormal.z;
  // transform
  surfaceNormal.x = dot( normalize( tangent  ), surfaceNormal );
  surfaceNormal.y = dot( normalize( binormal ), surfaceNormal );
  surfaceNormal.z = dot( normalize( normal   ), surfaceNormal );

  //surfaceNormal = abs(surfaceNormal);

  texCoord += surfaceNormal.xy / surfaceNormal.z * scale; // scaled

  vec3 refractColor = texture2D( Color0, texCoord.st ).xyz;

  vec3 surfaceColor = texture2D( Color1, gl_TexCoord[0].st );
  
  //float blend = dot( surfaceNormal, e );

  gl_FragColor = vec4( texture2D( Normal1, gl_TexCoord[0].st ).rgb, 1.0 );
}
