couch/shaders/flat.vert

49 lines
1.3 KiB
GLSL
Raw Normal View History

2021-01-13 10:42:57 -06:00
#version 330 core
layout (location = 0) in vec3 pos;
2021-01-21 15:26:39 -06:00
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 uv;
2021-01-13 10:42:57 -06:00
2021-01-13 13:17:31 -06:00
uniform mat4 MODEL;
uniform mat4 VIEW;
uniform mat4 PROJECTION;
2021-01-13 10:42:57 -06:00
2021-01-19 16:08:37 -06:00
out vec3 UV;
2021-01-21 15:26:39 -06:00
out vec3 NORMAL;
2021-01-21 18:29:49 -06:00
flat out vec3 LIGHT;
2021-01-21 18:03:50 -06:00
struct DirectionalLight {
vec3 direction;
vec3 color;
float ambient;
float diffuse;
float specular;
};
uniform DirectionalLight directionalLight;
2021-01-13 10:42:57 -06:00
void main() {
2021-01-19 16:08:37 -06:00
vec4 vertex = PROJECTION * VIEW * MODEL * vec4(pos, 1.0);
gl_Position = vertex;
UV = vec3(uv * vertex.z, vertex.z);
2021-01-21 18:03:50 -06:00
NORMAL = (VIEW * MODEL * vec4(normal, 0.0)).xyz;
// Flat shading, we compute light per vertex
vec3 ambient = directionalLight.ambient * directionalLight.color;
vec3 direction = -(VIEW * vec4(directionalLight.direction, 0.0)).xyz;
float diff = dot(normalize(direction), normalize(NORMAL));
diff = max(diff, 0.0);
vec3 diffuse = directionalLight.diffuse * diff * directionalLight.color;
vec3 viewDir = (VIEW * MODEL * vec4(pos, 1.0)).xyz;
vec3 reflectionDir = reflect(normalize(direction), normalize(NORMAL));
float spec = dot(viewDir, reflectionDir);
spec = max(spec, 0.0);
spec = pow(spec, 2);
vec3 specular = directionalLight.specular * spec * directionalLight.color;
LIGHT = ambient + diffuse + specular;
2021-01-13 10:42:57 -06:00
}