#version 330 core
struct Material {
	sampler2D diffuseMap;
	sampler2D specularMap;
	float shininess;
};
struct Light {
	vec3 position;
  
	vec3 ambientIntensity;
	vec3 diffuseIntensity;
	vec3 specularIntensity;

	float constant;
	float linear;
	float quadratic;
};

out vec4 FragColor;

uniform vec3 viewPosition;
uniform Material material;
uniform Light light;

in vec2 TexCoords;
in vec3 FragmentPos;
in vec3 Normal;

void main()
{
	vec3 normal = normalize(Normal);

	vec3 ambientLight = light.ambientIntensity * texture(material.diffuseMap, TexCoords).rgb;

	vec3 lightDir = normalize(light.position - FragmentPos);
	float diff = max(dot(normal, lightDir), 0.0);
	vec3 diffuseLight = light.diffuseIntensity * diff * texture(material.diffuseMap, TexCoords).rgb;

	vec3 viewDir = normalize(viewPosition - FragmentPos);
	vec3 reflectDir = reflect(-lightDir, normal);
	float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
	vec3 specularLight = light.specularIntensity * spec * texture(material.specularMap, TexCoords).rgb;

	float distance = length(light.position - FragmentPos);
	float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));

	ambientLight *= attenuation;
	diffuseLight *= attenuation;
	specularLight *= attenuation;

	vec3 result = ambientLight + diffuseLight + specularLight;
	FragColor = vec4(result, 1.0);
} 