From e81487ae08a658811a0104ead4764ad71b449074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Fri, 16 Oct 2015 00:14:54 +0200 Subject: [PATCH] Added safety offset on lighting This avoids shadow algorithm to hit the surface it is currently checking --- src/render/software/GodRaysSampler.cpp | 1 + src/render/software/LightStatus.cpp | 8 +++++++- src/render/software/LightStatus.h | 10 ++++++++++ src/render/software/LightingManager.cpp | 8 +++++++- src/render/software/LightingManager.h | 9 +++++++-- 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/render/software/GodRaysSampler.cpp b/src/render/software/GodRaysSampler.cpp index 39c6e67..4233cd6 100644 --- a/src/render/software/GodRaysSampler.cpp +++ b/src/render/software/GodRaysSampler.cpp @@ -110,6 +110,7 @@ Color GodRaysSampler::getRawLight(const Vector3 &location, bool filtered) const if (lighting) { LightStatus status(lighting, location, *camera_location, filtered); + status.setSafetyOffset(0.0); // Not a hard surface, safety offset not needed lighting->fillStatus(status, location); return status.getSum(); } diff --git a/src/render/software/LightStatus.cpp b/src/render/software/LightStatus.cpp index 31555d1..e06baa5 100644 --- a/src/render/software/LightStatus.cpp +++ b/src/render/software/LightStatus.cpp @@ -7,6 +7,7 @@ LightStatus::LightStatus(LightingManager *manager, const Vector3 &location, const Vector3 &eye, bool filtered) { + this->safety_offset = -0.0000001; this->max_power = 0.0; this->manager = manager; this->location = location; @@ -25,7 +26,7 @@ void LightStatus::pushComponent(LightComponent component) return; } - if (manager->alterLight(component, location)) + if (manager->alterLight(component, location.add(component.direction.scale(safety_offset)))) { if (power > max_power) { @@ -64,3 +65,8 @@ Color LightStatus::getSum() const return final; } + +void LightStatus::setSafetyOffset(double safety_offset) +{ + this->safety_offset = -safety_offset; +} diff --git a/src/render/software/LightStatus.h b/src/render/software/LightStatus.h index 0f0d7c2..6de1251 100644 --- a/src/render/software/LightStatus.h +++ b/src/render/software/LightStatus.h @@ -29,7 +29,17 @@ public: */ Color getSum() const; + /** + * Set the safety offset for this status. + * + * The offset is used to not hit the surface we are currently working on immediately. + * It adds a small vector toward the light at the start of shadow casting. + * This number should remain very small to not produce precision errors. + */ + void setSafetyOffset(double safety_offset); + private: + double safety_offset; double max_power; LightingManager* manager; Vector3 location; diff --git a/src/render/software/LightingManager.cpp b/src/render/software/LightingManager.cpp index bb0bb96..5b087a2 100644 --- a/src/render/software/LightingManager.cpp +++ b/src/render/software/LightingManager.cpp @@ -10,6 +10,7 @@ LightingManager::LightingManager() { specularity = true; + filtering = true; } int LightingManager::getStaticLightsCount() const @@ -81,7 +82,7 @@ void LightingManager::unregisterFilter(LightFilter *filter) bool LightingManager::alterLight(LightComponent &component, const Vector3 &location) { - if (component.altered) + if (filtering and component.altered) { for (auto filter:filters) { @@ -105,6 +106,11 @@ void LightingManager::setSpecularity(bool enabled) specularity = enabled; } +void LightingManager::setFiltering(bool enabled) +{ + filtering = enabled; +} + Color LightingManager::applyFinalComponent(const LightComponent &component, const Vector3 &eye, const Vector3 &location, const Vector3 &normal, const SurfaceMaterial &material) { Color result, light_color; diff --git a/src/render/software/LightingManager.h b/src/render/software/LightingManager.h index f4c2544..49cf1ae 100644 --- a/src/render/software/LightingManager.h +++ b/src/render/software/LightingManager.h @@ -77,6 +77,11 @@ public: */ void setSpecularity(bool enabled); + /** + * Enable or disable the filtering (shadows). + */ + void setFiltering(bool enabled); + /** * Apply a final component on a surface material. */ @@ -93,11 +98,11 @@ public: Color apply(const Vector3 &eye, const Vector3 &location, const Vector3 &normal, const SurfaceMaterial &material); private: - int specularity; + bool specularity; + bool filtering; std::vector static_lights; std::vector filters; std::vector sources; - }; }