2022-02-11 21:24:15 +00:00
|
|
|
#version 460 core
|
|
|
|
out vec4 FragColor;
|
|
|
|
|
2022-02-13 13:38:39 +00:00
|
|
|
in vec3 Normal;
|
|
|
|
in vec3 FragPos;
|
2022-02-16 18:46:19 +00:00
|
|
|
in vec2 TexCoords;
|
2022-02-13 13:38:39 +00:00
|
|
|
|
2022-02-15 15:15:37 +00:00
|
|
|
struct Material {
|
2022-02-16 18:46:19 +00:00
|
|
|
sampler2D diffuse;
|
|
|
|
sampler2D specular;
|
2022-02-15 15:15:37 +00:00
|
|
|
float shininess;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Light{
|
2022-02-16 19:12:07 +00:00
|
|
|
vec3 position;
|
2022-02-16 18:57:18 +00:00
|
|
|
|
2022-02-16 19:32:55 +00:00
|
|
|
vec3 direction; // neccessary when using directional lights or spotlights.
|
|
|
|
float cutoff;
|
|
|
|
float outerCutOff;
|
2022-02-15 15:15:37 +00:00
|
|
|
|
|
|
|
vec3 ambient;
|
|
|
|
vec3 diffuse;
|
|
|
|
vec3 specular;
|
2022-02-16 19:12:07 +00:00
|
|
|
|
|
|
|
// lets create point lights
|
|
|
|
float constant;
|
|
|
|
float linear;
|
|
|
|
float quadratic;
|
|
|
|
|
|
|
|
|
2022-02-15 15:15:37 +00:00
|
|
|
};
|
|
|
|
|
2022-02-16 19:12:07 +00:00
|
|
|
|
2022-02-15 15:15:37 +00:00
|
|
|
uniform Light light;
|
|
|
|
uniform Material material;
|
|
|
|
|
|
|
|
|
2022-02-13 13:38:39 +00:00
|
|
|
uniform vec3 viewPos;
|
|
|
|
|
2022-02-11 21:24:15 +00:00
|
|
|
|
2022-02-12 20:38:31 +00:00
|
|
|
|
2022-02-11 21:24:15 +00:00
|
|
|
void main()
|
|
|
|
{
|
2022-02-16 19:32:55 +00:00
|
|
|
|
|
|
|
|
2022-02-13 13:38:39 +00:00
|
|
|
// ambient lighting calculation
|
2022-02-16 18:46:19 +00:00
|
|
|
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
|
2022-02-13 13:38:39 +00:00
|
|
|
|
|
|
|
// diffuse lighting calculation
|
|
|
|
vec3 norm = normalize(Normal);
|
2022-02-16 19:12:07 +00:00
|
|
|
vec3 lightDir = normalize(light.position - FragPos);
|
2022-02-13 13:38:39 +00:00
|
|
|
|
|
|
|
float diff = max(dot(norm, lightDir), 0.0);
|
2022-02-16 18:46:19 +00:00
|
|
|
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
|
2022-02-13 13:38:39 +00:00
|
|
|
|
2022-02-15 15:15:37 +00:00
|
|
|
// specular lighting
|
2022-02-13 13:38:39 +00:00
|
|
|
vec3 viewDir = normalize(viewPos - FragPos);
|
|
|
|
vec3 reflectDir = reflect(-lightDir, norm);
|
|
|
|
|
2022-02-15 15:15:37 +00:00
|
|
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
2022-02-16 18:46:19 +00:00
|
|
|
vec3 specular = light.specular * spec * vec3( texture(material.specular, TexCoords));
|
2022-02-13 13:38:39 +00:00
|
|
|
|
2022-02-16 19:32:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
float theta = dot(lightDir, normalize(-light.direction));
|
|
|
|
float epsilon = light.cutoff - light.outerCutOff;
|
|
|
|
float intensity = clamp((theta - light.outerCutOff)/ epsilon, 0.0, 1.0);
|
|
|
|
|
|
|
|
|
|
|
|
diffuse *= intensity;
|
|
|
|
specular *= intensity;
|
|
|
|
|
|
|
|
|
2022-02-16 19:12:07 +00:00
|
|
|
// calculate the attenuation
|
|
|
|
float distance = length(light.position - FragPos);
|
|
|
|
float attenuation = 1.0/ (light.constant + light.linear * distance + light.quadratic * (distance*distance));
|
|
|
|
|
|
|
|
|
2022-02-16 19:32:55 +00:00
|
|
|
|
2022-02-16 19:12:07 +00:00
|
|
|
ambient *= attenuation;
|
|
|
|
diffuse *= attenuation;
|
|
|
|
specular *= attenuation;
|
2022-02-13 13:38:39 +00:00
|
|
|
|
2022-02-16 19:32:55 +00:00
|
|
|
|
|
|
|
|
2022-02-15 15:15:37 +00:00
|
|
|
vec3 result = ambient + diffuse + specular ;
|
2022-02-13 13:38:39 +00:00
|
|
|
|
|
|
|
FragColor = vec4(result, 1.0);
|
2022-02-11 21:24:15 +00:00
|
|
|
}
|