Implement flashlight

This commit is contained in:
2023-10-09 19:33:09 -04:00
parent c98c7efcdb
commit c202e47d23
2 changed files with 233 additions and 113 deletions

View File

@@ -1,38 +1,57 @@
#version 460 core
out vec4 FragColor;
in vec3 Normal;
struct Material {
//sampler2D diffuse;
//sampler2D specular;
float shininess;
vec3 color;
};
struct Light {
vec3 position;
vec3 direction;
float largecutoff;
float smallcutoff;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
in vec3 FragPos;
in vec3 Normal;
uniform vec3 viewPos;
uniform vec3 lightColor;
uniform vec3 objectColor;
uniform Material material;
uniform Light light;
void main()
{
// ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
vec3 lightDir = normalize(light.position - FragPos);
vec3 vecDist = viewPos - FragPos;
vec3 normDist = normalize(vecDist);
// check if lighting is inside the spotlight cone
float theta = dot(lightDir, normalize(-light.direction));
float co = clamp((theta - light.largecutoff) / (light.smallcutoff - light.largecutoff), 0.0, 1.0);
// diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normDist;
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec3 diffuse = light.diffuse * diff * material.color * co;
// specular
float specularStrength = 0.5;
vec3 viewDir = normDist;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 8);
vec3 specular = specularStrength * spec * lightColor;
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * spec * material.color * co;
float dist = distance(viewPos, FragPos);
float att = 1.0 / (1.0 + 0.1*dist + 0.01*dist*dist);
// attenuation
float distance = length(light.position - FragPos);
float atten = 1.0 / (1.0 + 0.1 * distance + 0.01 * (distance * distance));
vec3 result = (ambient + diffuse + specular) * att * objectColor;
diffuse *= atten;
specular *= atten;
vec3 result = light.ambient + diffuse + specular;
FragColor = vec4(result, 1.0);
}