// All content © 2010-2011 DigiPen (USA) Corporation, all rights reserved.

//Fragment shader for edge detection

#version 150

// ---------------------------------------------------------------------------
//uniforms

uniform sampler2D		uNormalDepth; //texture containing scene depth info
uniform vec4			uEdgeColor; 
uniform float			uFarClipPlane;

// ---------------------------------------------------------------------------
// from vertexShader

in vec2					vTexCoord;

in vec2					vBotLeft;
in vec2					vTopLeft;
in vec2					vBotRight;
in vec2					vTopRight;

in vec4					vFarPlane;

// ---------------------------------------------------------------------------
// output

out vec4				out_Color;

void main()
{
	//sample normals and depth in the surrounding pixels
	float botLeft = texture( uNormalDepth, vTexCoord + vBotLeft ).a * uFarClipPlane;
	float topLeft = texture( uNormalDepth, vTexCoord + vTopLeft ).a * uFarClipPlane;
	float botRight = texture( uNormalDepth, vTexCoord + vBotRight ).a * uFarClipPlane;
	float topRight = texture( uNormalDepth, vTexCoord + vTopRight ).a * uFarClipPlane;

	//total change in depth values
	float dDepth = abs( botLeft - topRight ) + abs( topLeft - botRight );

	//filter out small changes
	dDepth = clamp( ( dDepth - 0.8) * 10.0, 0.0, 1.0 );

	float edgeAmt = clamp( (dDepth) * 1.0, 0.0, 1.0);

	if ( edgeAmt < 0.01 )
		discard;

	out_Color = uEdgeColor * edgeAmt;
}




