2013-12-21 23:41:19 +00:00
|
|
|
#include "OpenGLWater.h"
|
|
|
|
|
2013-12-22 14:04:33 +00:00
|
|
|
#include "OpenGLRenderer.h"
|
2013-12-21 23:41:19 +00:00
|
|
|
#include "OpenGLShaderProgram.h"
|
2013-12-22 14:04:33 +00:00
|
|
|
#include "OpenGLSharedState.h"
|
2015-12-07 22:32:55 +00:00
|
|
|
#include "OpenGLVertexArray.h"
|
2013-12-22 14:04:33 +00:00
|
|
|
#include "WaterRenderer.h"
|
2013-12-24 10:48:06 +00:00
|
|
|
#include "Scenery.h"
|
|
|
|
#include "WaterDefinition.h"
|
|
|
|
#include "SurfaceMaterial.h"
|
2015-08-17 20:55:30 +00:00
|
|
|
#include "FloatNode.h"
|
|
|
|
#include "FloatDiff.h"
|
2015-09-10 21:41:15 +00:00
|
|
|
#include "IntNode.h"
|
2015-12-11 00:39:47 +00:00
|
|
|
#include "Logs.h"
|
2013-12-21 23:41:19 +00:00
|
|
|
|
2015-12-13 16:16:26 +00:00
|
|
|
static const string path_height = "/terrain/water_height";
|
|
|
|
static const string path_reflection = "/water/reflection";
|
|
|
|
static const string path_model = "/water/model";
|
2013-12-21 23:41:19 +00:00
|
|
|
|
2015-12-11 00:12:55 +00:00
|
|
|
OpenGLWater::OpenGLWater(OpenGLRenderer *renderer) : OpenGLPart(renderer, "water") {
|
2015-08-20 22:25:34 +00:00
|
|
|
enabled = true;
|
2013-12-21 23:41:19 +00:00
|
|
|
|
|
|
|
program = createShader("water");
|
|
|
|
program->addVertexSource("water");
|
2014-11-21 10:40:47 +00:00
|
|
|
program->addFragmentSource("atmosphere");
|
2013-12-22 17:47:24 +00:00
|
|
|
program->addFragmentSource("tonemapping");
|
2013-12-24 14:00:32 +00:00
|
|
|
program->addFragmentSource("fadeout");
|
2015-09-06 23:15:59 +00:00
|
|
|
program->addFragmentSource("ui");
|
2014-01-21 20:41:15 +00:00
|
|
|
program->addFragmentSource("noise");
|
2013-12-21 23:41:19 +00:00
|
|
|
program->addFragmentSource("water");
|
|
|
|
|
2015-12-07 22:32:55 +00:00
|
|
|
vertices = createVertexArray(false, true);
|
|
|
|
vertices->setVertexCount(4);
|
|
|
|
vertices->set(0, Vector3(-1.0f, 0.0f, -1.0f));
|
|
|
|
vertices->set(1, Vector3(-1.0f, 0.0f, 1.0f));
|
|
|
|
vertices->set(2, Vector3(1.0f, 0.0f, -1.0f));
|
|
|
|
vertices->set(3, Vector3(1.0f, 0.0f, 1.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenGLWater::~OpenGLWater() {
|
|
|
|
}
|
2015-08-17 20:55:30 +00:00
|
|
|
|
2015-12-07 22:32:55 +00:00
|
|
|
void OpenGLWater::initialize() {
|
2015-08-17 20:55:30 +00:00
|
|
|
// Watch for definition changes
|
2015-12-11 00:39:47 +00:00
|
|
|
Scenery *scenery = renderer->getScenery();
|
|
|
|
startWatching(scenery, path_height);
|
|
|
|
startWatching(scenery, path_reflection);
|
|
|
|
startWatching(scenery, path_model, false);
|
2013-12-21 23:41:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLWater::update() {
|
2015-12-11 00:39:47 +00:00
|
|
|
OpenGLSharedState *state = renderer->getSharedState();
|
|
|
|
|
2015-09-10 21:41:15 +00:00
|
|
|
WaterDefinition *water = renderer->getScenery()->getWater();
|
2015-12-11 00:39:47 +00:00
|
|
|
state->set("waterMaterialColor", *water->material->base);
|
|
|
|
state->set("waterMaterialReflection", water->material->reflection);
|
|
|
|
state->set("waterMaterialShininess", water->material->shininess);
|
|
|
|
state->set("waterMaterialHardness", water->material->hardness);
|
2014-01-05 19:37:51 +00:00
|
|
|
|
2015-12-24 00:21:12 +00:00
|
|
|
state->set("waterNoise", renderer->getWaterRenderer()->getNoise());
|
2013-12-21 23:41:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLWater::render() {
|
|
|
|
if (enabled) {
|
2015-12-07 22:32:55 +00:00
|
|
|
program->draw(vertices);
|
2015-08-20 22:25:34 +00:00
|
|
|
}
|
2013-12-21 23:41:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLWater::nodeChanged(const DefinitionNode *node, const DefinitionDiff *) {
|
2015-12-11 00:39:47 +00:00
|
|
|
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) {
|
2015-09-10 21:41:15 +00:00
|
|
|
update();
|
|
|
|
}
|
2015-08-17 20:55:30 +00:00
|
|
|
}
|
2015-08-20 22:25:34 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLWater::setEnabled(bool enabled) {
|
2015-08-20 22:25:34 +00:00
|
|
|
this->enabled = enabled;
|
|
|
|
}
|