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

uniform sampler2D colorMap;

uniform float pointCount;
//uniform vec4 pointColor[];

const vec4 pointColor = vec4(0.6,0.6,0.6,1.0);
//const float constant_att = 0.4;
const float linear_att = 5.0;
//const float quadratic_att = 5.0;

void main (void)
{
   vec3 vVec = 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 diffuse;
   float specular;
   
  float att = 1.0 / (linear_att * d);
  diffuse = max( dot(lVec, n), 0.0 );      
  vDiffuse += pointColor * gl_FrontMaterial.diffuse * diffuse * att;    
  specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );  
  vSpecular += pointColor * gl_FrontMaterial.specular * specular * att;
   
   if(pointCount >= 2.0) {
	   lVec = normalize(lightVec[1]);
	   d = length(lightVec[1]);
	   att = 1.0 / (linear_att * d);
		diffuse = max( dot(lVec, n), 0.0 );      
  		vDiffuse += pointColor * gl_FrontMaterial.diffuse * diffuse * att;    
  		specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );  
  		vSpecular += pointColor * gl_FrontMaterial.specular * specular * att;
   }
   
   if(pointCount >= 3.0) {
	   lVec = normalize(lightVec[2]);
	   d = length(lightVec[2]);
	   att = 1.0 / (linear_att * d);
		diffuse = max( dot(lVec, n), 0.0 );      
  		vDiffuse += pointColor * gl_FrontMaterial.diffuse * diffuse * att;    
  		specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );  
  		vSpecular += pointColor * gl_FrontMaterial.specular * specular * att;
   }
   
   if(pointCount >= 4.0) {
	   lVec = normalize(lightVec[3]);
	   d = length(lightVec[3]);
	   att = 1.0 / (linear_att * d);
		diffuse = max( dot(lVec, n), 0.0 );      
  		vDiffuse += pointColor * gl_FrontMaterial.diffuse * diffuse * att;    
  		specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );  
  		vSpecular += pointColor * gl_FrontMaterial.specular * specular * att;
   }
   
   if(pointCount >= 5.0) {
	   lVec = normalize(lightVec[4]);
	   d = length(lightVec[4]);
	   att = 1.0 / (linear_att * d);
		diffuse = max( dot(lVec, n), 0.0 );
  		vDiffuse += pointColor * gl_FrontMaterial.diffuse * diffuse * att;    
  		specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );  
  		vSpecular += pointColor * gl_FrontMaterial.specular * specular * att;
   }
   
   /*if(pointCount >= 6.0) {
	   lVec = normalize(lightVec[5]);
	   d = length(lightVec[5]);
	   att = 1.0 / (linear_att * d);
		diffuse = max( dot(lVec, n), 0.0 );      
  		vDiffuse += pointColor * gl_FrontMaterial.diffuse * diffuse * att;    
  		specular = pow(clamp(dot(reflect(-lVec, n), vVec), 0.0, 1.0), gl_FrontMaterial.shininess );  
  		vSpecular += pointColor * gl_FrontMaterial.specular * specular * att;
   }*/
   
   gl_FragColor = (vAmbient*base + vDiffuse*base + vSpecular);
}