#version 330

uniform sampler2D shadowmap;
uniform mat4 lightspace;
uniform vec3 viewPos;
uniform vec3 sun;
uniform vec4 fogColor;

uniform sampler2D ssaoAmbient; // Ambient strength from SSAO pass

// G-Buffers
uniform sampler2D gPos;
uniform sampler2D gNorm;
uniform sampler2D gColor;
// Fragment information from G-Buffers
vec4 fragPos;
vec4 fragPosLightspace;
float fragPosLightspaceZ;
vec3 fragNormal;
vec4 fragColor;

in vec2 fragPosScreen;

out vec4 outputColor;


const float ambient = 0.3, specularStrength = 0.08, specularShininess = 8;
const float fogDensity = .00003;


float light;
vec3 color = vec3(0);
void lightSun();
float lightSunShadow();
void lightPoint(int i);


void loadGBuffer() {
	vec4 fragGNormal = texture(gNorm, fragPosScreen);
	fragNormal = fragGNormal.xyz;
	if (fragNormal == vec3(0.0f, 0.0f, 0.0f))
		discard;
	fragPosLightspaceZ = fragGNormal.w;


	vec4 fragGPos = texture(gPos, fragPosScreen);
	fragPos = vec4(fragGPos.xyz + viewPos, 1.0f);

	fragColor = texture(gColor, fragPosScreen);

	fragPosLightspace = lightspace * fragPos;
}


void main() {

	loadGBuffer();

	light = ambient * texture(ssaoAmbient, fragPosScreen).r;

	lightSun();

	color += fragColor.rgb * light;

	//float z = gl_FragCoord.z / gl_FragCoord.w;
	//float fog = clamp(exp(-fogDensity * z * z), 0.2, 1);

	//outputColor = mix(fogColor, color, fog);
	outputColor = vec4(color, 1);
}

void lightSun() {
	/* Diffuse */
	vec3 lightDir = sun;
	float diffuse = max(dot(fragNormal, lightDir), 0.0f);

	/* Specular */
	vec3 viewDir = normalize(viewPos - fragPos.xyz);
	vec3 reflectDir = reflect(-lightDir, fragNormal);
	float specular = specularStrength * pow(max(dot(viewDir, reflectDir), 0.0), specularShininess);

	float shadow = lightSunShadow();
	light += diffuse * shadow;
	color += vec3(specular) * shadow;
}

float lightSunShadow() {
	/* Shadow */
	float bias = max(0.0013 * (1.0 - dot(fragNormal, sun)), 0.0001);
	vec3 projCoords = fragPosLightspace.xyz / fragPosLightspace.w;
	projCoords = projCoords*0.5 + 0.5;
	float closestDepth = texture(shadowmap, projCoords.xy).r;
	//float currentDepth = projCoords.z;
	float currentDepth = fragPosLightspaceZ;
	float shadow = 0;

	if (currentDepth > 1.0f || currentDepth < 0.0f)
		return 1.0f;

	//vec2 texelSize = clamp((currentDepth+bias-closestDepth)*100.0f, 0.05f, 1.5f) / textureSize(shadowmap, 0);
	vec2 texelSize = 0.4 / textureSize(shadowmap, 0);
	for (int x=-4; x<=4; ++x)
		for (int y=-4; y<=4; ++y) {
			float pcfDepth = texture(shadowmap, projCoords.xy + vec2(x,y)*texelSize).r;
			shadow += currentDepth-bias < pcfDepth ? 1.0f : 0.0f;
		}
	return min(shadow/81.0f, 1.0f);
}
