LearnOpenGL/shader.fs

54 lines
1.1 KiB
GLSL

#version 460 core
out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
struct Material {
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
struct Light{
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform Light light;
uniform Material material;
uniform vec3 viewPos;
void main()
{
// ambient lighting calculation
vec3 ambient = light.ambient * material.ambient;
// diffuse lighting calculation
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(light.position - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * ( diff * material.diffuse);
// specular lighting
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * ( spec * material.specular);
// Calculate the result;
vec3 result = ambient + diffuse + specular ;
FragColor = vec4(result, 1.0);
}