voxel-engine/shaders/shader.fs

45 lines
1.1 KiB
Forth
Raw Permalink Normal View History

#version 330 core
2023-03-26 22:21:36 +02:00
in vec3 vColor;
in vec3 vNormal;
2023-03-26 22:21:36 +02:00
in vec3 FragPos;
out vec4 FragColor;
2023-03-26 22:21:36 +02:00
vec3 lightColor = vec3(1.0);
vec3 lightDir = -normalize(vec3(0.0, 100.0, 0.0) - vec3(32.0));
2023-03-27 19:19:27 +02:00
float ambientStrength = 0.05;
float diffuseStrength = 0.7;
float specularStrength = 0.25;
2023-03-26 22:21:36 +02:00
uniform vec3 viewPos;
uniform float u_time;
void main()
{
2023-03-26 22:21:36 +02:00
// offset the normal a tiny bit, so that the color of faces opposing lightDir is not completely
// flat
vec3 normal = normalize(vNormal);
// Blinn-Phong lighting
// Ambient
vec3 ambient = lightColor*vColor;
// Diffuse
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = vColor * diff;
// Blinn Specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0);
vec3 specular = lightColor * vColor * spec;
// Final color
vec3 color = ambient * ambientStrength + diffuse * diffuseStrength + specular * specularStrength;
2023-03-27 19:19:27 +02:00
float gamma = 2.2;
FragColor.rgb = pow(color.rgb, vec3(1.0/gamma));
}