
// Uniform variables for texturing
uniform sampler2D 	aaa_tex2d[4];

// Dimension of textures : 0, 1, 2, 3 dimension, -1 if unused per Tex Unit
uniform	int			aaa_tex_dim[4];

// Size of Texture for each texture unit
uniform vec2		aaa_tex_0_size;

// 0 = time
// 1 = mouse X
// 2 = mouse Y
// 3 = steps -> from 0.1 to 10.0
uniform float		aaa_pu_float[6];

float intensity(in vec4 color){
	return sqrt((color.x*color.x)+(color.y*color.y)+(color.z*color.z));
}

vec3 sobel(float stepx, float stepy, vec2 center)
{
	// get samples around pixel
    float tleft = intensity(texture2D(aaa_tex2d[0],center + vec2(-stepx,stepy)));
    float left = intensity(texture2D(aaa_tex2d[0],center + vec2(-stepx,0)));
    float bleft = intensity(texture2D(aaa_tex2d[0],center + vec2(-stepx,-stepy)));
    float top = intensity(texture2D(aaa_tex2d[0],center + vec2(0,stepy)));
    float bottom = intensity(texture2D(aaa_tex2d[0],center + vec2(0,-stepy)));
    float tright = intensity(texture2D(aaa_tex2d[0],center + vec2(stepx,stepy)));
    float right = intensity(texture2D(aaa_tex2d[0],center + vec2(stepx,0)));
    float bright = intensity(texture2D(aaa_tex2d[0],center + vec2(stepx,-stepy)));
 
	// Sobel masks (see http://en.wikipedia.org/wiki/Sobel_operator)
	//        1 0 -1     -1 -2 -1
	//    X = 2 0 -2  Y = 0  0  0
	//        1 0 -1      1  2  1
	
	// You could also use Scharr operator:
	//        3 0 -3        3 10   3
	//    X = 10 0 -10  Y = 0  0   0
	//        3 0 -3        -3 -10 -3
 
    float x = tleft + 2.0*left + bleft - tright - 2.0*right - bright;
    float y = -tleft - 2.0*top - tright + bleft + 2.0 * bottom + bright;
    float color = sqrt((x*x) + (y*y));
    return vec3(color,color,color);
 }

void main(void)
{
	vec2 uv = gl_FragCoord.xy / aaa_tex_0_size.xy;
	vec4 color = texture2D(aaa_tex2d[0], uv.xy);
	
	gl_FragColor.xyz = sobel(aaa_pu_float[3]/aaa_tex_0_size.x, aaa_pu_float[3]/aaa_tex_0_size.y, uv);
}

