#version 120

uniform vec3 lightPos1;
uniform vec3 lightPos2;
uniform vec3 ka;
uniform vec3 kd;
uniform vec3 ks;
uniform float s;
uniform float i1;
uniform float i2;

varying vec3 color; // passed from the vertex shader
varying vec4 p;
varying vec4 n;

void main()
{
  vec4 normal = normalize(n);
  vec3 norm = vec3(normal.x, normal.y, normal.z);
 
  vec4 npos = normalize(p);
  vec3 pos = vec3(npos.x, npos.y, npos.z);

  vec3 light1 = lightPos1 - vec3(p.x, p.y, p.z);
  vec3 lnorm1 = normalize(light1);
  float temp1 = dot(lnorm1, norm);
  vec3 cd1 = kd*max(0, temp1);

  
  vec3 h1 = normalize(lnorm1 - pos);
  vec3 cs1 = ks*pow(max(0, dot(h1, norm)), s);

  vec3 light2 = lightPos2 - vec3(p.x, p.y, p.z);
  vec3 lnorm2 = normalize(light2);
  float temp2 = dot(lnorm2, norm);
  vec3 cd2 = kd*max(0, temp2);

  
  vec3 h2 = normalize(lnorm2 - pos);
  vec3 cs2 = ks*pow(max(0, dot(h2, norm)), s);


  vec4 c = vec4(ka.r + cd1.r + cs1.r, ka.g + cd1.g + cs1.g, ka.b + cd1.b + cs1.b, 1.0);
  vec4 color1 = i1*vec4(ka.r + cd1.r + cs1.r, ka.g + cd1.g + cs1.g, ka.b + cd1.b + cs1.b, 1.0);
  vec4 color2 = i2*vec4(ka.r + cd2.r + cs2.r, ka.g + cd2.g + cs2.g, ka.b + cd2.b + cs2.b, 1.0);
	
  gl_FragColor = color1 + color2;
}
