Added DefinitionWatcher.startWatching

This commit is contained in:
Michaël Lemaire 2015-12-11 01:39:47 +01:00
parent 9cece93ec1
commit b54bd65df9
4 changed files with 66 additions and 29 deletions

View file

@ -1,4 +1,16 @@
#include "DefinitionWatcher.h" #include "DefinitionWatcher.h"
#include "DefinitionNode.h"
#include "Logs.h"
DefinitionWatcher::DefinitionWatcher() { 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;
}
}

View file

@ -19,6 +19,12 @@ class DEFINITIONSHARED_EXPORT DefinitionWatcher {
* Abstract method called when a node changed. * Abstract method called when a node changed.
*/ */
virtual void nodeChanged(const DefinitionNode *node, const DefinitionDiff *diff) = 0; 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);
}; };
} }
} }

View file

@ -10,6 +10,11 @@
#include "AtmosphereRenderer.h" #include "AtmosphereRenderer.h"
#include "AtmosphereModelBruneton.h" #include "AtmosphereModelBruneton.h"
#include "FloatNode.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") { OpenGLSkybox::OpenGLSkybox(OpenGLRenderer *renderer) : OpenGLPart(renderer, "skybox") {
program = createShader("skybox"); program = createShader("skybox");
@ -50,12 +55,15 @@ OpenGLSkybox::~OpenGLSkybox() {
void OpenGLSkybox::initialize() { void OpenGLSkybox::initialize() {
// Watch for definition changes // Watch for definition changes
renderer->getScenery()->getAtmosphere()->propDayTime()->addWatcher(this, true); Scenery *scenery = renderer->getScenery();
renderer->getScenery()->getAtmosphere()->propHumidity()->addWatcher(this, true); startWatching(scenery, path_daytime);
renderer->getScenery()->getAtmosphere()->propSunRadius()->addWatcher(this, true); startWatching(scenery, path_humidity);
startWatching(scenery, path_sun_radius);
} }
void OpenGLSkybox::update() { void OpenGLSkybox::update() {
Logs::debug() << "[OpenGL] Updating atmosphere textures" << endl;
SoftwareBrunetonAtmosphereRenderer *bruneton = SoftwareBrunetonAtmosphereRenderer *bruneton =
(SoftwareBrunetonAtmosphereRenderer *)renderer->getAtmosphereRenderer(); (SoftwareBrunetonAtmosphereRenderer *)renderer->getAtmosphereRenderer();
renderer->getSharedState()->set("transmittanceTexture", bruneton->getModel()->getTextureTransmittance()); renderer->getSharedState()->set("transmittanceTexture", bruneton->getModel()->getTextureTransmittance());
@ -67,19 +75,20 @@ void OpenGLSkybox::render() {
} }
void OpenGLSkybox::nodeChanged(const DefinitionNode *node, const DefinitionDiff *) { 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); 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; Color sun_color = newdef->sun_color;
renderer->getSharedState()->set("sunColor", sun_color); state->set("sunColor", sun_color);
renderer->getSharedState()->set("dayTime", renderer->getScenery()->getAtmosphere()->propDayTime()->getValue()); state->set("dayTime", newdef->propDayTime()->getValue());
} else if (node->getPath() == "/atmosphere/humidity") { } else if (node->getPath() == path_humidity) {
renderer->getSharedState()->set("atmosphereHumidity", state->set("atmosphereHumidity", newdef->propHumidity()->getValue());
renderer->getScenery()->getAtmosphere()->propHumidity()->getValue()); } else if (node->getPath() == path_sun_radius) {
} else if (node->getPath() == "/atmosphere/sun_radius") { state->set("sunRadius", newdef->propSunRadius()->getValue());
renderer->getSharedState()->set("sunRadius",
renderer->getScenery()->getAtmosphere()->propSunRadius()->getValue());
} }
} }

View file

@ -12,6 +12,11 @@
#include "FloatNode.h" #include "FloatNode.h"
#include "FloatDiff.h" #include "FloatDiff.h"
#include "IntNode.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") { OpenGLWater::OpenGLWater(OpenGLRenderer *renderer) : OpenGLPart(renderer, "water") {
enabled = true; enabled = true;
@ -38,19 +43,23 @@ OpenGLWater::~OpenGLWater() {
void OpenGLWater::initialize() { void OpenGLWater::initialize() {
// Watch for definition changes // Watch for definition changes
renderer->getScenery()->getTerrain()->propWaterHeight()->addWatcher(this, true); Scenery *scenery = renderer->getScenery();
renderer->getScenery()->getWater()->propReflection()->addWatcher(this, true); startWatching(scenery, path_height);
renderer->getScenery()->getWater()->propModel()->addWatcher(this, false); startWatching(scenery, path_reflection);
startWatching(scenery, path_model, false);
} }
void OpenGLWater::update() { void OpenGLWater::update() {
WaterDefinition *water = renderer->getScenery()->getWater(); OpenGLSharedState *state = renderer->getSharedState();
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);
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() { void OpenGLWater::render() {
@ -60,12 +69,13 @@ void OpenGLWater::render() {
} }
void OpenGLWater::nodeChanged(const DefinitionNode *node, const DefinitionDiff *) { void OpenGLWater::nodeChanged(const DefinitionNode *node, const DefinitionDiff *) {
if (node->getPath() == "/terrain/water_height") { OpenGLSharedState *state = renderer->getSharedState();
renderer->getSharedState()->set("waterOffset", renderer->getScenery()->getTerrain()->getWaterOffset());
} else if (node->getPath() == "/water/reflection") { if (node->getPath() == path_height) {
renderer->getSharedState()->set("waterReflection", state->set("waterOffset", renderer->getScenery()->getTerrain()->getWaterOffset());
renderer->getScenery()->getWater()->propReflection()->getValue()); } else if (node->getPath() == path_reflection) {
} else if (node->getPath() == "/water/model") { state->set("waterReflection", renderer->getScenery()->getWater()->propReflection()->getValue());
} else if (node->getPath() == path_model) {
update(); update();
} }
} }