

////BLENDER shader to draw burn/screen etc

#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D al_tex;		///what we're drawing
uniform sampler2D base_tex; 	///what is already drawn

uniform bool al_use_tex;
varying vec4 varying_color;
varying vec2 varying_texcoord;

float screenWidth=640.0;
float screenHeight=360.0;

uniform int blendselect;
//1 = screen
//2 = burn
//3 = multiply


vec4 channelBlend_Screen(vec4 base, vec4 blend){
	return vec4(1.0) - ((vec4(1.0) - blend) * (vec4(1.0) - base));
}

vec4 channelBlend_Burn(vec4 base, vec4 blend){
	return vec4(1.0) - (vec4(1.0) - base) / blend;
}

vec4 channelBlend_Multiply(vec4 base, vec4 blend){
	return base * blend;
}

void main()
{
	if (al_use_tex) {
		//get drawing colour
    	gl_FragColor = varying_color * texture2D(al_tex, varying_texcoord);

		///dont do anything if theres no alpha even..
		//if (gl_FragColor.a <=0) discard;
		
		//figure out actual screen coords because fuck openGL
		vec2 screenCoords = vec2((gl_FragCoord.x)/screenWidth,  (gl_FragCoord.y)/screenHeight);
		    	
    	//blend each r/g/b with whats already there
    	if (blendselect == 1) {
	    	gl_FragColor = channelBlend_Screen(texture2D(base_tex, screenCoords), gl_FragColor);
		} else if (blendselect == 2) {
	    	gl_FragColor = channelBlend_Burn(texture2D(base_tex, screenCoords), gl_FragColor);
		} else if (blendselect == 3) {
	    	gl_FragColor = channelBlend_Multiply(texture2D(base_tex, screenCoords), gl_FragColor);
		}


    	//if (gl_FragCoord.x < 0.5) {
	    //	gl_FragColor = texture2D(base_tex, screenCoords);
	    //	gl_FragColor.g=0;
	    //	gl_FragColor.b=0;
	    //	gl_FragColor.a=1;
	    	//gl_FragColor.r = gl_FragCoord.x;
    	//}
    	
    	
	} else {
    	gl_FragColor = varying_color;
	 }
 
}

