add: added lighting classes and a shader to use multiple lights
This commit is contained in:
parent
ee9e540b5b
commit
2610da8f8c
4 changed files with 359 additions and 64 deletions
153
resources/shaders/light_shader.glsl
Normal file
153
resources/shaders/light_shader.glsl
Normal file
|
@ -0,0 +1,153 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoords;
|
||||
in vec3 FragPos;
|
||||
|
||||
struct DirLight {
|
||||
vec3 direction;
|
||||
vec3 color;
|
||||
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
|
||||
struct PointLight {
|
||||
vec3 position;
|
||||
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 color;
|
||||
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
};
|
||||
|
||||
struct SpotLight {
|
||||
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 DirLight dirLight;
|
||||
#define NR_POINT_LIGHTS 16
|
||||
uniform PointLight pointLights[NR_POINT_LIGHTS];
|
||||
uniform int pointLightCount;
|
||||
uniform SpotLight spotLight;
|
||||
|
||||
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
|
||||
{
|
||||
// ambient
|
||||
float ambientStrength = 0.1;
|
||||
vec3 ambient = ambientStrength * light.color;
|
||||
|
||||
// diffuse
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 lightDir = normalize(-light.direction);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = diff * light.color;
|
||||
|
||||
// specular
|
||||
float specularStrength = 0.5;
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
||||
vec3 specular = specularStrength * spec * light.color;
|
||||
|
||||
return (ambient + diffuse + specular);
|
||||
}
|
||||
|
||||
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
|
||||
{
|
||||
float distance = length(light.position - FragPos);
|
||||
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
||||
|
||||
// ambient// ambient
|
||||
float ambientStrength = 0.1;
|
||||
vec3 ambient = ambientStrength * light.color;
|
||||
|
||||
// diffuse
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 lightDir = normalize(light.position - FragPos);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = diff * light.color;
|
||||
|
||||
// specular
|
||||
float specularStrength = 0.5;
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
||||
vec3 specular = specularStrength * spec * light.color;
|
||||
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation;
|
||||
specular *= attenuation;
|
||||
|
||||
return (ambient + diffuse + specular);
|
||||
}
|
||||
|
||||
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
|
||||
{
|
||||
// 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 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;
|
||||
|
||||
return (ambient + diffuse + specular);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
// properties
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
|
||||
// phase 1: Directional lighting
|
||||
vec3 result = CalcDirLight(dirLight, norm, viewDir);
|
||||
// phase 2: Point lights
|
||||
for(int i = 0; i < pointLightCount; i++)
|
||||
result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
|
||||
// phase 3: Spot light
|
||||
result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
|
||||
|
||||
FragColor = texture(texture_diffuse1, TexCoords) * vec4(result, 1.0);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue