
const float twoPi = 6.28318531;

uniform vec4 powerLevel;
uniform float time;

const int nLightCount = 2;

uniform sampler2D diffuseRGBspecA, glowMap;

varying vec3 normal;
varying vec4 pos;
varying vec3 light[nLightCount];
varying float dist[nLightCount];

void applyGlow() {
	vec4 glow = texture2D(glowMap, gl_TexCoord[0].xy);
	float pctPower = powerLevel.x / (powerLevel.y + 0.0001);
	float powerFlux = (sin(time * twoPi) + 0.666) / 2.0;
	gl_FragColor.rgb = max(glow.rgb * min(powerFlux + pctPower, 1.0), gl_FragColor.rgb);
}

void main() {
	vec4 color = gl_FrontMaterial.diffuse, texSamp = texture2D(diffuseRGBspecA, gl_TexCoord[0].xy);
	vec4 matspec = gl_FrontMaterial.specular * texSamp.a;
	float shininess = gl_FrontMaterial.shininess * (0.5 + texSamp.a);

	vec3 n = normalize(normal);
	vec3 v = normalize(-pos.xyz);

	vec4 diffuse = gl_LightModel.ambient * gl_FrontMaterial.ambient;
  
	vec4 specular = vec4(0.0,0.0,0.0,0.0);
	//if(nLightCount > 0)
	{
		const int i = 0;
		float falloff = 1.0 / (1.0 + (gl_LightSource[i].quadraticAttenuation * dist[i] * dist[i]));
		float nl = max(0.0, dot(n, light[i]));
		float intensity = nl * falloff;
		
		diffuse.rgb += gl_LightSource[i].diffuse.rgb * intensity;
		
		vec3 r = normalize(-reflect(normalize(light[i]), n));
		specular.rgb += gl_LightSource[i].specular.rgb * (pow(max(0.0, dot(r, v)), shininess) * falloff);
	}
	//if(nLightCount > 1)
	{
		const int i = 1;
		float falloff = 1.0 / (1.0 + (gl_LightSource[i].quadraticAttenuation * dist[i] * dist[i]));
		float nl = max(0.0, dot(n, light[i]));
		float intensity = nl * falloff;
		
		diffuse.rgb += gl_LightSource[i].diffuse.rgb * intensity;
		
		vec3 r = normalize(-reflect(normalize(light[i]), n));
		specular.rgb += gl_LightSource[i].specular.rgb * (pow(max(0.0, dot(r, v)), shininess) * falloff);
	}

	gl_FragColor = diffuse * color;	
	gl_FragColor *= texSamp;
	gl_FragColor += specular * matspec;

	applyGlow();
}