#version 450

layout(location = 0) in vec3 norm;
layout(location = 1) in vec3 color_off;
layout(location = 2) in vec3 color_on;
layout(location = 3) in vec3 pos;

layout(location = 0) out vec4 fin_color;

layout(push_constant, std430) uniform pc {
    layout(offset = 64) vec3 camera_pos;
};

struct Pipeline0PointLight {
    vec3 pos;
    vec3 color;
};

struct Pipeline0Spotlight {
    vec3 pos;
    vec3 dir;
    vec3 color;
    float range;
};

layout(std140, binding = 0) uniform Pipeline0UBO {
    int point_light_count;
    int spotlight_count;
    Pipeline0PointLight point_light_arr[120];
    Pipeline0Spotlight spotlight_arr [20];
};

float get_intensity(float dist){
    return 1 / (dist * dist * 1.8 + dist * 0.7 + 1);
}

void main(){
    vec3 diffuse_illumination = vec3(0);
    vec3 specular_illumination = vec3(0);
    for (int i = 0; i < point_light_count; i++) {
        Pipeline0PointLight lamp = point_light_arr[i];
        vec3 to_light = -pos + lamp.pos;
        float dist = length(to_light);
        vec3 U = to_light / dist;
        diffuse_illumination += get_intensity(dist) * max(0, dot(U, norm)) * lamp.color;
        vec3 A = reflect(-U, norm);
        vec3 B = normalize(-pos+camera_pos);
        // specular_illumination += get_intensity(dist) * pow(max(0, dot(A, B)), 256) * lamp.color;
    }
    for (int i = 0; i < spotlight_count; i++) {
        Pipeline0Spotlight lamp = spotlight_arr[i];
    }
    vec3 color = color_off * diffuse_illumination + (0.05 + 0.45 * length(color_off)) * specular_illumination + color_on;
    fin_color = vec4(color, 1);
}
