#version 400

in vec4 fragNormal;
in vec4 fragPos;
in vec4 eyePos;

uniform sampler2D mirrorTex;

out vec4 result;

float SchlickReflectionCoef( float NdotL )
{
	return 0.05 + 0.95 * pow( (1.0f-NdotL), 5.0 );
}

// This is the actual code that is executed.
void main( void )
{
  // We have a texture (for the reflection) computed from the eye's position.
  //    that means our texture coordinate depends on where this fragment falls
  //    in the framebuffer.  Use it's normalized device coordinate (in [-1..1])
  //    directly as a texture coordinate (but first scale and bias to [0..1])
  vec2  texCoord = 0.5 * ( fragPos.xy / fragPos.w ) + 0.5;
  
  // From fragment, compute the surface normal and the direction to the eye
  vec3  norm = normalize( fragNormal.xyz );
  vec3  toEye = normalize( -eyePos.xyz );
  
  // We need N dot E to compute our Fresnel approximation
  float NdotE = dot(norm,toEye);
  
  // On back side of mirror, draw constant grey of 0.05.
  //    On front side, copy from the mirror texture & reflect 80% of the light.
  //    Also add in a slight bluish-tint based on the Fresnel reflection.  Here we
  //    use the Schlick approximation to the Fresnel term.
  result = gl_FrontFacing ? SchlickReflectionCoef(NdotE)*vec4(0,0,1,1)+0.8*texture( mirrorTex, texCoord )
                          : vec4(0.05, 0.05, 0.05, 1.0f );
}