/*
Original Sobel Filter based on factor progrsmming language code:
Copyright Joe Groff, Slava Pestov

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

uniform float screenWidth;
uniform float screenHeight;
uniform sampler2D depthMap;
uniform sampler2D normalMap;

float threshold(float thr1, float thr2 , float val) {
 if (val < thr1) {return 0.0;}
 if (val > thr2) {return 1.0;}
 return 0.0;
}

float SobelThresh(sampler2D Map){
  float x_stepSize = 1.0/screenWidth;
  float y_stepSize = 1.0/screenHeight;

  vec2 coords = vec2(gl_FragCoord.x/ screenWidth,  gl_FragCoord.y/ screenHeight);

  float depth_samples[8];

  depth_samples[0] = texture2D(Map, coords + vec2(-x_stepSize, -y_stepSize)).x;
  depth_samples[1] = texture2D(Map, coords + vec2( 0,          -y_stepSize)).x;
  depth_samples[2] = texture2D(Map, coords + vec2( x_stepSize, -y_stepSize)).x;
 
  depth_samples[3] = texture2D(Map, coords + vec2(-x_stepSize,  0          )).x;
 
  depth_samples[4] = texture2D(Map, coords + vec2( x_stepSize,  0          )).x;
 
  depth_samples[5] = texture2D(Map, coords + vec2(-x_stepSize,  y_stepSize)).x;
  depth_samples[6] = texture2D(Map, coords + vec2( 0,           y_stepSize)).x;
  depth_samples[7] = texture2D(Map, coords + vec2( x_stepSize,  y_stepSize)).x;
 
  float horizontal = 1.0 * depth_samples[0] + 2.0 * depth_samples[3] + 1.0 * depth_samples[5]
                     - 1.0 * depth_samples[2] - 2.0 * depth_samples[4] - 1.0 * depth_samples[7];
 
  float vertical   = 1.0 * depth_samples[0] + 2.0 * depth_samples[1] + 1.0 * depth_samples[2]
                     - 1.0 * depth_samples[5] - 2.0 * depth_samples[6] - 1.0 * depth_samples[7];

  float sobel_x =  sqrt(horizontal*horizontal + vertical*vertical);

  return threshold(0.95,0.96,sobel_x);

}

float SobelRGBThresh(sampler2D Map){
  float x_stepSize = 1.0/screenWidth;
  float y_stepSize = 1.0/screenHeight;

  vec2 coords = vec2(gl_FragCoord.x/ screenWidth,  gl_FragCoord.y/ screenHeight);

  float depth_samples[8];

  depth_samples[0] = texture2D(Map, coords + vec2(-x_stepSize, -y_stepSize)).x;
  depth_samples[1] = texture2D(Map, coords + vec2( 0,          -y_stepSize)).x;
  depth_samples[2] = texture2D(Map, coords + vec2( x_stepSize, -y_stepSize)).x;
 
  depth_samples[3] = texture2D(Map, coords + vec2(-x_stepSize,  0          )).x;
 
  depth_samples[4] = texture2D(Map, coords + vec2( x_stepSize,  0          )).x;
 
  depth_samples[5] = texture2D(Map, coords + vec2(-x_stepSize,  y_stepSize)).x;
  depth_samples[6] = texture2D(Map, coords + vec2( 0,           y_stepSize)).x;
  depth_samples[7] = texture2D(Map, coords + vec2( x_stepSize,  y_stepSize)).x;
 
  float horizontal = 1.0 * depth_samples[0] + 2.0 * depth_samples[3] + 1.0 * depth_samples[5]
                     - 1.0 * depth_samples[2] - 2.0 * depth_samples[4] - 1.0 * depth_samples[7];
 
  float vertical   = 1.0 * depth_samples[0] + 2.0 * depth_samples[1] + 1.0 * depth_samples[2]
                     - 1.0 * depth_samples[5] - 2.0 * depth_samples[6] - 1.0 * depth_samples[7];

  float sobel_x =  sqrt(horizontal*horizontal + vertical*vertical);

  depth_samples[0] = texture2D(Map, coords + vec2(-x_stepSize, -y_stepSize)).y;
  depth_samples[1] = texture2D(Map, coords + vec2( 0,          -y_stepSize)).y;
  depth_samples[2] = texture2D(Map, coords + vec2( x_stepSize, -y_stepSize)).y;
 
  depth_samples[3] = texture2D(Map, coords + vec2(-x_stepSize,  0          )).y;
 
  depth_samples[4] = texture2D(Map, coords + vec2( x_stepSize,  0          )).y;
 
  depth_samples[5] = texture2D(Map, coords + vec2(-x_stepSize,  y_stepSize)).y;
  depth_samples[6] = texture2D(Map, coords + vec2( 0,           y_stepSize)).y;
  depth_samples[7] = texture2D(Map, coords + vec2( x_stepSize,  y_stepSize)).y;
 
  horizontal = 1.0 * depth_samples[0] + 2.0 * depth_samples[3] + 1.0 * depth_samples[5]
                     - 1.0 * depth_samples[2] - 2.0 * depth_samples[4] - 1.0 * depth_samples[7];
 
  vertical   = 1.0 * depth_samples[0] + 2.0 * depth_samples[1] + 1.0 * depth_samples[2]
                     - 1.0 * depth_samples[5] - 2.0 * depth_samples[6] - 1.0 * depth_samples[7];

  float sobel_y =  sqrt(horizontal*horizontal + vertical*vertical);

  depth_samples[0] = texture2D(Map, coords + vec2(-x_stepSize, -y_stepSize)).z;
  depth_samples[1] = texture2D(Map, coords + vec2( 0,          -y_stepSize)).z;
  depth_samples[2] = texture2D(Map, coords + vec2( x_stepSize, -y_stepSize)).z;
 
  depth_samples[3] = texture2D(Map, coords + vec2(-x_stepSize,  0          )).z;
 
  depth_samples[4] = texture2D(Map, coords + vec2( x_stepSize,  0          )).z;
 
  depth_samples[5] = texture2D(Map, coords + vec2(-x_stepSize,  y_stepSize)).z;
  depth_samples[6] = texture2D(Map, coords + vec2( 0,           y_stepSize)).z;
  depth_samples[7] = texture2D(Map, coords + vec2( x_stepSize,  y_stepSize)).z;
 
  horizontal = 1.0 * depth_samples[0] + 2.0 * depth_samples[3] + 1.0 * depth_samples[5]
                     - 1.0 * depth_samples[2] - 2.0 * depth_samples[4] - 1.0 * depth_samples[7];
 
  vertical   = 1.0 * depth_samples[0] + 2.0 * depth_samples[1] + 1.0 * depth_samples[2]
                     - 1.0 * depth_samples[5] - 2.0 * depth_samples[6] - 1.0 * depth_samples[7];

  float sobel_z =  sqrt(horizontal*horizontal + vertical*vertical);


  float sobel = max(sobel_x,sobel_y);
  sobel = max(sobel,sobel_z);

  return threshold(0.95,0.96,sobel);

}

void main()
{

  vec4 color = vec4(1.0,1.0,1.0,1.0);
  float depthEdge = SobelThresh(depthMap);
  float normalEdge = SobelRGBThresh(normalMap);
  float maxEdge = max(depthEdge,normalEdge);
  color.r = 1.0-maxEdge;
  color.g = 1.0-maxEdge;
  color.b = 1.0-maxEdge;
  gl_FragColor = color;
}
