#version 430 core
/*
* Copyright 2023 Vienna University of Technology.
* Institute of Computer Graphics and Algorithms.
* This file is part of the GCG Lab Framework and must not be redistributed.
*/
layout (location = 0) out vec4 color;
layout (location = 1) out vec4 BrightColor;





uniform float specularAlpha;

uniform bool draw_normals;
uniform bool draw_texcoords;







// in vec2 TexCoords;
// in vec3 WorldPos;
// in vec3 Normal;
in VertexData {
    vec3 position_world;
    vec3 normal_world;
    vec2 uv;
} vert;

// Material parameters
// uniform vec3 albedo;
// uniform float metallic;
// uniform float roughness;
uniform vec3 materialCoefficients; // x = ambient, y = diffuse, z = specular    // x = albedo, y = metallic, z = roughness
const float metallic = materialCoefficients.y;
const float roughness = materialCoefficients.z;

// Texture sampler
//uniform sampler2D albedoMap;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;

uniform bool useNormalMapping;

// Point lights
//uniform vec3 lightPositions[4];
//uniform vec3 lightColors[4];
// Define a maximum number of point lights
#define MAX_POINT_LIGHTS 10
// Uniform structure to hold multiple point lights
uniform struct PointLight {
    vec3 color;
    vec3 position;
    vec3 attenuation;
} pointLights[MAX_POINT_LIGHTS];
uniform int numPointLights; // Number of active point lights

// Directional light
// uniform vec3 dirLightDirection;
// uniform vec3 dirLightColor;
uniform struct DirectionalLight {
    vec3 color;
    vec3 direction;
} dirL;



// Camera
// uniform vec3 camPos;
uniform vec3 camera_world;

const float PI = 3.14159265359;

float DistributionGGX(vec3 N, vec3 H, float roughness) {
    float a = roughness * roughness;
    float a2 = a * a;
    float NdotH = max(dot(N, H), 0.0);
    float NdotH2 = NdotH * NdotH;

    float num = a2;
    float denom = (NdotH2 * (a2 - 1.0) + 1.0);
    denom = PI * denom * denom;

    return num / denom;
}

float GeometrySchlickGGX(float NdotV, float roughness) {
    float r = (roughness + 1.0);
    float k = (r * r) / 8.0;

    float num = NdotV;
    float denom = NdotV * (1.0 - k) + k;

    return num / denom;
}

float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) {
    float NdotV = max(dot(N, V), 0.0);
    float NdotL = max(dot(N, L), 0.0);
    float ggx2 = GeometrySchlickGGX(NdotV, roughness);
    float ggx1 = GeometrySchlickGGX(NdotL, roughness);

    return ggx1 * ggx2;
}

vec3 fresnelSchlick(float cosTheta, vec3 F0) {
    return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}

void main() {
    vec3 albedoColor = texture(diffuseTexture, vert.uv).rgb;

    vec3 N = normalize(vert.normal_world);

    if (useNormalMapping) {
        N = texture(normalTexture, vert.uv).xyz * 2.0f - 1.0f;
        if (vert.normal_world.z < 0.9) {
            N = normalize(vert.normal_world * N);

        }
    }

    vec3 V = normalize(camera_world - vert.position_world);
    vec3 R = reflect(-V, N);

    // Calculate reflectance at normal incidence; if dielectric (like plastic) use F0 of 0.04
    // if it's a metal, use the albedo color as F0 (metallic workflow)
    vec3 F0 = vec3(0.04);
    F0 = mix(F0, albedoColor, metallic);

    // reflectance equation
    vec3 Lo = vec3(0.0);

    // Point lights
    for(int i = 0; i < numPointLights; ++i) {
        vec3 L = normalize(pointLights[i].position - vert.position_world);
        vec3 H = normalize(V + L);
        float distance = length(pointLights[i].position - vert.position_world);
        vec3 attenuation = pointLights[i].attenuation;
        vec3 radiance = pointLights[i].color * attenuation;

        // Cook-Torrance BRDF
        float NDF = DistributionGGX(N, H, roughness);
        float G   = GeometrySmith(N, V, L, roughness);
        vec3 F    = fresnelSchlick(max(dot(H, V), 0.0), F0);

        vec3 numerator    = NDF * G * F;
        float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
        vec3 specular = numerator / max(denominator, 0.001);

        vec3 kS = F;
        vec3 kD = vec3(1.0) - kS;
        kD *= 1.0 - metallic;

        float NdotL = max(dot(N, L), 0.0);
        Lo += (kD * albedoColor / PI + specular) * radiance * NdotL;
    }

    // Directional light
    vec3 L = normalize(-dirL.direction);
    vec3 H = normalize(V + L);
    vec3 radiance = dirL.color;

    float NDF = DistributionGGX(N, H, roughness);
    float G = GeometrySmith(N, V, L, roughness);
    vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);

    vec3 numerator = NDF * G * F;
    float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
    vec3 specular = numerator / max(denominator, 0.001);

    vec3 kS = F;
    vec3 kD = vec3(1.0) - kS;
    kD *= 1.0 - metallic;

    float NdotL = max(dot(N, L), 0.0);
    Lo += (kD * albedoColor / PI + specular) * radiance * NdotL;

    // Since we're not using IBL, ambient lighting can be a simple constant term
    vec3 ambient = vec3(0.03) * albedoColor;
    vec3 tempColor = ambient + Lo;

    // HDR tonemapping
    tempColor = tempColor / (tempColor + vec3(1.0));
    // gamma correct
    tempColor = pow(tempColor, vec3(1.0 / 2.2));

    color = vec4(tempColor, 1.0);

    if (draw_normals) {
        color.rgb = N;
    }
    if (draw_texcoords) {
        color.rgb = vec3(vert.uv, 0);
    }

    // bloom
    float brightness = dot(color.rgb, vec3(0.2126f, 0.7152f, 0.0722f));
    if(brightness > 0.5){
        BrightColor = vec4(color.rgb * brightness * brightness, 1.0f);
    } else{
        BrightColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);
    }
}

/*
void main() {    
    vec3 n = normalize(vert.normal_world);
    if (useNormalMapping) {
        n = texture(normalTexture, vert.uv).xyz * 2.0f - 1.0f;
        if (vert.normal_world.z < 0.9) {
            n = normalize(vert.normal_world * n);

        }
    }
    vec3 v = normalize(vert.position_world - camera_world);
    vec3 R = normalize(clampedReflect(v, n));
    vec3 reflectionColor = getCornellBoxReflectionColor(vert.position_world, R);
    vec3 F0 = vec3(0.1); // <-- some kind of plastic
    vec3 reflectivity = fresnelSchlick(dot(n, -v), F0);

    vec3 texColor = texture(diffuseTexture, vert.uv).rgb;


    color = vec4(texColor * materialCoefficients.x, 1); // ambient
    
    // add directional light contribution
    color.rgb += 0.1 * phong(n, -dirL.direction, -v, dirL.color * texColor, materialCoefficients.y, dirL.color, materialCoefficients.z, specularAlpha, false, vec3(0));
            
    // add point lights contribution
    for (int i = 0; i < numPointLights; i++) {
        color.rgb += 1.0f * phong(n, pointLights[i].position - vert.position_world, -v, pointLights[i].color * texColor, materialCoefficients.y, pointLights[i].color, materialCoefficients.z, specularAlpha, true, pointLights[i].attenuation);
    }

    //color = vec4(mix(color.xyz, reflectionColor, reflectivity), 1.0f);

    if (draw_normals) {
        color.rgb = n;
    }
    if (draw_texcoords) {
        color.rgb = vec3(vert.uv, 0);
    }

    // bloom
    float brightness = dot(color.rgb, vec3(0.2126f, 0.7152f, 0.0722f));
    if(brightness > 0.5){
        BrightColor = vec4(color.rgb * brightness * brightness, 1.0f);
    } else{
        BrightColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);
    }

}
*/