//varying vec3 eyeVec;
varying vec2 texCoord;
varying vec3 lightVec[]; 
varying vec3 spotDirVec[];
varying vec3 tNormal;

uniform sampler2D colorMap;

uniform float spotCount;
//uniform float spotExponent[];
//uniform float spotAngle[];
//uniform vec4 spotColor[];

//const float spotExponent = 10.0;
const float spotExponent = 0.2;
const float spotAngle = 0.7053981625;
const vec4 spotColor = vec4(1.0,1.0,1.0,1.0);
//const float linear_att = 0.2;
//const float point_att = 0.4;

void main (void)
{
   vec3 vVec = vec3(0.0, 0.0, 1.0);//normalize(eyeVec);
   vec4 base = texture2D(colorMap, texCoord);
   vec3 n = normalize(tNormal);
   vec4 vDiffuse = vec4(0.0);
   vec4 vSpecular = vec4(0.0);
   float d = length(lightVec[0]);
   vec3 lVec = normalize(lightVec[0]);
   vec4 vAmbient = gl_FrontMaterial.ambient;
   
   float spotDot = dot(-lVec, spotDirVec[0]);
   
   float att;
   float diffuse;
   float specular;
   if(spotDot > cos(spotAngle)) {
      att = pow(spotDot, spotExponent);
      diffuse = max( dot(lVec, n), 0.0 );      
      vDiffuse += spotColor * gl_FrontMaterial.diffuse * diffuse * att;// + vec4(1.0, 0.0, 0.0, 0.0);    
      specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );  
      vSpecular += spotColor * gl_FrontMaterial.specular * specular * att;
   }
   /*else {
   		att = 1.0 / (linear_att * d);
   		att *= point_att;
		diffuse = max( dot(lVec, n), 0.0 );      
  		vDiffuse += spotColor * gl_FrontMaterial.diffuse * diffuse * att;    
  		specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );  
  		vSpecular += spotColor * gl_FrontMaterial.specular * specular * att;
   }*/
   
   if(spotCount >= 2.0) {
	   lVec = normalize(lightVec[1]);
	   spotDot = dot(-lVec, spotDirVec[1]);
	   if(spotDot > cos(spotAngle)) {
	      att = pow(spotDot, spotExponent);
	      diffuse = max( dot(lVec, n), 0.0 );
	      vDiffuse += spotColor * gl_FrontMaterial.diffuse * diffuse * att;// + vec4(0.0, 1.0, 0.0, 0.0);
	      specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );
	      vSpecular += spotColor * gl_FrontMaterial.specular * specular * att;
	   }
	  /*else {
	   		d = length(lightVec[1]);
	   	 	att = 1.0 / (linear_att * d);
	   		att *= point_att;
			diffuse = max( dot(lVec, n), 0.0 );      
	  		vDiffuse += spotColor * gl_FrontMaterial.diffuse * diffuse * att;    
	  		specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );  
	  		vSpecular += spotColor * gl_FrontMaterial.specular * specular * att;
	   }*/
   }
   
   if(spotCount >= 3.0) {
	   lVec = normalize(lightVec[2]);
	   spotDot = dot(-lVec, spotDirVec[2]);
	   if(spotDot > cos(spotAngle)) {
	      att = pow(spotDot, spotExponent);
	      diffuse = max( dot(lVec, n), 0.0 );
	      vDiffuse += spotColor * gl_FrontMaterial.diffuse * diffuse * att;// + vec4(0.0, 0.0, 1.0, 0.0);
	      specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );
	      vSpecular += spotColor * gl_FrontMaterial.specular * specular * att;
	   }
	   /*else {
	   		d = length(lightVec[2]);
	   		att = 1.0 / (linear_att * d);
	   		att *= point_att;
			diffuse = max( dot(lVec, n), 0.0 );      
	  		vDiffuse += spotColor * gl_FrontMaterial.diffuse * diffuse * att;    
	  		specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );  
	  		vSpecular += spotColor * gl_FrontMaterial.specular * specular * att;
	   }*/
   }
   
   gl_FragColor = (vAmbient*base + vDiffuse*base + vSpecular);
}