const int nLightCount = 2;

uniform sampler2D diffuseRGBglowA, normalMap;

varying vec3 normal, binormal, tangent;
varying vec3 npos;
varying vec2 uv;

vec3 light[nLightCount];
float dist[nLightCount];

void main() {
	vec3 color = gl_FrontMaterial.diffuse.rgb;
	vec4 texSamp = texture2D(diffuseRGBglowA, uv.xy);
	vec3 matspec = gl_FrontMaterial.specular.rgb;
	float shininess = gl_FrontMaterial.shininess;

	vec3 normMap = (texture2D(normalMap, uv.xy).xyz * 2.0) - vec3(1.0);
	float gloss = (length(normMap) - 0.5) / 0.5;
	shininess *= (0.5 + gloss);
	matspec *= (0.5 + gloss);
	
	vec3 n = normalize(normal) * normMap.z;
	n += normalize(binormal) * normMap.x;
	n += normalize(tangent) * normMap.y;
	n = normalize(n);
	
	vec3 v = normalize(npos);
	if(nLightCount > 0) {
		const int i = 0;
		light[i] = gl_LightSource[i].position.xyz + npos;
		dist[i] = length(light[i]);
		light[i] = light[i] / dist[i];
	}
	if(nLightCount > 1) {
		const int i = 1;
		light[i] = gl_LightSource[i].position.xyz + npos;
		dist[i] = length(light[i]);
		light[i] = light[i] / dist[i];
	}

	vec3 diffuse = gl_LightModel.ambient.rgb * gl_FrontMaterial.ambient.rgb;

	vec3 specular = vec3(0.0);
	if(nLightCount > 0) {
		const int i = 0;
		float falloff = 1.0 / (1.0 + (gl_LightSource[i].quadraticAttenuation * dist[i] * dist[i]));
		float intensity = max(0.0, dot(n, light[i])) * falloff;
		
		diffuse += gl_LightSource[i].diffuse.rgb * intensity;
		
		vec3 r = -reflect(light[i], n);
		specular += gl_LightSource[i].specular.rgb * (pow(max(0.0, dot(r, v)), shininess) * intensity);
	}
	if(nLightCount > 1) {
		const int i = 1;
		float falloff = 1.0 / (1.0 + (gl_LightSource[i].quadraticAttenuation * dist[i] * dist[i]));
		float intensity = max(0.0, dot(n, light[i])) * falloff;
		
		diffuse += gl_LightSource[i].diffuse.rgb * intensity;
		
		vec3 r = -reflect(light[i], n);
		specular += gl_LightSource[i].specular.rgb * (pow(max(0.0, dot(r, v)), shininess) * intensity);
	}
	gl_FragColor.rgb = ((diffuse + vec3(texSamp.a)) * color * texSamp.rgb) + (specular * matspec);
	gl_FragColor.a = 1.0;
}
