#version 330

#define SSAO_SAMPLE_COUNT 32
const float radius = 0.5, bias = 0;

uniform vec3 viewPos;
uniform mat4 view, projection;
uniform float time; // current time in seconds, for extra noise

uniform sampler2D rand;
uniform vec2      randSize; // in pixels

// SSAO samples (normalized, in tangent space)
layout (std140) uniform uboSamples {
	vec3 samples[SSAO_SAMPLE_COUNT];
};

// G-Buffers
uniform sampler2D gPos;
uniform sampler2D gNorm;
// Fragment information from G-Buffers
vec3 fragPos; // World position
vec3 fragNormal; // World normal
float fragDepthView;


in vec2 fragPosScreen;
layout (location = 0) out float outputColor;


// A random number generator from the web.
// https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
float whatever(vec2 co){
  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}


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

	vec4 fragGPos = texture(gPos, fragPosScreen);
	fragPos = fragGPos.xyz + viewPos;
	fragDepthView = fragGPos.w;
	gl_FragDepth = fragGPos.w * 0.5 + 0.5;
}

vec3 viewTransform(vec3 from) {
	vec4 temp = view * vec4(from, 1);
	return temp.xyz / temp.w;
}


void main() {

	loadGBuffer();

	vec4 randval = texture(rand, gl_FragCoord.xy / randSize);

	//vec3 fragNormalView = viewTransform(fragNormal);
	vec3 fragPosView = viewTransform(fragPos);

	vec3 tangent   = normalize(randval.xyz - fragNormal*dot(randval.xyz, fragNormal));
	vec3 bitangent = cross(fragNormal, tangent);
	mat3 TBN       = mat3(view) * mat3(tangent, bitangent, fragNormal) * radius;

	float occlusion = 0;
	for (int i = 0; i < SSAO_SAMPLE_COUNT; i++) {
		vec4 viewpos = vec4(fragPosView + TBN * samples[i], 1.0f);

		vec4 ndcpos = projection * viewpos;
		ndcpos.xyz /= ndcpos.w;

		float sampleDepth = texture(gPos, ndcpos.xy*0.5 + 0.5).w;

		float rangeCheck = smoothstep(0.0, 1.0, radius / abs(fragDepthView - sampleDepth));
		occlusion       += (sampleDepth >= viewpos.z/viewpos.w + bias ? 1.0 : 0.0) * rangeCheck; 
	}

	outputColor = 1.0 - (occlusion / SSAO_SAMPLE_COUNT);
}
