#version 420

uniform sampler2D bg;
uniform sampler2D norm;
uniform sampler2D depth;
uniform mat4 acf_orient;
uniform vec3 sun_dir;
uniform float sun_pitch;

layout(location = 1) in vec2 tex_coord;
layout(location = 0) in vec3 tex_norm;
layout(location = 0) out vec4 color_out;

void main()
{
    vec2 bg_sz = vec2(textureSize2D(bg, 0));
    vec4 bg_pixel = texture(bg, gl_FragCoord.xy / bg_sz);
    float white = (bg_pixel.x + bg_pixel.y) + bg_pixel.z;
    vec2 norm_pixel = texture(norm, tex_coord).xy - vec2(0.5);
    float depth_val = clamp(texture(depth, tex_coord).x, 0.0, 1.5);
    vec3 norm_dir = (acf_orient * vec4(tex_norm, 1.0)).xyz;
    float sun_angle = 1.0 - clamp(dot(norm_dir, sun_dir), 0.0, 1.0);
    float sun_darkening = sin(radians(clamp(sun_angle, 0.0, 90.0)));
    color_out = vec4(1.0, 1.0, 1.0, (((depth_val - (length(norm_pixel) / 2.0)) - (sun_angle / 2.0)) - (sun_darkening / 2.0)) * sqrt(white));
}

