OpenGL-utility/examples/movement/shaders/fragmentShader.frag

66 lines
1.5 KiB
GLSL
Raw Normal View History

2021-01-24 02:31:28 +00:00
#version 330 core
2021-01-27 21:42:47 +00:00
struct Material
{
2021-01-29 18:42:36 +00:00
sampler2D diffuse;
sampler2D specular;
2021-01-27 21:42:47 +00:00
float shininess;
};
2021-01-28 00:07:19 +00:00
struct Light
{
vec3 position;
vec3 ambient, diffuse, specular;
float ambientStrength;
2021-01-29 18:42:36 +00:00
float constant, linear, quadratic;
2021-01-28 00:07:19 +00:00
};
2021-01-29 19:36:17 +00:00
struct Flashlight
{
vec3 position, direction;
float angle, outerAngle;
vec3 diffuse, specular;
float constant, linear, quadratic;
};
2021-01-24 02:31:28 +00:00
in vec2 oUV;
2021-01-26 14:34:35 +00:00
in vec3 oNormal;
in vec3 oFragPos;
2021-01-24 02:31:28 +00:00
out vec4 FragColor;
2021-01-27 21:42:47 +00:00
uniform vec3 viewPos;
uniform Material material;
2021-01-28 00:07:19 +00:00
uniform Light light;
2021-01-29 19:36:17 +00:00
uniform Flashlight fl;
2021-01-27 21:42:47 +00:00
2021-01-24 02:31:28 +00:00
void main()
{
2021-01-29 19:36:17 +00:00
vec3 lightDir = normalize(fl.position - oFragPos);
float theta = dot(lightDir, normalize(-fl.direction));
float epsilon = fl.angle - fl.outerAngle;
float intensity = clamp((theta - fl.outerAngle) / epsilon, 0.0, 1.0);
2021-01-27 21:42:47 +00:00
2021-01-26 14:34:35 +00:00
vec3 norm = normalize(oNormal);
2021-01-27 21:42:47 +00:00
// Diffuse light
2021-01-26 14:34:35 +00:00
2021-01-27 21:42:47 +00:00
float diff = max(dot(norm, lightDir), 0.0);
2021-01-29 19:36:17 +00:00
vec3 diffuse = fl.diffuse * diff * vec3(texture(material.diffuse, oUV));
2021-01-27 21:42:47 +00:00
// Specular light
vec3 viewDir = normalize(viewPos - oFragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
2021-01-29 19:36:17 +00:00
vec3 specular = fl.specular * spec * vec3(texture(material.specular, oUV));
float dist = length(fl.position - oFragPos);
float attenuation = 1.0 / (fl.constant + fl.linear * dist + fl.quadratic * (dist * dist));
2021-01-26 14:34:35 +00:00
2021-01-29 19:36:17 +00:00
// Ambient light
vec3 ambient = light.ambient * light.ambientStrength * vec3(texture(material.diffuse, oUV));
2021-01-25 19:53:28 +00:00
2021-01-29 19:36:17 +00:00
FragColor = vec4(ambient + ((diffuse + specular) * attenuation * intensity), 1.0);
2021-01-24 02:31:28 +00:00
}