88 lines
2.4 KiB
GLSL
88 lines
2.4 KiB
GLSL
#version 330 core
|
|
out vec4 FragColor;
|
|
|
|
in vec3 Normal;
|
|
in vec3 FragPos;
|
|
in vec2 TexCoords;
|
|
|
|
struct Material {
|
|
sampler2D diffuse;
|
|
sampler2D specular;
|
|
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;
|
|
};
|
|
|
|
#define POINT_LIGHTS_COUNT 4
|
|
|
|
uniform Material material;
|
|
uniform vec3 viewPos;
|
|
|
|
uniform DirectionalLight directionalLight;
|
|
uniform PointLight pointLights[POINT_LIGHTS_COUNT];
|
|
|
|
|
|
vec3 ComputeDirectionalLight(DirectionalLight light, vec3 norm, vec3 viewDir)
|
|
{
|
|
vec3 lightDir = normalize(-light.direction);
|
|
vec3 reflectDir = reflect(-lightDir, norm);
|
|
|
|
float diff = max(dot(norm, lightDir), 0.0);
|
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
|
|
|
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
|
|
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
|
|
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
|
|
|
|
return (ambient + diffuse + specular);
|
|
}
|
|
|
|
vec3 ComputePointLight(PointLight light, vec3 norm, vec3 viewDir)
|
|
{
|
|
float distance = length(light.position - FragPos);
|
|
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
|
|
|
vec3 lightDir = normalize(light.position - FragPos);
|
|
vec3 reflectDir = reflect(-lightDir, norm);
|
|
|
|
float diff = max(dot(norm, lightDir), 0.0);
|
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
|
|
|
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
|
|
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
|
|
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
|
|
|
|
return (ambient * attenuation + diffuse * attenuation + specular * attenuation);
|
|
}
|
|
|
|
void main()
|
|
{
|
|
vec3 norm = normalize(Normal);
|
|
vec3 viewDir = normalize(viewPos - FragPos);
|
|
|
|
vec3 result = vec3(0.0);
|
|
result += ComputeDirectionalLight(directionalLight, norm, viewDir);
|
|
for (int i = 0; i < POINT_LIGHTS_COUNT; i++) {
|
|
result += ComputePointLight(pointLights[i], norm, viewDir);
|
|
}
|
|
|
|
FragColor = vec4(result, 1.0);
|
|
}
|