add: added spotlight light shader
This commit is contained in:
parent
8f1bf32a1e
commit
ee9e540b5b
2 changed files with 99 additions and 6 deletions
64
resources/shaders/spotlight_light.glsl
Normal file
64
resources/shaders/spotlight_light.glsl
Normal file
|
@ -0,0 +1,64 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoords;
|
||||
in vec3 FragPos;
|
||||
|
||||
struct Light {
|
||||
vec3 position;
|
||||
vec3 direction;
|
||||
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 color;
|
||||
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
float cutOff;
|
||||
float outerCutOff;
|
||||
};
|
||||
|
||||
uniform sampler2D texture_diffuse1;
|
||||
uniform vec3 viewPos;
|
||||
uniform Light light;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Calculate the direction of the light
|
||||
vec3 lightDir = normalize(light.position - FragPos);
|
||||
|
||||
// Calculate the angle between the light direction and the spotlight direction
|
||||
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));
|
||||
|
||||
// ambient
|
||||
float ambientStrength = 0.1;
|
||||
vec3 ambient = ambientStrength * light.color;
|
||||
|
||||
// diffuse
|
||||
vec3 norm = normalize(Normal);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = diff * light.color;
|
||||
|
||||
// specular
|
||||
float specularStrength = 0.5;
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
||||
vec3 specular = specularStrength * spec * light.color;
|
||||
|
||||
// Apply attenuation and intensity
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation * intensity;
|
||||
specular *= attenuation * intensity;
|
||||
|
||||
vec3 result = ambient + diffuse + specular;
|
||||
vec4 light = vec4(result, 1.0);
|
||||
FragColor = texture(texture_diffuse1, TexCoords) * light;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue