#version 120 // -*- c++ -*-
#extension GL_EXT_gpu_shader4 : require

#if __VERSION__ == 120
#   define          texelFetch texelFetch2D
#endif

uniform sampler2D LinearZ;
uniform float glFar;
uniform float rEdgeScale;
uniform float rEdgeIntensity;

void main(void)
{
  ivec2 SP = ivec2(gl_FragCoord.xy);

  float
    rZ = texelFetch(LinearZ, SP,0).r;

  if (rZ >= glFar)
  {
    gl_FragColor.rgba = vec4(1.0);
  }
  else
  {
    vec3
      cNormal =  vec3(texelFetch( LinearZ, SP + ivec2(1,0),0).r - texelFetch(LinearZ, SP - ivec2(1,0),0).r, 
                      texelFetch( LinearZ, SP + ivec2(0,1),0).r - texelFetch(LinearZ, SP - ivec2(0,1),0).r,
                      rZ * rEdgeScale);

    cNormal = normalize(cNormal);

    float
      rIntensity = max(cNormal.z, 0.0);

    gl_FragColor.rgba = vec4(mix(1.0, rIntensity, rEdgeIntensity));
  }
}                                                  
