varying vec3 normal;
varying vec3 tangent;
varying vec3 binormal;
varying vec3 up;
varying float pos;

uniform sampler2D Normal; // normal map with bump!


//////////////////////////////////////////////////////////////////////////
// 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);

void main()
{
  vec3 norm;

  // read norm
  vec3 tangentspacenormal = texture2D( Normal, gl_TexCoord[0].st ).xyz;
  // unpack
  tangentspacenormal = tangentspacenormal * 2.0 - 1.0;
  // transform
  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 );
}