#version 450 core
#extension GL_ARB_shading_language_include : require
#include "../../../lib/lights.fs"


layout (location = 0) out vec4 accum;
layout (location = 1) out float reveal;

struct Material {
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
}; 


//inputs
in vec3 FragPos;
in vec3 ViewFragPos;
in vec3 Normal;
in vec2 TexCoord;
in vec4 FragPosLightSpace;


//view position
uniform dvec3 viewPos;

//material
uniform Material material;


uniform mat4 view;


//function prototypes
float linearizeDepth(float d,float zNear,float zFar);
float weightCalcOrigin(float finalAlpha, float zLoc, float linearizedLoc);
float weightCalcFlat(float finalAlpha, float zLoc, float linearizedLoc);
float weightCalcNew(float finalAlpha, float zLoc, float linearizedLoc);


void main(){

    vec3 norm = normalize(Normal);
    vec3 viewDir = normalize(vec3(viewPos) - FragPos);
    
    //grab light intensity
    vec3 lightIntensity = vec3(calcLightIntensityTotal(norm));

    //get color of base texture
    vec4 textureColor = texture(material.diffuse, TexCoord);
    vec3 textureRGB = textureColor.rgb;

    //shadow
    float shadow = ShadowCalculation(FragPosLightSpace, normalize(-directLight.direction), norm);

    //
    //point light calculations
    uint clusterIndex = findCluster(ViewFragPos, zNear, zFar);
    uint pointLightCount = clusters[clusterIndex].count;
    for(int i = 0; i < pointLightCount; i++){
        uint pointLightIndex = clusters[clusterIndex].lightIndices[i];
        PointLight pointLight = pointLight[pointLightIndex];
        lightIntensity = lightIntensity + CalcPointLight(pointLight, norm, FragPos, viewDir);
    }
    //error checking on light clusters
    if(pointLightCount > MAX_LIGHTS_PER_CLUSTER){
        accum = vec4(1.0f,0.0f,0.0f,1);
        reveal = textureColor.a;
        return;
    }

    //calculate final color
    vec3 shadowModifiedColor = textureRGB * lightIntensity * max(shadow, 0.4);
    vec4 finalColor = vec4(shadowModifiedColor,textureColor.a);

    //calculate weight function
    float weight = clamp(pow(min(1.0, finalColor.a * 10.0) + 0.01, 3.0) * 1e8 * 
                        pow(1.0 - gl_FragCoord.z * 0.9, 3.0), 1e-2, 3e3);

    //emit colors
    accum = vec4(finalColor.rgb * finalColor.a, finalColor.a) * weight;
    reveal = finalColor.a;
}

//a weight calculation
float weightCalcOrigin(float finalAlpha, float zLoc, float linearizedLoc){
    float weight = clamp(pow(min(1.0, finalAlpha * 10.0) + 0.01, 3.0) * 1e8 * 
                         pow(1.0 - zLoc * 0.9, 3.0), 1e-2, 3e3);
    return weight;
}

//a weight calculation
float weightCalcFlat(float finalAlpha, float zLoc, float linearizedLoc){
    return 0.1f;
}

//a weight calculation
float weightCalcNew(float finalAlpha, float zLoc, float linearizedLoc){
    float alphaComponent = pow(min(1.0, finalAlpha * 10.0) + 0.01, 3.0);
    float zComponent = pow(1.0 - zLoc * 0.9, 3.0);
    
    float weightRaw = alphaComponent * 1e8 * zComponent;
    float weight = clamp(weightRaw, 1e-2, 3e3);
    return weight;
}

//Linearizes the depth
float linearizeDepth(float d,float zNear,float zFar){
    return zNear * zFar / (zFar + d * (zNear - zFar));
}
