From 96f1e23a54a8028cd771585a950c8f5a614dd443 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Thu, 21 Jan 2021 15:35:21 -0600 Subject: [PATCH] Fix diffuse lighting --- shaders/flat.frag | 8 ++++++-- shaders/flat.vert | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/shaders/flat.frag b/shaders/flat.frag index f099139..4abea14 100644 --- a/shaders/flat.frag +++ b/shaders/flat.frag @@ -39,7 +39,11 @@ void main() { if (!material.unshaded) { vec3 ambient = directionalLight.ambient * directionalLight.color; - vec3 diffuse = directionalLight.diffuse * reflect(directionalLight.direction, NORMAL) * directionalLight.color; - FragColor *= vec4(ambient, 1.0); + ambient = max(ambient, 0.0); + + vec3 diffuse = directionalLight.diffuse * dot(directionalLight.direction, NORMAL) * directionalLight.color; + diffuse = max(diffuse, 0.0); + + FragColor *= vec4(ambient + diffuse, 1.0); } } diff --git a/shaders/flat.vert b/shaders/flat.vert index 0c248b6..89102ab 100644 --- a/shaders/flat.vert +++ b/shaders/flat.vert @@ -15,5 +15,5 @@ void main() { vec4 vertex = PROJECTION * VIEW * MODEL * vec4(pos, 1.0); gl_Position = vertex; UV = vec3(uv * vertex.z, vertex.z); - NORMAL = normal; + NORMAL = (PROJECTION * VIEW * MODEL * vec4(normal, 0.0)).xyz; }