varying vec4 ViewPos;
varying vec3 normal;

uniform sampler2D Color0;

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

void main()
{
  vec3 N = normalize( normal );
  
  vec4 color, norm, packpos;
  
  color  = texture2D( Color0, gl_TexCoord[0].st); // color
  norm   = vec4( N.x, N.y, N.z, 1.0 );
  
  // 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[0] = norm * .5 + .5;
  gl_FragData[1] = packpos;
}