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

52 lines
1.2 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-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-27 21:42:47 +00:00
2021-01-24 02:31:28 +00:00
void main()
{
2021-01-27 21:42:47 +00:00
// Ambient light
2021-01-29 18:42:36 +00:00
vec3 ambient = light.ambient * light.ambientStrength * vec3(texture(material.diffuse, oUV));
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-28 00:07:19 +00:00
vec3 lightDir = normalize(light.position - oFragPos);
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 18:42:36 +00:00
vec3 diffuse = light.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 18:42:36 +00:00
vec3 specular = light.specular * spec * vec3(texture(material.specular, oUV));
2021-01-26 14:34:35 +00:00
2021-01-29 18:42:36 +00:00
float dist = length(light.position - oFragPos);
float attenuation = 1.0 / (light.constant + light.linear * dist + light.quadratic * (dist * dist));
2021-01-25 19:53:28 +00:00
2021-01-29 18:42:36 +00:00
FragColor = vec4((ambient + diffuse + specular) * attenuation, 1.0);
2021-01-24 02:31:28 +00:00
}