//varying vec4 ViewPos;
varying vec3 normal;
varying vec3 tangent;
varying vec3 binormal;
varying vec3 WorldNormal;
varying float pos;

uniform sampler2D Height; // mix
uniform sampler2D Normal; // normal map

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

const vec2 bitSh  = vec2(256.0, 1.0);
const vec2 bitMsk = vec2(0.0, 1.0 / 256.0);

const float scale = 0.02;
const float bias  = -0.02;
void main()
{
  vec3 norm;
  vec3 e = -normalize( ViewPos.xyz );

  // scale blending
  vec3 Nscreen = abs( normalize(WorldNormal).xyz ); 
  Nscreen = ( Nscreen - .2 ) * 7.0;
  Nscreen = max( Nscreen, 0.0 );
  Nscreen = Nscreen / ( Nscreen.x + Nscreen.y + Nscreen.z );

  vec3 tangentspacenormal = texture2D(Normal, gl_TexCoord[0].st) * Nscreen.x +
                            texture2D(Normal, gl_TexCoord[1].st) * NScreen.y +
                            texture2D(Normal, gl_TexCoord[2].st) * NScreen.z;

  vec3 t1 = normalize(tangent);
  vec3 b1 = normalize(binormal);
  vec3 n1 = normalize(normal);
  norm.x = dot(vec3( t1.x, b1.x, n1.x ), tangentspacenormal);
  norm.y = dot(vec3( t1.y, b1.y, n1.y ), tangentspacenormal);
  norm.z = dot(vec3( t1.z, b1.z, n1.z ), tangentspacenormal);

  gl_FragColor = vec4(norm * 0.5 + 0.5, pos);
}
/*
  // normal ///////////////////////////////////////////////////////////////////////////////////////
  vec3 h;
  h.x = 1.0 - texture2D( Height, gl_TexCoord[0].st ).x * Nscreen.x;
  h.y = 1.0 - texture2D( Height, gl_TexCoord[1].st ).x * Nscreen.y;
  h.z = 1.0 - texture2D( Height, gl_TexCoord[2].st ).x * Nscreen.z;
  h *= scale;
  h += bias;
  
  vec2 offsetx = h.x * e.yz;//vec2( 0.0 );//
  vec2 offsety = h.y * e.xz;//vec2( 0.0 );//
  vec2 offsetz = h.z * e.yx;//vec2( 0.0 );//

  vec2 xprojscale = 0.112233 * gl_TexCoord[0].st;
  vec2 yprojscale = 0.112233 * gl_TexCoord[1].st;
  vec2 zprojscale = 0.112233 * gl_TexCoord[2].st;

  vec3 tangentspacenormal = texture2D( Normal, xprojscale        + offsetx ).xyz * Nscreen.x +
                            texture2D( Normal, yprojscale        + offsety ).xyz * Nscreen.y +
                            texture2D( Normal, zprojscale        + offsetz ).xyz * Nscreen.z +
                            texture2D( Normal, gl_TexCoord[0].st + offsetx ).xyz * Nscreen.x +
                            texture2D( Normal, gl_TexCoord[1].st + offsety ).xyz * Nscreen.y +
                            texture2D( Normal, gl_TexCoord[2].st + offsetz ).xyz * Nscreen.z;
  
  // tangentspace scaled... magic number here
  tangentspacenormal = tangentspacenormal * 0.86 - 1.0;
  
  // normalized ver
  vec3 t1 = normalize( tangent );
  vec3 b1 = normalize( binormal );
  vec3 n1 = normalize( normal );
  norm.x = dot( vec3( t1.x, b1.x, n1.x ), tangentspacenormal );
  norm.y = dot( vec3( t1.y, b1.y, n1.y ), tangentspacenormal );
  norm.z = dot( vec3( t1.z, b1.z, n1.z ), tangentspacenormal );
  
    // position //////////////////////////////
  // compute linear eye depth
  float pos = length( ViewPos.xyz );//-ViewPos.z;

  gl_FragColor = vec4( norm * 0.5 + 0.5, pos );
}
*/