From c690bc6e1a92fae19922e48b58c4874e330b490a Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 7 Mar 2016 14:18:26 +0100 Subject: [PATCH] floor shader --- shader/floor.fragment.glsl | 34 ++++++++++++++++++++++++++++++++++ shader/floor.vertex.glsl | 16 ++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 shader/floor.fragment.glsl create mode 100644 shader/floor.vertex.glsl diff --git a/shader/floor.fragment.glsl b/shader/floor.fragment.glsl new file mode 100644 index 0000000..46f611c --- /dev/null +++ b/shader/floor.fragment.glsl @@ -0,0 +1,34 @@ +#version 330 core + +in vec3 view_position; +in vec3 view_normal; + +struct DirectionalLight { + // direction in view space + vec3 direction; + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; +uniform DirectionalLight global_light = DirectionalLight( + vec3(0.57735026918962576451), + vec3(0.1), + vec3(1.0), + vec3(1.0) +); + + +void main() { + // TODO: pull from texture (or some other source) + const vec3 material_color = vec3(1.0); + + vec3 normal = normalize(view_normal); + float cos_theta = dot(normal, global_light.direction); + float cos_alpha = dot(-normalize(view_position), reflect(-global_light.direction, normal)); + + vec3 ambient = global_light.ambient * material_color; + vec3 diffuse = global_light.diffuse * material_color * max(0.0, cos_theta); + vec3 specular = global_light.specular * pow(max(0.0, cos_alpha), 5); + + gl_FragColor = vec4(ambient + diffuse + specular, 1.0); +} diff --git a/shader/floor.vertex.glsl b/shader/floor.vertex.glsl new file mode 100644 index 0000000..b74d164 --- /dev/null +++ b/shader/floor.vertex.glsl @@ -0,0 +1,16 @@ +#version 330 core + +layout(location = 0) in vec3 vert_position; +layout(location = 1) in vec3 vert_normal; + +uniform mat4 MV; +uniform mat4 MVP; + +out vec3 view_position; +out vec3 view_normal; + +void main() { + gl_Position = MVP * vec4(vert_position, 1.0); + view_position = (MV * vec4(vert_position, 1.0)).xyz; + view_normal = mat3(MV) * vert_normal; +} -- 2.39.2