LearnOpenGL/shader.fs
Nigel de96aa4136 Added multiple lights to the scene
Both a directional and four points lights are used in
this scene.

Fixed a memory leak caused by the vertex Array buffer of the lights never being deleted
2022-02-16 21:06:40 +01:00

99 lines
2.4 KiB
GLSL

#version 460 core
out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoords;
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
};
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight{
vec3 position;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
#define NR_POINT_LIGHTS 4
uniform PointLight PointLights[NR_POINT_LIGHTS];
uniform DirLight dirlight;
uniform Material material;
uniform vec3 viewPos;
vec3 CalcDirLight(DirLight light , vec3 normal , vec3 viewDir)
{
vec3 lightDir = normalize(-light.direction);
float diff = max(dot(normal,lightDir), 0.0);
vec3 reflectDir = reflect(-lightDir, normal);
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 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos );
float diff = max(dot(normal, lightDir), 0.0);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance *distance));
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));
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
void main()
{
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);
// Fase 1 : Directional lighting
vec3 result = CalcDirLight(dirlight, norm, viewDir);
// Fase 2 : Point lights
for( int i = 0; i < NR_POINT_LIGHTS; i++){
result += CalcPointLight(PointLights[i], norm, FragPos, viewDir);
}
FragColor = vec4(result, 1.0);
}