From 95d98e3ed7e7930a91926590807cab9d238c30b9 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Wed, 24 Feb 2016 14:58:41 -0800 Subject: [PATCH] Wire up zone ambientURL --- interface/src/Application.cpp | 14 ++----- .../src/EntityTreeRenderer.cpp | 38 +++++++++++++++++-- .../src/EntityTreeRenderer.h | 3 ++ libraries/model/src/model/Stage.cpp | 15 ++++---- libraries/model/src/model/Stage.h | 9 +++-- .../src/DeferredLightingEffect.cpp | 33 ++++------------ .../render-utils/src/DeferredLightingEffect.h | 4 +- .../src/SceneScriptingInterface.cpp | 4 ++ .../src/SceneScriptingInterface.h | 4 ++ 9 files changed, 69 insertions(+), 55 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 1ff448f35f..6205d3291b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3816,18 +3816,10 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se }); } - // Setup the current Zone Entity lighting and skybox + // Setup the current Zone Entity lighting { - // FIXME: Use a zone setting to determine the ambient light mode - DependencyManager::get()->setAmbientLightMode(-1); - auto skyStage = DependencyManager::get()->getSkyStage(); - DependencyManager::get()->setGlobalLight(skyStage->getSunLight()->getDirection(), skyStage->getSunLight()->getColor(), skyStage->getSunLight()->getIntensity(), skyStage->getSunLight()->getAmbientIntensity()); - - auto skybox = model::SkyboxPointer(); - if (skyStage->getBackgroundMode() == model::SunSkyStage::SKY_BOX) { - skybox = skyStage->getSkybox(); - } - DependencyManager::get()->setGlobalSkybox(skybox); + auto sun = DependencyManager::get()->getSkyStage()->getSunLight(); + DependencyManager::get()->setGlobalLight(sun); } { diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index 85a4b13b29..3bf1e7bc92 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -140,7 +140,8 @@ void EntityTreeRenderer::update() { // even if we haven't changed positions, if we previously attempted to set the skybox, but // have a pending download of the skybox texture, then we should attempt to reapply to // get the correct texture. - if (_pendingSkyboxTexture && _skyboxTexture && _skyboxTexture->isLoaded()) { + if ((_pendingSkyboxTexture && _skyboxTexture && _skyboxTexture->isLoaded()) || + (_pendingAmbientTexture && _ambientTexture && _ambientTexture->isLoaded())) { applyZonePropertiesToScene(_bestZone); } @@ -253,6 +254,7 @@ void EntityTreeRenderer::forceRecheckEntities() { void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptr zone) { + auto textureCache = DependencyManager::get(); auto scene = DependencyManager::get(); auto sceneStage = scene->getStage(); auto skyStage = scene->getSkyStage(); @@ -264,7 +266,11 @@ void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptrresetAmbientSphere(); sceneKeyLight->setColor(_previousKeyLightColor); sceneKeyLight->setIntensity(_previousKeyLightIntensity); sceneKeyLight->setAmbientIntensity(_previousKeyLightAmbientIntensity); @@ -274,6 +280,7 @@ void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptrsetHour(_previousStageHour); sceneTime->setDay(_previousStageDay); + _hasPreviousZone = false; } @@ -306,6 +313,23 @@ void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptrsetHour(zone->getStageProperties().calculateHour()); sceneTime->setDay(zone->getStageProperties().calculateDay()); + bool isAmbientTextureSet = false; + if (zone->getKeyLightProperties().getAmbientURL().isEmpty()) { + _pendingAmbientTexture = false; + _ambientTexture.clear(); + } else { + _ambientTexture = textureCache->getTexture(zone->getKeyLightProperties().getAmbientURL(), CUBE_TEXTURE); + if (_ambientTexture->getGPUTexture()) { + _pendingAmbientTexture = false; + if (_ambientTexture->getGPUTexture()->getIrradiance()) { + sceneKeyLight->setAmbientSphere(_ambientTexture->getGPUTexture()->getIrradiance()); + isAmbientTextureSet = true; + } + } else { + _pendingAmbientTexture = true; + } + } + switch (zone->getBackgroundMode()) { case BACKGROUND_MODE_SKYBOX: { auto skybox = std::dynamic_pointer_cast(skyStage->getSkybox()); @@ -326,12 +350,16 @@ void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptr(); _skyboxTexture = textureCache->getTexture(zone->getSkyboxProperties().getURL(), CUBE_TEXTURE); if (_skyboxTexture->getGPUTexture()) { - skybox->setCubemap(_skyboxTexture->getGPUTexture()); + auto texture = _skyboxTexture->getGPUTexture(); + skybox->setCubemap(texture); _pendingSkyboxTexture = false; + if (!isAmbientTextureSet && texture->getIrradiance()) { + sceneKeyLight->setAmbientSphere(texture->getIrradiance()); + isAmbientTextureSet = true; + } } else { _pendingSkyboxTexture = true; } @@ -348,6 +376,10 @@ void EntityTreeRenderer::applyZonePropertiesToScene(std::shared_ptrresetAmbientSphere(); + } } const FBXGeometry* EntityTreeRenderer::getGeometryForEntity(EntityItemPointer entityItem) { diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.h b/libraries/entities-renderer/src/EntityTreeRenderer.h index f0454b2ecf..f1598221dd 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.h +++ b/libraries/entities-renderer/src/EntityTreeRenderer.h @@ -146,6 +146,9 @@ private: bool _pendingSkyboxTexture { false }; NetworkTexturePointer _skyboxTexture; + bool _pendingAmbientTexture { false }; + NetworkTexturePointer _ambientTexture; + bool _wantScripts; ScriptEngine* _entitiesScriptEngine; diff --git a/libraries/model/src/model/Stage.cpp b/libraries/model/src/model/Stage.cpp index 0ff613bd4b..6a1e95936b 100644 --- a/libraries/model/src/model/Stage.cpp +++ b/libraries/model/src/model/Stage.cpp @@ -202,14 +202,13 @@ void SunSkyStage::setSunModelEnable(bool isEnabled) { invalidate(); } -void SunSkyStage::setSunColor(const Vec3& color) { - _sunLight->setColor(color); -} -void SunSkyStage::setSunIntensity(float intensity) { - _sunLight->setIntensity(intensity); -} -void SunSkyStage::setSunAmbientIntensity(float intensity) { - _sunLight->setAmbientIntensity(intensity); +void SunSkyStage::setSunAmbientSphere(const gpu::SHPointer& sphere) { + if (sphere) { + _sunLight->setAmbientSphere(*sphere); + } else { + const gpu::SphericalHarmonics::Preset DEFAULT_AMBIENT_SPHERE = gpu::SphericalHarmonics::OLD_TOWN_SQUARE; + _sunLight->setAmbientSpherePreset(DEFAULT_AMBIENT_SPHERE); + } } void SunSkyStage::setSunDirection(const Vec3& direction) { diff --git a/libraries/model/src/model/Stage.h b/libraries/model/src/model/Stage.h index 912a7b77a8..31772d5e48 100644 --- a/libraries/model/src/model/Stage.h +++ b/libraries/model/src/model/Stage.h @@ -11,7 +11,7 @@ #ifndef hifi_model_Stage_h #define hifi_model_Stage_h -#include "gpu/Pipeline.h" +#include #include "Light.h" #include "Skybox.h" @@ -143,12 +143,13 @@ public: bool isSunModelEnabled() const { return _sunModelEnable; } // Sun properties - void setSunColor(const Vec3& color); + void setSunColor(const Vec3& color) { _sunLight->setColor(color); } const Vec3& getSunColor() const { return getSunLight()->getColor(); } - void setSunIntensity(float intensity); + void setSunIntensity(float intensity) { _sunLight->setIntensity(intensity); } float getSunIntensity() const { return getSunLight()->getIntensity(); } - void setSunAmbientIntensity(float intensity); + void setSunAmbientIntensity(float intensity) { _sunLight->setAmbientIntensity(intensity); } float getSunAmbientIntensity() const { return getSunLight()->getAmbientIntensity(); } + void setSunAmbientSphere(const gpu::SHPointer& sphere); // The sun direction is expressed in the world space void setSunDirection(const Vec3& direction); diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index e39c3f5de2..605dfaa2b9 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -562,32 +562,13 @@ static void loadLightProgram(const char* vertSource, const char* fragSource, boo } -void DeferredLightingEffect::setAmbientLightMode(int preset) { - if ((preset >= 0) && (preset < gpu::SphericalHarmonics::NUM_PRESET)) { - _ambientLightMode = preset; - auto light = _allocatedLights.front(); - light->setAmbientSpherePreset(gpu::SphericalHarmonics::Preset(preset % gpu::SphericalHarmonics::NUM_PRESET)); - } else { - // force to preset 0 - setAmbientLightMode(0); - } -} - -void DeferredLightingEffect::setGlobalLight(const glm::vec3& direction, const glm::vec3& diffuse, float intensity, float ambientIntensity) { - auto light = _allocatedLights.front(); - light->setDirection(direction); - light->setColor(diffuse); - light->setIntensity(intensity); - light->setAmbientIntensity(ambientIntensity); -} - -void DeferredLightingEffect::setGlobalSkybox(const model::SkyboxPointer& skybox) { - _skybox = skybox; - auto light = _allocatedLights.front(); - - if (_skybox && _skybox->getCubemap() && _skybox->getCubemap()->isDefined() && _skybox->getCubemap()->getIrradiance()) { - light->setAmbientSphere( (*_skybox->getCubemap()->getIrradiance()) ); - } +void DeferredLightingEffect::setGlobalLight(const model::LightPointer& light) { + auto globalLight = _allocatedLights.front(); + globalLight->setDirection(light->getDirection()); + globalLight->setColor(light->getColor()); + globalLight->setIntensity(light->getIntensity()); + globalLight->setAmbientIntensity(light->getAmbientIntensity()); + globalLight->setAmbientSphere(light->getAmbientSphere()); } model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 8cb4bbba8f..b7a32c2c16 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -49,9 +49,7 @@ public: void setupKeyLightBatch(gpu::Batch& batch, int lightBufferUnit, int skyboxCubemapUnit); // update global lighting - void setAmbientLightMode(int preset); - void setGlobalLight(const glm::vec3& direction, const glm::vec3& diffuse, float intensity, float ambientIntensity); - void setGlobalSkybox(const model::SkyboxPointer& skybox); + void setGlobalLight(const model::LightPointer& light); const LightStage& getLightStage() { return _lightStage; } void setShadowMapEnabled(bool enable) { _shadowMapEnabled = enable; }; diff --git a/libraries/script-engine/src/SceneScriptingInterface.cpp b/libraries/script-engine/src/SceneScriptingInterface.cpp index b2f3824221..079cfff2c7 100644 --- a/libraries/script-engine/src/SceneScriptingInterface.cpp +++ b/libraries/script-engine/src/SceneScriptingInterface.cpp @@ -77,6 +77,10 @@ void SceneScripting::KeyLight::setAmbientIntensity(float intensity) { _skyStage->setSunAmbientIntensity(intensity); } +void SceneScripting::KeyLight::setAmbientSphere(const gpu::SHPointer& sphere) { + _skyStage->setSunAmbientSphere(sphere); +} + glm::vec3 SceneScripting::KeyLight::getDirection() const { return _skyStage->getSunDirection(); } diff --git a/libraries/script-engine/src/SceneScriptingInterface.h b/libraries/script-engine/src/SceneScriptingInterface.h index 0be8b066aa..12681b1887 100644 --- a/libraries/script-engine/src/SceneScriptingInterface.h +++ b/libraries/script-engine/src/SceneScriptingInterface.h @@ -81,6 +81,10 @@ namespace SceneScripting { // setDirection is only effective if stage Sun model is disabled void setDirection(const glm::vec3& direction); + // AmbientTexture is unscriptable - it must be set through the zone entity + void setAmbientSphere(const gpu::SHPointer& sphere); + void resetAmbientSphere() { setAmbientSphere(nullptr); } + protected: model::SunSkyStagePointer _skyStage; };