#version 330 core

layout(location=0) out vec4 fragColor;

in vec3 worldNormal;
in vec3 Normal;
in vec2 texCoord;
in vec3 FragPos;
in vec4 FragPosLightSpace;

uniform sampler2D color_texture;
uniform sampler2D shadowMap;

uniform vec3 viewPos;

//material information
uniform vec3 ambientFactor;
uniform vec3 diffuseFactor;
uniform vec3 specularFactor;
uniform float shininess;

//light information
uniform vec3 lightPos;
uniform vec3 ambientColor;
uniform vec3 diffuseColor;
uniform vec3 specularColor;

float CalcShadow(vec4 fragPosLightSpace)
{
	// transform to [-1,1]
	vec3 projCoord = fragPosLightSpace.xyz/fragPosLightSpace.w;
	// transform to [0,1]
	projCoord = projCoord*0.5+0.5;

	//float closestDepth = texture(shadowMap, projCoord.xy).r;
	float currentDepth = projCoord.z;
	
	// calculate bias
	vec3 norm = normalize(Normal);
	vec3 lightDirection = normalize(lightPos-FragPos);
	float bias = max(0.05*(1.0-dot(norm, lightDirection)), 0.005);
	
	float shadow = 0.0;
	vec2 texelSize = 1.0/ textureSize(shadowMap,0);
	for(int x = -1; x<= 1; ++x)
	{
		for(int y = -1; y<=1; ++y)
		{
			float pcf = texture(shadowMap, projCoord.xy + vec2(x,y)*texelSize).r;
			shadow += currentDepth-bias>pcf? 1.0:0.0;
		}
	}
	shadow/=9.0;

	if(projCoord.z>1.0)
		shadow = 0.0;
	return shadow;
}

void main()

{
	vec3 objectColor=  texture(color_texture, texCoord).rgb;
	vec3 norm = normalize(Normal);
	//vec3 lightColor = vec3(1.0f,1.0f,1.0f).rgb;
	
	//ambient 
	vec3 ambient =  ambientFactor* objectColor;

	//diffuse
	vec3 lightDir = normalize(lightPos - FragPos);

	float diff = max(dot(lightDir,norm),0.0);
	vec3 diffuse = diffuseColor*(diff* diffuseFactor);

	//specular
	vec3 viewDir = normalize(viewPos-FragPos);
	vec3 reflectDir = reflect(-lightDir, norm);

	vec3 halfDir= normalize(lightDir+viewDir);
	float spec = pow(max(dot(norm, halfDir),0.0),shininess);
	vec3 specular = specularColor*(spec*specularFactor);

	//shadow calculation
	float shadow =  CalcShadow(FragPosLightSpace);
	vec3 result =(ambient +(1.0-shadow) *(diffuse + specular))*objectColor;

	fragColor = vec4(result, 1.0f);
}