uniform sampler2D Color0;
uniform sampler2D Normal;
uniform sampler2D Position;

const vec3 Up = vec3( 0.0, 0.0, 1.0 );

const float offsetx = 1.0 / 1024.0;
const float offsety = 1.0 / 768.0;

void main()
{
  vec3 N = texture2D( Normal, gl_TexCoord[0].st ).xyz;
  N = N * 2.0 - 1.0;
  
  // get positive ndotl
  float NDotL = dot( N, Up );// * 0.5 + 0.5;//abs( dot(N, L) );//
  //NDotL = max( 0.0, NDotL ) * 0.5 + 0.5;

  //float bend = 0.1;
  //NDotL = ( NDotL + bend ) / ( 1.0 + bend ); 

  // do a sobel filter here
  vec3 c00 = texture2D( Color0, gl_TexCoord[1].st ).rgb;
  vec3 c01 = texture2D( Color0, gl_TexCoord[2].st ).rgb;
  vec3 c02 = texture2D( Color0, gl_TexCoord[3].st ).rgb;
  vec3 c10 = texture2D( Color0, gl_TexCoord[4].st ).rgb;
  vec3 c12 = texture2D( Color0, gl_TexCoord[5].st ).rgb;
  vec3 c20 = texture2D( Color0, gl_TexCoord[6].st ).rgb;
  vec3 c21 = texture2D( Color0, gl_TexCoord[7].st ).rgb;
  vec3 c22 = texture2D( Color0, gl_TexCoord[0].st + vec2( offsetx, offsety ) ).rgb;

  vec3 sx = -1.0 * c00 + -2.0 * c10 + -1.0 * c20 + 1.0 * c02 + 2.0 * c12 + 1.0 * c22;
  vec3 sy = -1.0 * c00 + -2.0 * c01 + -1.0 * c02 + 1.0 * c20 + 2.0 * c21 + 1.0 * c22;

  // convert to grey scale
  vec3 se = abs(sx) + abs(sy);
  float sef = dot( se, vec3( .299, .587, .114 ) ); 
  vec4 Color   = texture2D( Color0, gl_TexCoord[0].st );
  // scale
  sef = 0.5 * sef;
  Color.rgb += vec3( sef, sef, sef );
/* 
*/
  //vec4 Color   = texture2D( Color0, gl_TexCoord[0].st );
  vec4 Light   = vec4( 0.1, 0.2, 0.4, 1.0 ) * NDotL;
  vec4 Ambient = vec4( 0.15, 0.15, 0.15, 1.0 );//vec4( 0.22, 0.22, 0.22, 1.0 );

  //const float scale = 0.005;
  //Color.rgb = texture2D( Color0, gl_TexCoord[0].st + ( Color.xy / Color.z ) * scale ).rgb;// + se;

  gl_FragColor = ( Light + Ambient ) * Color;
}