LearnOpenGL/shader.fs

56 lines
1.3 KiB
Forth
Raw Normal View History

2022-02-11 21:24:15 +00:00
#version 460 core
out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
2022-02-16 18:46:19 +00:00
in vec2 TexCoords;
struct Material {
2022-02-16 18:46:19 +00:00
sampler2D diffuse;
sampler2D specular;
float shininess;
};
struct Light{
2022-02-16 18:57:18 +00:00
// vec3 position; // Not neccessary when using directional lights.
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform Light light;
uniform Material material;
uniform vec3 viewPos;
2022-02-11 21:24:15 +00:00
2022-02-12 20:38:31 +00:00
2022-02-11 21:24:15 +00:00
void main()
{
// ambient lighting calculation
2022-02-16 18:46:19 +00:00
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
// diffuse lighting calculation
vec3 norm = normalize(Normal);
2022-02-16 18:57:18 +00:00
vec3 lightDir = normalize(-light.direction); // simplified with directional lights normalize(light.position - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
2022-02-16 18:46:19 +00:00
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
// specular lighting
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
2022-02-16 18:46:19 +00:00
vec3 specular = light.specular * spec * vec3( texture(material.specular, TexCoords));
// Calculate the result;
vec3 result = ambient + diffuse + specular ;
FragColor = vec4(result, 1.0);
2022-02-11 21:24:15 +00:00
}