From 331056c2064f05bb25d0e8d72cdbd5f73dd5fbbb Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Thu, 4 Mar 2021 14:49:48 -0600 Subject: [PATCH] Add ambient light from point lights --- core/Shaders/Shader.cpp | 2 ++ demo/exampleworld/main.lua | 3 ++- shaders/flat.vert | 16 +++++++++++----- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/core/Shaders/Shader.cpp b/core/Shaders/Shader.cpp index d1e4c3b..6e8d5d4 100644 --- a/core/Shaders/Shader.cpp +++ b/core/Shaders/Shader.cpp @@ -91,6 +91,8 @@ void Shader::UpdatePointLights(PointLightList pointLights) { glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "radius").c_str()), pointLights[i]->GetRadius()); glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "ambient").c_str()), pointLights[i]->GetAmbient()); + glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "diffuse").c_str()), pointLights[i]->GetDiffuse()); + glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "specular").c_str()), pointLights[i]->GetSpecular()); } } diff --git a/demo/exampleworld/main.lua b/demo/exampleworld/main.lua index 68656b1..a5a78a6 100644 --- a/demo/exampleworld/main.lua +++ b/demo/exampleworld/main.lua @@ -185,7 +185,8 @@ function init_point_lights() pointLight:Translate(couch.Vector3(i * -10.0, 0, -10)) pointLight:SetRadius(10.0) pointLight:SetColor(color) - pointLight:SetAmbient(1.0) + pointLight:SetAmbient(0.2) + pointLight:SetDiffuse(1.0) pointLight:SetSpecular(0.1) local lightBox = couch.Mesh.FromFile("../resources/cube.obj") lightBox:UniformScale(0.5); diff --git a/shaders/flat.vert b/shaders/flat.vert index 5f98d3c..dd7295c 100644 --- a/shaders/flat.vert +++ b/shaders/flat.vert @@ -55,13 +55,16 @@ struct Material { uniform DirectionalLight directionalLight; uniform Material material; +float calcDiffuseIntensity(in vec3 direction, in vec3 normal) { + float diff = dot(normalize(direction), normalize(normal)); + return max(diff, 0.0); +} + void calcDirectionalLight(in vec3 normal) { AMBIENT += directionalLight.ambient * directionalLight.color * material.ambient; vec3 direction = -(VIEW * vec4(directionalLight.direction, 0.0)).xyz; - float diff = dot(normalize(direction), normalize(normal)); - diff = max(diff, 0.0); - DIFFUSE += directionalLight.diffuse * diff * directionalLight.color * material.diffuse; + DIFFUSE += directionalLight.diffuse * directionalLight.color * material.diffuse * calcDiffuseIntensity(direction, normal); vec3 viewDir = normalize((VIEW * MODEL * vec4(pos, 1.0)).xyz); vec3 reflectionDir = reflect(normalize(direction), normalize(normal)); @@ -72,10 +75,13 @@ void calcDirectionalLight(in vec3 normal) { } void calcPointLight(in vec3 normal, in PointLight pointLight) { - vec4 ray = VIEW * MODEL * vec4(pos, 1.0) - VIEW * vec4(pointLight.pos, 1.0); - float dist = length(ray); + vec3 direction = vec3(VIEW * vec4(pointLight.pos, 1.0) - VIEW * MODEL * vec4(pos, 1.0)).xyz; + float dist = length(direction); + float attenuation = max(1.0 - dist * dist / pointLight.radius / pointLight.radius, 0.0); AMBIENT += pointLight.color * material.ambient * pointLight.ambient * attenuation; + + DIFFUSE += pointLight.color * pointLight.diffuse * material.diffuse * calcDiffuseIntensity(direction, normal); } void main() {