Skip to content

Commit

Permalink
Fixed attenuation for spot lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
dorosch committed Sep 15, 2022
1 parent 20e5aaa commit 0683f62
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/core/ecs/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions resources/shaders/default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 5 additions & 6 deletions resources/shaders/material.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions src/core/render/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
3 changes: 3 additions & 0 deletions src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0683f62

Please sign in to comment.