From 0683f62337dd9152e326da32c4636b8770c5a4e1 Mon Sep 17 00:00:00 2001 From: Andrey Doroschenko Date: Thu, 15 Sep 2022 10:23:22 +0300 Subject: [PATCH] Fixed attenuation for spot lighting --- include/core/ecs/component.hpp | 2 +- resources/shaders/default.frag | 11 +++++------ resources/shaders/material.frag | 11 +++++------ src/core/render/api.cpp | 3 +++ src/editor.cpp | 3 +++ 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/core/ecs/component.hpp b/include/core/ecs/component.hpp index 74357ed..0f7a77e 100644 --- a/include/core/ecs/component.hpp +++ b/include/core/ecs/component.hpp @@ -81,7 +81,7 @@ namespace Engine { // Only for directional and spot lighting glm::vec3 direction = glm::vec3(0.0f, 0.0f, 0.0f); - // Only for point lighting + // Only for point and spot lighting float constant = 1.0f; float linear = 0.35f; float quadratic = 0.44f; diff --git a/resources/shaders/default.frag b/resources/shaders/default.frag index d44f200..29244e5 100644 --- a/resources/shaders/default.frag +++ b/resources/shaders/default.frag @@ -124,13 +124,12 @@ vec3 calculateSpotLight(Light light, vec3 normal, vec3 fragPos, vec3 viewDir) { diffuse *= intensity; specular *= intensity; - // TODO: Why it's not work?? // Attenuation - // float distance = length(light.position - FragPos); - // float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); - // ambient *= attenuation; - // diffuse *= attenuation; - // specular *= attenuation; + float distance = length(light.position - fragPos); + float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); + ambient *= attenuation; + diffuse *= attenuation; + specular *= attenuation; return ambient + diffuse + specular; } diff --git a/resources/shaders/material.frag b/resources/shaders/material.frag index 7236193..25be696 100644 --- a/resources/shaders/material.frag +++ b/resources/shaders/material.frag @@ -134,13 +134,12 @@ vec3 calculateSpotLight(Light light, Material material, vec3 normal, vec3 fragPo diffuse *= intensity; specular *= intensity; - // TODO: Why it's not work?? // Attenuation - // float distance = length(light.position - FragPos); - // float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); - // ambient *= attenuation; - // diffuse *= attenuation; - // specular *= attenuation; + float distance = length(light.position - fragPos); + float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); + ambient *= attenuation; + diffuse *= attenuation; + specular *= attenuation; return ambient + diffuse + specular; } diff --git a/src/core/render/api.cpp b/src/core/render/api.cpp index 228e7d7..a90b491 100644 --- a/src/core/render/api.cpp +++ b/src/core/render/api.cpp @@ -167,6 +167,9 @@ void Render::RenderObject(Object *object, glm::mat4 projection, glm::mat4 view, lighting->light->direction.y, lighting->light->direction.z ); + shader->UniformFloat("light.constant", lighting->light->constant); + shader->UniformFloat("light.linear", lighting->light->linear); + shader->UniformFloat("light.quadratic", lighting->light->quadratic); shader->UniformFloat("light.cutOff", glm::cos(glm::radians(lighting->light->cutOff))); shader->UniformFloat("light.outerCutOff", glm::cos(glm::radians(lighting->light->outerCutOff))); } diff --git a/src/editor.cpp b/src/editor.cpp index f4ac836..47c9ea6 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -169,6 +169,9 @@ namespace Engine { case Engine::Graphics::Lighting::Type::SPOT: ImGui::SliderFloat("cutOff", &selectedEntity->light->cutOff, 0, 90.0); ImGui::SliderFloat("outerCutOff", &selectedEntity->light->outerCutOff, 0, 90.0); + ImGui::SliderFloat("constant", &selectedEntity->light->constant, 0, 1.0); + ImGui::SliderFloat("linear", &selectedEntity->light->linear, 0, 1.0); + ImGui::SliderFloat("quadratic", &selectedEntity->light->quadratic, 0, 1.0); ImGui::SliderFloat3("direction", &selectedEntity->light->direction[0], -10.0, 10.0); break; default: