2023-08-30 05:59:37 +03:00
|
|
|
#version 430 core
|
2023-07-25 11:52:12 +03:00
|
|
|
out vec4 FragColor;
|
|
|
|
|
|
|
|
in vec3 Normal;
|
|
|
|
in vec3 FragPos;
|
|
|
|
in vec2 TexCoords;
|
|
|
|
|
|
|
|
struct Material {
|
2023-08-30 05:59:37 +03:00
|
|
|
sampler2D texture_diffuse1;
|
|
|
|
sampler2D texture_specular1;
|
2023-07-25 11:52:12 +03:00
|
|
|
float shininess;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PointLight {
|
|
|
|
vec3 position;
|
|
|
|
|
|
|
|
vec3 ambient;
|
|
|
|
vec3 diffuse;
|
|
|
|
vec3 specular;
|
|
|
|
|
|
|
|
float constant;
|
|
|
|
float linear;
|
|
|
|
float quadratic;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DirectionalLight {
|
|
|
|
vec3 direction;
|
|
|
|
|
|
|
|
vec3 ambient;
|
|
|
|
vec3 diffuse;
|
|
|
|
vec3 specular;
|
|
|
|
};
|
|
|
|
|
2023-08-21 17:43:31 +03:00
|
|
|
struct SpotLight {
|
|
|
|
vec3 position;
|
|
|
|
vec3 direction;
|
|
|
|
float cutOff;
|
|
|
|
float outerCutOff;
|
|
|
|
|
|
|
|
vec3 ambient;
|
|
|
|
vec3 diffuse;
|
|
|
|
vec3 specular;
|
|
|
|
|
|
|
|
float constant;
|
|
|
|
float linear;
|
|
|
|
float quadratic;
|
|
|
|
};
|
|
|
|
|
2023-07-25 11:52:12 +03:00
|
|
|
#define POINT_LIGHTS_COUNT 4
|
|
|
|
|
|
|
|
uniform Material material;
|
|
|
|
uniform vec3 viewPos;
|
|
|
|
|
2023-08-21 17:43:31 +03:00
|
|
|
uniform SpotLight spotLight;
|
2023-07-25 11:52:12 +03:00
|
|
|
uniform DirectionalLight directionalLight;
|
|
|
|
uniform PointLight pointLights[POINT_LIGHTS_COUNT];
|
|
|
|
|
|
|
|
|
|
|
|
vec3 ComputeDirectionalLight(DirectionalLight light, vec3 norm, vec3 viewDir)
|
|
|
|
{
|
|
|
|
vec3 lightDir = normalize(-light.direction);
|
2023-09-02 02:39:46 +03:00
|
|
|
vec3 halfwayDir = normalize(lightDir + viewDir);
|
2023-07-25 11:52:12 +03:00
|
|
|
|
|
|
|
float diff = max(dot(norm, lightDir), 0.0);
|
2023-09-02 02:39:46 +03:00
|
|
|
float spec = pow(max(dot(norm, halfwayDir), 0.0), material.shininess);
|
2023-07-25 11:52:12 +03:00
|
|
|
|
2023-08-30 05:59:37 +03:00
|
|
|
vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, TexCoords));
|
|
|
|
vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, TexCoords));
|
|
|
|
vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, TexCoords));
|
2023-07-25 11:52:12 +03:00
|
|
|
|
|
|
|
return (ambient + diffuse + specular);
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:43:31 +03:00
|
|
|
vec3 ComputePointLight(PointLight light, vec3 norm, vec3 viewDir, vec3 fragPos)
|
2023-07-25 11:52:12 +03:00
|
|
|
{
|
2023-08-21 17:43:31 +03:00
|
|
|
float distance = length(light.position - fragPos);
|
2023-07-25 11:52:12 +03:00
|
|
|
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
|
|
|
|
2023-08-21 17:43:31 +03:00
|
|
|
vec3 lightDir = normalize(light.position - fragPos);
|
2023-09-02 02:39:46 +03:00
|
|
|
vec3 halfwayDir = normalize(lightDir + viewDir);
|
2023-07-25 11:52:12 +03:00
|
|
|
|
|
|
|
float diff = max(dot(norm, lightDir), 0.0);
|
2023-09-02 02:39:46 +03:00
|
|
|
float spec = pow(max(dot(norm, halfwayDir), 0.0), material.shininess);
|
2023-07-25 11:52:12 +03:00
|
|
|
|
2023-08-30 05:59:37 +03:00
|
|
|
vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, TexCoords));
|
|
|
|
vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, TexCoords));
|
|
|
|
vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, TexCoords));
|
2023-07-25 11:52:12 +03:00
|
|
|
|
|
|
|
return (ambient * attenuation + diffuse * attenuation + specular * attenuation);
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:43:31 +03:00
|
|
|
vec3 ComputeSpotLight(SpotLight light, vec3 norm, vec3 viewDir, vec3 fragPos) {
|
|
|
|
vec3 lightDir = normalize(light.position - fragPos);
|
2023-09-02 02:39:46 +03:00
|
|
|
vec3 halfwayDir = normalize(lightDir + viewDir);
|
2023-08-21 17:43:31 +03: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);
|
|
|
|
|
|
|
|
float distance = length(light.position - fragPos);
|
|
|
|
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
|
|
|
|
|
|
|
float diff = max(dot(norm, lightDir), 0.0);
|
2023-09-02 02:39:46 +03:00
|
|
|
float spec = pow(max(dot(norm, halfwayDir), 0.0), material.shininess);
|
2023-08-21 17:43:31 +03:00
|
|
|
|
2023-08-30 05:59:37 +03:00
|
|
|
vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, TexCoords));
|
|
|
|
vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, TexCoords));
|
|
|
|
vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, TexCoords));
|
2023-08-21 17:43:31 +03:00
|
|
|
|
|
|
|
// we leave ambient alone so there's always a little light
|
|
|
|
return ((ambient * attenuation) + (diffuse * intensity * attenuation) + (specular * intensity * attenuation));
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:52:12 +03:00
|
|
|
void main()
|
|
|
|
{
|
|
|
|
vec3 norm = normalize(Normal);
|
|
|
|
vec3 viewDir = normalize(viewPos - FragPos);
|
|
|
|
|
|
|
|
vec3 result = vec3(0.0);
|
2023-08-21 17:43:31 +03:00
|
|
|
|
2023-07-25 11:52:12 +03:00
|
|
|
result += ComputeDirectionalLight(directionalLight, norm, viewDir);
|
|
|
|
for (int i = 0; i < POINT_LIGHTS_COUNT; i++) {
|
2023-08-21 17:43:31 +03:00
|
|
|
result += ComputePointLight(pointLights[i], norm, viewDir, FragPos);
|
2023-07-25 11:52:12 +03:00
|
|
|
}
|
2023-08-21 17:43:31 +03:00
|
|
|
result += ComputeSpotLight(spotLight, norm, viewDir, FragPos);
|
2023-07-25 11:52:12 +03:00
|
|
|
|
|
|
|
FragColor = vec4(result, 1.0);
|
|
|
|
}
|