#version 400 core
uniform vec3 lDirection;
uniform bool isSky;
uniform bool isPhong;
uniform sampler2D tex;

in vec3 fNormal;
in vec3 fPosition;
in vec4 ifColor;
in vec2 texCoords;

out vec4 fColor;

void
main()
{
    vec4 texColor = texture(tex, texCoords);
    if(isSky) {
        fColor = texColor;
    } else if(isPhong) {
        // Get lighting vectors
        vec3 LightDirection = normalize(vec3(1,3,1));
        vec3 nfNormal = normalize(fNormal);
        vec3 nviewDirection = normalize(fPosition);

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

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

        // Fragment color
        vec4 color = texColor * diffuse * 0.6 + vec4(1) * specular * 0.6;

        // Compute final color
        fColor = vec4(color.xyz, 1);
        //fColor = vec4(color * (0.1 + diffuse +  specular), 1);
        //fColor = vec4(fNormal, 1);
    } else {
        fColor = texColor * ifColor;
    }
}
