varying vec4 ViewPos;
varying vec3 normal;
varying vec3 up;

uniform sampler2D Color0;
uniform sampler2D Color1;

//////////////////////////////////////////////////////////////////////////
// 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()
{
  vec4 color, norm, packpos;
  
  // normal
  norm   = vec4( normalize( normal ), 1.0 );
  
  // want eye linear depth
  float pos = 1.0 - ViewPos.z / 10000.0;
 
  // color gets complicated
  vec2 texCoord;
  vec3 e = normalize( ViewPos.xyz );
  // v coord is normal dot up for a lighting like effect
  texCoord.y = max( dot( norm.xyz, up ), 0.0 ) * 0.45 + 0.5;
  // u coord is normal dot eye for cel-shading like effect
  texCoord.x = max( dot( norm.xyz, -e ), 0.0 ) * texCoord.y * 0.9+ 0.1;
  // modulate with texture color
  color  = texture2D( Color1, gl_TexCoord[0].st ) * texture2D( Color0, texCoord );
   
  // pack
  packpos = fract( pos * bitSh );
  packpos -= packpos.xxyz * bitMsk;
  
  //gl_FragData[0] = color;
  gl_FragData[0] = norm * .5 + .5;
  gl_FragData[1] = packpos;
}