diff --git a/src/definition/DefinitionWatcher.cpp b/src/definition/DefinitionWatcher.cpp index 888ed13..6c46820 100644 --- a/src/definition/DefinitionWatcher.cpp +++ b/src/definition/DefinitionWatcher.cpp @@ -1,4 +1,16 @@ #include "DefinitionWatcher.h" +#include "DefinitionNode.h" +#include "Logs.h" + DefinitionWatcher::DefinitionWatcher() { } + +void DefinitionWatcher::startWatching(const DefinitionNode *root, const string &path, bool init_diff) { + DefinitionNode *node = root->findByPath(path); + if (node) { + node->addWatcher(this, init_diff); + } else { + Logs::warning() << "[Definition] Node not found for watching : " << path << endl; + } +} diff --git a/src/definition/DefinitionWatcher.h b/src/definition/DefinitionWatcher.h index 84ee460..a0fb8db 100644 --- a/src/definition/DefinitionWatcher.h +++ b/src/definition/DefinitionWatcher.h @@ -19,6 +19,12 @@ class DEFINITIONSHARED_EXPORT DefinitionWatcher { * Abstract method called when a node changed. */ virtual void nodeChanged(const DefinitionNode *node, const DefinitionDiff *diff) = 0; + + protected: + /** + * Start watching a path in a definition tree. + */ + void startWatching(const DefinitionNode *root, const std::string &path, bool init_diff = true); }; } } diff --git a/src/render/opengl/OpenGLSkybox.cpp b/src/render/opengl/OpenGLSkybox.cpp index 5070a96..bde8335 100644 --- a/src/render/opengl/OpenGLSkybox.cpp +++ b/src/render/opengl/OpenGLSkybox.cpp @@ -10,6 +10,11 @@ #include "AtmosphereRenderer.h" #include "AtmosphereModelBruneton.h" #include "FloatNode.h" +#include "Logs.h" + +static const std::string path_daytime = "/atmosphere/daytime"; +static const std::string path_humidity = "/atmosphere/humidity"; +static const std::string path_sun_radius = "/atmosphere/sun_radius"; OpenGLSkybox::OpenGLSkybox(OpenGLRenderer *renderer) : OpenGLPart(renderer, "skybox") { program = createShader("skybox"); @@ -50,12 +55,15 @@ OpenGLSkybox::~OpenGLSkybox() { void OpenGLSkybox::initialize() { // Watch for definition changes - renderer->getScenery()->getAtmosphere()->propDayTime()->addWatcher(this, true); - renderer->getScenery()->getAtmosphere()->propHumidity()->addWatcher(this, true); - renderer->getScenery()->getAtmosphere()->propSunRadius()->addWatcher(this, true); + Scenery *scenery = renderer->getScenery(); + startWatching(scenery, path_daytime); + startWatching(scenery, path_humidity); + startWatching(scenery, path_sun_radius); } void OpenGLSkybox::update() { + Logs::debug() << "[OpenGL] Updating atmosphere textures" << endl; + SoftwareBrunetonAtmosphereRenderer *bruneton = (SoftwareBrunetonAtmosphereRenderer *)renderer->getAtmosphereRenderer(); renderer->getSharedState()->set("transmittanceTexture", bruneton->getModel()->getTextureTransmittance()); @@ -67,19 +75,20 @@ void OpenGLSkybox::render() { } void OpenGLSkybox::nodeChanged(const DefinitionNode *node, const DefinitionDiff *) { - if (node->getPath() == "/atmosphere/daytime") { + OpenGLSharedState *state = renderer->getSharedState(); + AtmosphereDefinition *newdef = renderer->getScenery()->getAtmosphere(); + + if (node->getPath() == path_daytime) { Vector3 sun_direction = renderer->getAtmosphereRenderer()->getSunDirection(false); - renderer->getSharedState()->set("sunDirection", sun_direction); + state->set("sunDirection", sun_direction); - Color sun_color = renderer->getScenery()->getAtmosphere()->sun_color; - renderer->getSharedState()->set("sunColor", sun_color); + Color sun_color = newdef->sun_color; + state->set("sunColor", sun_color); - renderer->getSharedState()->set("dayTime", renderer->getScenery()->getAtmosphere()->propDayTime()->getValue()); - } else if (node->getPath() == "/atmosphere/humidity") { - renderer->getSharedState()->set("atmosphereHumidity", - renderer->getScenery()->getAtmosphere()->propHumidity()->getValue()); - } else if (node->getPath() == "/atmosphere/sun_radius") { - renderer->getSharedState()->set("sunRadius", - renderer->getScenery()->getAtmosphere()->propSunRadius()->getValue()); + state->set("dayTime", newdef->propDayTime()->getValue()); + } else if (node->getPath() == path_humidity) { + state->set("atmosphereHumidity", newdef->propHumidity()->getValue()); + } else if (node->getPath() == path_sun_radius) { + state->set("sunRadius", newdef->propSunRadius()->getValue()); } } diff --git a/src/render/opengl/OpenGLWater.cpp b/src/render/opengl/OpenGLWater.cpp index 160c291..325791f 100644 --- a/src/render/opengl/OpenGLWater.cpp +++ b/src/render/opengl/OpenGLWater.cpp @@ -12,6 +12,11 @@ #include "FloatNode.h" #include "FloatDiff.h" #include "IntNode.h" +#include "Logs.h" + +static const std::string path_height = "/terrain/water_height"; +static const std::string path_reflection = "/water/reflection"; +static const std::string path_model = "/water/model"; OpenGLWater::OpenGLWater(OpenGLRenderer *renderer) : OpenGLPart(renderer, "water") { enabled = true; @@ -38,19 +43,23 @@ OpenGLWater::~OpenGLWater() { void OpenGLWater::initialize() { // Watch for definition changes - renderer->getScenery()->getTerrain()->propWaterHeight()->addWatcher(this, true); - renderer->getScenery()->getWater()->propReflection()->addWatcher(this, true); - renderer->getScenery()->getWater()->propModel()->addWatcher(this, false); + Scenery *scenery = renderer->getScenery(); + startWatching(scenery, path_height); + startWatching(scenery, path_reflection); + startWatching(scenery, path_model, false); } void OpenGLWater::update() { - WaterDefinition *water = renderer->getScenery()->getWater(); - renderer->getSharedState()->set("waterMaterialColor", *water->material->base); - renderer->getSharedState()->set("waterMaterialReflection", water->material->reflection); - renderer->getSharedState()->set("waterMaterialShininess", water->material->shininess); - renderer->getSharedState()->set("waterMaterialHardness", water->material->hardness); + OpenGLSharedState *state = renderer->getSharedState(); - renderer->getSharedState()->set("simplexSampler", NoiseFunctionSimplex::getNormalTexture(), true, true); + WaterDefinition *water = renderer->getScenery()->getWater(); + state->set("waterMaterialColor", *water->material->base); + state->set("waterMaterialReflection", water->material->reflection); + state->set("waterMaterialShininess", water->material->shininess); + state->set("waterMaterialHardness", water->material->hardness); + + Logs::debug() << "[OpenGL] Updating simplex texture" << endl; + state->set("simplexSampler", NoiseFunctionSimplex::getNormalTexture(), true, true); } void OpenGLWater::render() { @@ -60,12 +69,13 @@ void OpenGLWater::render() { } void OpenGLWater::nodeChanged(const DefinitionNode *node, const DefinitionDiff *) { - if (node->getPath() == "/terrain/water_height") { - renderer->getSharedState()->set("waterOffset", renderer->getScenery()->getTerrain()->getWaterOffset()); - } else if (node->getPath() == "/water/reflection") { - renderer->getSharedState()->set("waterReflection", - renderer->getScenery()->getWater()->propReflection()->getValue()); - } else if (node->getPath() == "/water/model") { + OpenGLSharedState *state = renderer->getSharedState(); + + if (node->getPath() == path_height) { + state->set("waterOffset", renderer->getScenery()->getTerrain()->getWaterOffset()); + } else if (node->getPath() == path_reflection) { + state->set("waterReflection", renderer->getScenery()->getWater()->propReflection()->getValue()); + } else if (node->getPath() == path_model) { update(); } }