diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 151fb7990d..81a33f17e3 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -763,7 +763,8 @@ void DefaultLightingSetup::run(const RenderContextPointer& renderContext) { _defaultLight = lp; // Add the global light to the light stage (for later shadow rendering) - _defaultLightID = lightStage->addLight(lp); + // Set this light to be the default + _defaultLightID = lightStage->addLight(lp, true); lightStage->addShadow(_defaultLightID); } diff --git a/libraries/render-utils/src/LightStage.cpp b/libraries/render-utils/src/LightStage.cpp index 0cc30495b8..d62f52047b 100644 --- a/libraries/render-utils/src/LightStage.cpp +++ b/libraries/render-utils/src/LightStage.cpp @@ -34,28 +34,31 @@ LightStage::LightStage() { ambientOffLight->setColor(model::Vec3(0.0)); ambientOffLight->setIntensity(0.0f); ambientOffLight->setType(model::Light::Type::AMBIENT); - _ambientOffLight = addLight(ambientOffLight); + _ambientOffLightId = addLight(ambientOffLight); const LightPointer pointOffLight { std::make_shared() }; pointOffLight->setAmbientIntensity(0.0f); pointOffLight->setColor(model::Vec3(0.0)); pointOffLight->setIntensity(0.0f); pointOffLight->setType(model::Light::Type::POINT); - _pointOffLight = addLight(pointOffLight); + _pointOffLightId = addLight(pointOffLight); const LightPointer spotOffLight { std::make_shared() }; spotOffLight->setAmbientIntensity(0.0f); spotOffLight->setColor(model::Vec3(0.0)); spotOffLight->setIntensity(0.0f); spotOffLight->setType(model::Light::Type::SPOT); - _spotOffLight = addLight(spotOffLight); + _spotOffLightId = addLight(spotOffLight); const LightPointer sunOffLight { std::make_shared() }; sunOffLight->setAmbientIntensity(0.0f); sunOffLight->setColor(model::Vec3(0.0)); sunOffLight->setIntensity(0.0f); sunOffLight->setType(model::Light::Type::SUN); - _sunOffLight = addLight(sunOffLight); + _sunOffLightId = addLight(sunOffLight); + + // Set default light to the off ambient light (until changed) + _defaultLightId = _ambientOffLightId; } LightStage::Shadow::Schema::Schema() { @@ -294,10 +297,12 @@ LightStage::Index LightStage::findLight(const LightPointer& light) const { } } -LightStage::Index LightStage::addLight(const LightPointer& light) { +LightStage::Index LightStage::addLight(const LightPointer& light, const bool shouldSetAsDefault) { + Index lightId; + auto found = _lightMap.find(light); if (found == _lightMap.end()) { - auto lightId = _lights.newElement(light); + lightId = _lights.newElement(light); // Avoid failing to allocate a light, just pass if (lightId != INVALID_INDEX) { @@ -314,10 +319,15 @@ LightStage::Index LightStage::addLight(const LightPointer& light) { updateLightArrayBuffer(lightId); } - return lightId; } else { - return (*found).second; + lightId = (*found).second; } + + if (shouldSetAsDefault) { + _defaultLightId = lightId; + } + + return lightId; } LightStage::Index LightStage::addShadow(Index lightIndex, float maxDistance, unsigned int cascadeCount) { @@ -349,24 +359,27 @@ LightStage::LightPointer LightStage::removeLight(Index index) { } LightStage::LightPointer LightStage::getCurrentKeyLight() const { - Index keyLightId{ 0 }; - if (!_currentFrame._sunLights.empty()) { + Index keyLightId{ _defaultLightId }; + // There is always at least 1 light (the off light) + if (_currentFrame._sunLights.size() > 1) { keyLightId = _currentFrame._sunLights.front(); } return _lights.get(keyLightId); } LightStage::LightPointer LightStage::getCurrentAmbientLight() const { - Index keyLightId{ 0 }; - if (!_currentFrame._ambientLights.empty()) { + Index keyLightId { _defaultLightId }; + // There is always at least 1 light (the off light) + if (_currentFrame._ambientLights.size() > 1) { keyLightId = _currentFrame._ambientLights.front(); } return _lights.get(keyLightId); } LightStage::ShadowPointer LightStage::getCurrentKeyShadow() const { - Index keyLightId{ 0 }; - if (!_currentFrame._sunLights.empty()) { + Index keyLightId { _defaultLightId }; + // There is always at least 1 light (the off light) + if (_currentFrame._sunLights.size() > 1) { keyLightId = _currentFrame._sunLights.front(); } auto shadow = getShadow(keyLightId); @@ -375,8 +388,9 @@ LightStage::ShadowPointer LightStage::getCurrentKeyShadow() const { } LightStage::LightAndShadow LightStage::getCurrentKeyLightAndShadow() const { - Index keyLightId{ 0 }; - if (!_currentFrame._sunLights.empty()) { + Index keyLightId { _defaultLightId }; + // There is always at least 1 light (the off light) + if (_currentFrame._sunLights.size() > 1) { keyLightId = _currentFrame._sunLights.front(); } auto shadow = getShadow(keyLightId); diff --git a/libraries/render-utils/src/LightStage.h b/libraries/render-utils/src/LightStage.h index 5ba0b02131..b47e454b88 100644 --- a/libraries/render-utils/src/LightStage.h +++ b/libraries/render-utils/src/LightStage.h @@ -118,7 +118,7 @@ public: using Shadows = render::indexed_container::IndexedPointerVector; Index findLight(const LightPointer& light) const; - Index addLight(const LightPointer& light); + Index addLight(const LightPointer& light, const bool shouldSetAsDefault = false); Index addShadow(Index lightIndex, float maxDistance = 20.0f, unsigned int cascadeCount = 1U); @@ -185,10 +185,10 @@ public: Frame _currentFrame; - Index getAmbientOffLight() { return _ambientOffLight; } - Index getPointOffLight() { return _pointOffLight; } - Index getSpotOffLight() { return _spotOffLight; } - Index getSunOffLight() { return _sunOffLight; } + Index getAmbientOffLight() { return _ambientOffLightId; } + Index getPointOffLight() { return _pointOffLightId; } + Index getSpotOffLight() { return _spotOffLightId; } + Index getSunOffLight() { return _sunOffLightId; } protected: @@ -205,17 +205,12 @@ protected: LightMap _lightMap; // define off lights + Index _ambientOffLightId; + Index _pointOffLightId; + Index _spotOffLightId; + Index _sunOffLightId; - const LightPointer ambientOffLight { std::make_shared() }; - const LightPointer pointOffLight { std::make_shared() }; - const LightPointer spotOffLight { std::make_shared() }; - const LightPointer sunOffLight { std::make_shared() }; - - Index _ambientOffLight; - Index _pointOffLight; - Index _spotOffLight; - Index _sunOffLight; - + Index _defaultLightId; }; using LightStagePointer = std::shared_ptr;