#version 400 core
uniform mat4 mvMatrix;
uniform mat4 projMatrix;
uniform mat3 normalMatrix;
uniform bool isPhong;
uniform vec3 lDirection;
uniform float alpha;

uniform vec3 pointLight[3];

in vec4 vPosition;
in vec3 vNormal;
out vec3 fNormal;
out vec3 fPosition;

vec4 calcDirLight(vec4 eye, vec3 fPos, vec3 fNorm) {
    // Get lighting vectors
    vec3 LightDirection = normalize(lDirection);
        vec3 nfNormal = normalize(fNorm);
    vec3 nviewDirection = normalize(fPos);

    // Compute diffuse component
    float diff = 0.65 * max(0.0, dot(nfNormal, LightDirection));

    // Compute specular component
        //vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection));

        vec3 Rl = reflect(-LightDirection, fNorm);
        float spec = 0.1*pow(max(0.0, dot(Rl, nviewDirection)), 16);

    // Compute ambient component
    vec3 amb = vec3(0.1);

        return vec4(amb + diff + spec, alpha);
}

vec4 calcPointLight(vec4 eye, vec3 fPos, vec3 fNorm, int i) {
    // Get lighting vectors
    vec3 LightDirection = normalize(pointLight[i] - fPos);
    vec3 nfNormal = normalize(fNorm);
    vec3 nviewDirection = normalize(fPos);

    // Attenuation
    float distance    = length(pointLight[i] - nviewDirection);
    float attenuation = 1.0f / (distance * distance);

    // Compute diffuse component
    float diff = attenuation * 0.65 * max(0.0, dot(nfNormal, LightDirection));

    // Compute specular component
    vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection));
    float spec = attenuation * 0.1*pow(max(0.0, dot(Rl, nviewDirection)), 16);

    // Compute ambient component
    vec3 amb = attenuation * vec3(0.1);

    return 0.3 * vec4(amb + diff + spec, alpha);
}

void
main()
{
     vec4 vEyeCoord = mvMatrix * vPosition;
     gl_Position = projMatrix * vEyeCoord;

     fPosition = vEyeCoord.xyz;
     fNormal = normalMatrix*vNormal;
}

