#version 120

uniform mat4 MVL; 
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 lightnorm = vec3(MVL[3].x, MVL[3].y, MVL[3].z);

  vec3 light = lightnorm - vec3(p.x, p.y, p.z);
  vec3 lnorm = normalize(vec3(light.x,light.y,light.z));
  float temp = dot(lnorm, norm);
  vec3 cd = kd*max(0, temp);

  vec3 h = normalize(lnorm - pos);
  vec3 cs = ks*pow(max(0, dot(h, norm)), s);

  vec4 c = vec4(ka.r + cd.r + cs.r, ka.g + cd.g + cs.g, ka.b + cd.b + cs.b, 1.0);
  gl_FragColor = c;
}
