#extension GL_EXT_gpu_shader4 : enable

varying vec4 position;
varying vec4 mainColor;
varying vec4 defaultSpecularColor; //The specular color to use when there is no specular map
varying vec4 defaultGlowColor; //The glow color to use when there is no glow map

varying vec4 specularFactor; //Is multiplied to the final specular component
varying vec4 glowFactor; //Is multipled to the final glow component

uniform sampler2D colorMap;
uniform sampler2D specularMap;
uniform sampler2D glowMap;
uniform sampler2D lightMap;
uniform int textureEnabled;
uniform vec2 lightExpansionFactor;

uniform vec4 ambientLight;

void main() {
	
	bool hasColorMap = (textureEnabled % 2) == 1;
	bool hasGlowMap = textureEnabled == 2 || textureEnabled == 3 || textureEnabled == 6 || textureEnabled == 7;
	bool hasSpecularMap = textureEnabled >= 4;
	
	vec2 screenPos = vec2(position.x, position.y)*0.5+vec2(0.5, 0.5);
	vec2 lightMapPos = vec2(position.x/lightExpansionFactor.x, position.y/lightExpansionFactor.y)*0.5+vec2(0.5, 0.5);
	
	vec4 diffuseColor = mainColor;
	vec4 specularColor = defaultSpecularColor;
	vec4 glowColor = defaultGlowColor;
	vec4 lightColor = texture2D(lightMap, lightMapPos);
	lightColor[3] = 1.0;
	
	if (hasColorMap)
		diffuseColor *= texture2D(colorMap, gl_TexCoord[0].st); //Sample the color texture
	else if (hasGlowMap) {
		//Make the diffuse color transparent if there is no color map but there is a glow map
		diffuseColor = vec4(0.0, 0.0, 0.0, 0.0);
	}
	float diffuseAlpha = diffuseColor[3];

	if (hasSpecularMap)
		specularColor = texture2D(specularMap, gl_TexCoord[0].st); //Sample the specular texture
		
	//Apply specular map
	vec4 specularLightColor = vec4(0, 0, 0, 1); //Calculate the light value the specular color uses
	//Clamp specular light color
	float spec_pow = 0.7;
	specularLightColor[0] = min(pow(lightColor[0], spec_pow) + ambientLight[0], 1.0);
	specularLightColor[1] = min(pow(lightColor[1], spec_pow) + ambientLight[1], 1.0);
	specularLightColor[2] = min(pow(lightColor[2], spec_pow) + ambientLight[2], 1.0);
	
	specularColor *= specularColor[3]; //Multiply by alpha channel
	specularColor *= specularFactor;
	specularColor *= specularLightColor;
	
	//Apply ambient light
	lightColor[0] = min(lightColor[0]+ambientLight[0], 1.0);
	lightColor[1] = min(lightColor[1]+ambientLight[1], 1.0);
	lightColor[2] = min(lightColor[2]+ambientLight[2], 1.0);
	
	//Apply lighting to diffuse color
	diffuseColor *= lightColor;
	
	//Apply specular color
	diffuseColor[0] = max(diffuseColor[0], specularColor[0]);
	diffuseColor[1] = max(diffuseColor[1], specularColor[1]);
	diffuseColor[2] = max(diffuseColor[2], specularColor[2]);
	
	if (hasGlowMap)
		glowColor = texture2D(glowMap, gl_TexCoord[0].st); //Sample the glow texture
		
	//Apply glow factor
	glowColor *= glowFactor;
	
	float glowAlpha = glowColor[3];

	//Apply glow
	//Set the color as a fraction from white to the actual diffuse color based on the glow color
	
	vec4 glowScale = glowColor/diffuseColor;
	glowScale[0] = min(glowScale[0], 1.0);
	glowScale[1] = min(glowScale[1], 1.0);
	glowScale[2] = min(glowScale[2], 1.0);
	
	glowColor = (vec4(1.0, 1.0, 1.0, 1.0)-diffuseColor)*glowColor*glowScale;
	
	if (hasColorMap && hasGlowMap) {
		float totalAlpha = diffuseAlpha+glowAlpha;
		if (totalAlpha > 0.0) {
			float u = glowAlpha/totalAlpha;
			gl_FragColor = diffuseColor + glowColor*(u + glowAlpha*(1.0-u));
			gl_FragColor[3] = max(diffuseAlpha, diffuseAlpha*(1.0-u)+glowAlpha*u);
		}
		else {
			gl_FragColor = glowColor;
			gl_FragColor[3] = 0.0;
		}
	}
	else if (!hasColorMap && !hasGlowMap) {
		gl_FragColor = diffuseColor + glowColor;
		gl_FragColor[3] = max(diffuseAlpha, glowAlpha);
	}
	else if (hasGlowMap && !hasColorMap) {
		gl_FragColor = diffuseColor + glowColor;
		gl_FragColor[3] = glowAlpha;
	}
	else {
		gl_FragColor = diffuseColor + glowColor*glowAlpha;
		gl_FragColor[3] = diffuseAlpha;
	}
	
}