Updated example

This commit is contained in:
Robert 2021-01-30 00:55:55 +01:00
parent b185429475
commit 4c73eb29cc
2 changed files with 53 additions and 47 deletions

View file

@ -33,34 +33,60 @@ out vec4 FragColor;
uniform vec3 viewPos;
uniform Material material;
uniform Light light;
uniform Light pointLight;
uniform Flashlight fl;
vec3 CalcPointLight(Light light, vec3 normal, vec3 fragPos, vec3 viewDir);
vec3 CalcSpotLight(Flashlight light, vec3 normal, vec3 fragPos, vec3 viewDir);
void main()
{
vec3 lightDir = normalize(fl.position - oFragPos);
float theta = dot(lightDir, normalize(-fl.direction));
float epsilon = fl.angle - fl.outerAngle;
float intensity = clamp((theta - fl.outerAngle) / epsilon, 0.0, 1.0);
vec3 norm = normalize(oNormal);
// Diffuse light
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = fl.diffuse * diff * vec3(texture(material.diffuse, oUV));
// Specular light
vec3 viewDir = normalize(viewPos - oFragPos);
vec3 reflectDir = reflect(-lightDir, norm);
vec3 finalColor = vec3(0.0);
finalColor += CalcPointLight(pointLight, oNormal, oFragPos, viewDir);
finalColor += CalcSpotLight(fl, oNormal, oFragPos, viewDir);
FragColor = vec4(finalColor, 1.0);
}
vec3 CalcPointLight(Light 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);
vec3 specular = fl.specular * spec * vec3(texture(material.specular, oUV));
float dist = length(fl.position - oFragPos);
float attenuation = 1.0 / (fl.constant + fl.linear * dist + fl.quadratic * (dist * dist));
float dist = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * dist + light.quadratic * dist * dist);
// Ambient light
vec3 ambient = light.ambient * light.ambientStrength * vec3(texture(material.diffuse, oUV));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, oUV));
vec3 specular = light.specular * spec * vec3(texture(material.specular, oUV));
FragColor = vec4(ambient + ((diffuse + specular) * attenuation * intensity), 1.0);
return (ambient + (diffuse + specular) * attenuation);
}
vec3 CalcSpotLight(Flashlight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
float theta = dot(lightDir, -normalize(light.direction));
float epsilon = light.angle - light.outerAngle;
float intensity = clamp((theta - light.outerAngle) / epsilon, 0.0, 1.0);
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 dist = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * dist + light.quadratic * dist * dist);
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, oUV));
vec3 specular = light.specular * spec * vec3(texture(material.specular, oUV));
return ((diffuse + specular) * attenuation * intensity);
}