Have the original lighting system back and a little lightbulb model to show where the light is.
This commit is contained in:
parent
3231f747a9
commit
a29494325b
143 changed files with 16360 additions and 9 deletions
162
33-shadow-mapping/shaders/16-frag.glsl
Normal file
162
33-shadow-mapping/shaders/16-frag.glsl
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
#version 330 core
|
||||
struct Material {
|
||||
sampler2D diffuse;
|
||||
sampler2D specular;
|
||||
float shininess;
|
||||
};
|
||||
|
||||
out vec4 FragColor;
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform Material material;
|
||||
|
||||
struct DirLight {
|
||||
vec3 direction;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
|
||||
struct PointLight {
|
||||
vec3 position;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
};
|
||||
|
||||
struct SpotLight {
|
||||
vec3 direction;
|
||||
vec3 position;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 ambient;
|
||||
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
float cut_off;
|
||||
float outer_cut_off;
|
||||
};
|
||||
|
||||
|
||||
#define MAX_LIGHTS 4
|
||||
uniform int pointLightCount = 0;
|
||||
uniform PointLight pointLights[MAX_LIGHTS];
|
||||
|
||||
uniform int dirLightCount = 0;
|
||||
uniform DirLight dirLights[MAX_LIGHTS];
|
||||
|
||||
uniform int spotLightCount = 0;
|
||||
uniform SpotLight spotLights[MAX_LIGHTS];
|
||||
|
||||
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
|
||||
{
|
||||
vec3 mat_tex = vec3(texture(material.diffuse, TexCoords));
|
||||
vec3 spec_tex = vec3(texture(material.specular, TexCoords));
|
||||
|
||||
vec3 lightDir = normalize(-light.direction);
|
||||
// diffuse shading
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
// specular shading
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
|
||||
vec3 ambient = light.ambient * mat_tex;
|
||||
vec3 diffuse = light.diffuse * diff * mat_tex;
|
||||
vec3 specular = light.specular * spec * spec_tex;
|
||||
|
||||
return ambient + diffuse + specular;
|
||||
}
|
||||
|
||||
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
|
||||
{
|
||||
vec3 mat_tex = vec3(texture(material.diffuse, TexCoords));
|
||||
vec3 spec_tex = vec3(texture(material.specular, TexCoords));
|
||||
vec3 lightDir = normalize(light.position - fragPos);
|
||||
|
||||
// diffuse shading
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
// specular shading
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
|
||||
// attenuation
|
||||
float distance = length(light.position - fragPos);
|
||||
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
||||
|
||||
vec3 ambient = light.ambient * mat_tex;
|
||||
vec3 diffuse = light.diffuse * diff * mat_tex;
|
||||
vec3 specular = light.specular * spec * spec_tex;
|
||||
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation;
|
||||
specular *= attenuation;
|
||||
|
||||
return ambient + diffuse + specular;
|
||||
}
|
||||
|
||||
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
|
||||
{
|
||||
vec3 mat_tex = vec3(texture(material.diffuse, TexCoords));
|
||||
vec3 spec_tex = vec3(texture(material.specular, TexCoords));
|
||||
vec3 lightDir = normalize(light.position - fragPos);
|
||||
|
||||
// diffuse shading
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
// specular shading
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
|
||||
// attenuation
|
||||
float distance = length(light.position - fragPos);
|
||||
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
||||
|
||||
vec3 ambient = light.ambient * mat_tex;
|
||||
vec3 diffuse = light.diffuse * diff * mat_tex;
|
||||
vec3 specular = light.specular * spec * spec_tex;
|
||||
|
||||
float theta = dot(lightDir, normalize(-light.direction));
|
||||
float epsilon = light.cut_off - light.outer_cut_off;
|
||||
float intensity = clamp((theta - light.outer_cut_off) / epsilon, 0.0, 1.0);
|
||||
diffuse *= intensity;
|
||||
specular *= intensity;
|
||||
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation;
|
||||
specular *= attenuation;
|
||||
|
||||
return ambient + diffuse + specular;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
|
||||
// kick it off with the first dirlight
|
||||
vec3 result = CalcDirLight(dirLights[0], norm, viewDir);
|
||||
|
||||
// then augment with more (off by one)
|
||||
for(int i = 1; i < dirLightCount; i++) {
|
||||
result += CalcDirLight(dirLights[i], norm, viewDir);
|
||||
}
|
||||
|
||||
for(int i = 0; i < pointLightCount; i++) {
|
||||
result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
|
||||
}
|
||||
|
||||
for(int i = 0; i < spotLightCount; i++) {
|
||||
result += CalcSpotLight(spotLights[i], norm, FragPos, viewDir);
|
||||
}
|
||||
|
||||
FragColor = vec4(result, 1.0);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue