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 "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;
}
}

View file

@ -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);
};
}
}

View file

@ -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());
}
}

View file

@ -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();
}
}