Fully implemented lighting
Two types of light sources are currently supported: directional lights and point lights. A scene can have only one directional light and up to 1024 point lights.
This commit is contained in:
parent
214317e0bf
commit
503ed33aec
7 changed files with 181 additions and 48 deletions
|
|
@ -1,39 +1,80 @@
|
|||
#version 450
|
||||
|
||||
#define MAX_POINT_LIGHTS 1024
|
||||
|
||||
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
layout(location = 2) in vec3 Normal;
|
||||
layout(location = 3) in vec3 FragPos;
|
||||
layout(location = 4) in vec2 TexCoords;
|
||||
|
||||
layout (binding = 2) uniform LightUniform {
|
||||
vec3 pos;
|
||||
} lightPos;
|
||||
layout (binding = 2) uniform DirectionalLight {
|
||||
vec3 direction;
|
||||
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
} directional_light;
|
||||
layout (binding = 5) uniform PointLight {
|
||||
vec3 position;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 data;
|
||||
|
||||
} point_lights[MAX_POINT_LIGHTS];
|
||||
|
||||
layout (binding = 3) uniform ViewUniform {
|
||||
vec3 pos;
|
||||
} viewPos;
|
||||
|
||||
layout(push_constant) uniform pc {
|
||||
int light_count;
|
||||
} pushConstants;
|
||||
|
||||
layout (set = 1, binding = 0) uniform sampler2D textureSampler;
|
||||
layout (set = 1, binding = 1) uniform sampler2D diffuseSampler;
|
||||
|
||||
void main() {
|
||||
vec3 lightDiffuse = vec3(0.5, 0.5, 0.5);
|
||||
vec3 lightAmbient = vec3(0.2, 0.2, 0.2);
|
||||
vec3 lightSpecular = vec3(1.0, 1.0, 1.0);
|
||||
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 lightDir = normalize(lightPos.pos - FragPos);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = lightDiffuse * diff * vec3(texture(textureSampler, TexCoords));
|
||||
vec3 ambient = lightAmbient * vec3(texture(textureSampler, TexCoords));
|
||||
|
||||
vec3 viewDir = normalize(viewPos.pos - FragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
vec3 calc_directional_light(vec3 normal, vec3 viewDir) {
|
||||
vec3 lightDir = normalize(-directional_light.direction);
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 2);
|
||||
vec3 specular = lightSpecular * spec * vec3(texture(diffuseSampler, TexCoords));
|
||||
//vec3 specular = lightSpecular * spec * vec3(1.0, 1.0, 1.0);
|
||||
|
||||
vec3 result = (ambient + diffuse + specular);
|
||||
outColor = vec4(result, 1.0);
|
||||
vec3 ambient = directional_light.ambient * vec3(texture(textureSampler, TexCoords));
|
||||
vec3 diffuse = directional_light.diffuse * diff * vec3(texture(textureSampler , TexCoords));
|
||||
vec3 specular = directional_light.specular * spec * vec3(texture(diffuseSampler, TexCoords));
|
||||
return (ambient + diffuse + specular);
|
||||
}
|
||||
|
||||
vec3 calc_point_light(int index, vec3 normal, vec3 fragPos, vec3 viewDir) {
|
||||
float constant = point_lights[index].data[0];
|
||||
float linear = point_lights[index].data[1];
|
||||
float quadratic = point_lights[index].data[2];
|
||||
|
||||
vec3 lightDir = normalize(point_lights[index].position - fragPos);
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 2);
|
||||
float distance = length(point_lights[index].position - fragPos);
|
||||
float attenuation = 1.0 / (constant + linear * distance + quadratic * (distance * distance));
|
||||
vec3 ambient = point_lights[index].ambient * vec3(texture(textureSampler, TexCoords));
|
||||
vec3 diffuse = point_lights[index].diffuse * diff * vec3(texture(textureSampler, TexCoords));
|
||||
vec3 specular = point_lights[index].specular * spec * vec3(texture(diffuseSampler, TexCoords));
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation;
|
||||
specular *= attenuation;
|
||||
return (ambient + diffuse + specular);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 viewDir = normalize(viewPos.pos - FragPos);
|
||||
|
||||
vec3 result = calc_directional_light(norm, viewDir);
|
||||
for(int i = 0; i < pushConstants.light_count; i++)
|
||||
result += calc_point_light(i, norm, FragPos, viewDir);
|
||||
|
||||
outColor = vec4(result, 1.0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue