//main shader for everything - gladiators, with outlines, and now saturation...

#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D al_tex;
uniform bool al_use_tex;
varying vec4 varying_color;
varying vec2 varying_texcoord;

uniform float depth;

uniform bool outline;
uniform vec4 outlineCol;
uniform float pixelSizeX;
uniform float pixelSizeY;

uniform bool desaturate;
uniform float maxBrightness;

uniform bool fill;
uniform vec4 fillCol;

void main()
{

	


	
	if (al_use_tex) {
    	gl_FragColor = varying_color * texture2D(al_tex, varying_texcoord);
	
		if (gl_FragColor.a > 0.0) {  ///gotta be exactly 0 cause otherwise, vignette suffers - circle is visible
			gl_FragDepth = depth;
			
			if (fill) {
				///just fill with solid colour if this is a nonblank pixel.
				gl_FragColor = fillCol;
			}
			
		} else {

			if (outline){
				///this is a blank pixel, are we doing outlining?
				if ( texture2D(al_tex, varying_texcoord + vec2(-pixelSizeX, 0.0)).a > 0.8
					|| texture2D(al_tex, varying_texcoord + vec2(pixelSizeX, 0.0)).a > 0.8
					|| texture2D(al_tex, varying_texcoord + vec2(0.0, -pixelSizeY)).a > 0.8
					|| texture2D(al_tex, varying_texcoord + vec2(0.0, pixelSizeY)).a > 0.8
				) {
					gl_FragColor = outlineCol;
					gl_FragDepth = depth;
					

					
				} else {
					discard;
				}
			} else {
				discard; 
			}
		}
		
	} else { 
    	gl_FragColor = varying_color;
	}

	if (desaturate) {
		gl_FragColor.rgb = vec3(dot(vec3(0.2126,0.7152,0.0722), gl_FragColor.rgb));
	}

	if (maxBrightness < 1.0) {
		if (gl_FragColor.r > maxBrightness) gl_FragColor.r = maxBrightness;
		if (gl_FragColor.g > maxBrightness) gl_FragColor.g = maxBrightness;
		if (gl_FragColor.b > maxBrightness) gl_FragColor.b = maxBrightness;
	}


}

