Added attenuation

This commit is contained in:
Robert 2021-01-29 19:42:36 +01:00
parent f2d1975af3
commit 6981510aaf
11 changed files with 43 additions and 29 deletions

View file

@ -1,7 +1,8 @@
#version 330 core
struct Material
{
vec3 ambient, diffuse, specular;
sampler2D diffuse;
sampler2D specular;
float shininess;
};
@ -10,6 +11,8 @@ struct Light
vec3 position;
vec3 ambient, diffuse, specular;
float ambientStrength;
float constant, linear, quadratic;
};
in vec2 oUV;
@ -18,9 +21,6 @@ in vec3 oFragPos;
out vec4 FragColor;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform vec3 viewPos;
uniform Material material;
@ -29,23 +29,24 @@ uniform Light light;
void main()
{
// Ambient light
vec3 ambient = light.ambient * light.ambientStrength * material.ambient;
vec3 ambient = light.ambient * light.ambientStrength * vec3(texture(material.diffuse, oUV));
vec3 norm = normalize(oNormal);
// Diffuse light
vec3 lightDir = normalize(light.position - oFragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = (diff * material.diffuse) * light.diffuse;
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, oUV));
// Specular light
vec3 viewDir = normalize(viewPos - oFragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = (material.specular * spec) * light.specular;
vec3 specular = light.specular * spec * vec3(texture(material.specular, oUV));
vec4 objColor = mix(texture(texture1, oUV), texture(texture2, oUV), 0.2);
float dist = length(light.position - oFragPos);
float attenuation = 1.0 / (light.constant + light.linear * dist + light.quadratic * (dist * dist));
FragColor = vec4(ambient + diffuse + specular, 1.0) * objColor;
FragColor = vec4((ambient + diffuse + specular) * attenuation, 1.0);
}