varying vec4 ViewPos;
varying vec3 normal;
varying vec3 tangent;
varying vec3 binormal;
varying vec3 taneye;
varying vec3 up;

uniform sampler2D Color0;
uniform sampler2D Distance;

//////////////////////////////////////////////////////////////////////////
// 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(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
const vec4 bitMsk = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);

const float scale = 0.014;
const float bias  = 0.005;

void main()
{
   // parallax bump
  float height = scale * texture2D( Distance, gl_TexCoord[0].st ).g + bias;
  
  vec4 color, packpos;
  vec3 norm;

  // offset texture look up
  vec2 texCoord = gl_TexCoord[0].st + height * taneye.xy;
  // color is offset
  color  = texture2D( Color0, gl_TexCoord[0].st );
  // distance func for alpha test
  color.a = texture2D( Distance, texCoord ).g;// whee hooo
  // scale by distance
  color.a *= ( 1000.0 + ViewPos.z ) / 1000.0;

  norm   = normalize( normal );
  
  // want eye linear depth
  float pos = 1.0 - ViewPos.z / 10000.0;
 
  // pack
  packpos = fract( pos * bitSh );
  packpos -= packpos.xxyz * bitMsk;
  
  gl_FragData[0] = color;
  gl_FragData[1] = vec4( norm * .5 + .5, 1.0 );
  gl_FragData[2] = packpos;
}
