#version 450


layout(location = 3) uniform vec4 mat_ambi;
layout(location = 4) uniform vec4 mat_diff;
layout(location = 5) uniform vec4 mat_spec;

layout(location = 10) uniform vec3 eye_pos;

layout(location = 0) in vec3 fs_position;
layout(location = 1) in vec3 fs_normal;
layout(location = 2) in vec3 fs_tex_coord;

layout(location = 0) out vec4 color;


void main()
{
    vec3 sun_loc = vec3(0, 7, 0);

    vec3 ambi = mat_ambi.rgb;
    vec3 diff = mat_diff.rgb;
    vec3 spec = mat_spec.rgb;
    float shininess = mat_spec.w;

    // vectors
    vec3 n = normalize(fs_normal);
    vec3 l = normalize(sun_loc);
    vec3 v = normalize(eye_pos - fs_position);
    vec3 r = normalize(2 * dot(n, l) * n - l);

    // blinn
    // vec3 halfv = normalize(l + normalize(eye_pos));
    // float reflection = max(0.0001, dot(n, halfv)) * reflection_allow;

    // whether or not reflection should be visible: 1 or 0
    float reflection_allow = (sign(dot(n, l)) + 1.0) * 0.5;

    float reflection = max(0.0001, dot(v, r)) * reflection_allow;

    float diff_res = max(0, dot(n, l));
    float spec_res = max(0, pow(reflection, shininess));

    vec3 result = ambi + diff_res * diff + spec_res * spec;


    color = vec4(result, 1.0);
}
