#version 460 core
in vec2 v_texcoord;
in vec3 v_normal;
in vec3 v_position;

layout (binding = 1, std140) uniform Light {
    uniform vec4 light_color;
    uniform vec3 light_pos;
    uniform vec3 camera_pos;
};

uniform sampler2D u_texture;

out vec4 f_color;

vec4 pointLight() {
    vec3 light_vector = light_pos - v_position;
    float dist = length(light_vector);
    float a = 1.0;
	float b = 0.7;
    float intensity = 1 / (a * dist * dist + b * dist + 1.0f);

    // ambient lighting
    float ambient = 0.2;

    // diffuse lighting
    float diffuse_light = 0.7;
    vec3 normal = normalize(v_normal);
    vec3 light_direction = normalize(light_vector);
    float diffuse = diffuse_light * max(dot(normal, light_direction), 0);

    // specular lighting
    float specular_light = 0.7;
    vec3 view_direction = normalize(camera_pos - v_position);
    vec3 reflection_direction = reflect(-light_direction, normal);
    vec3 halfway_vec = normalize(view_direction + light_direction);
    float specular = specular_light * pow(max(dot(normal, halfway_vec), 0), 100);

    return (texture(u_texture, v_texcoord) * (diffuse * intensity + ambient) + specular * intensity) * light_color;
}

void main() {
    f_color = pointLight();
}
