paysages3d/src/render/opengl/OpenGLWater.cpp

76 lines
2.6 KiB
C++
Raw Normal View History

2013-12-21 23:41:19 +00:00
#include "OpenGLWater.h"
#include "OpenGLRenderer.h"
2013-12-21 23:41:19 +00:00
#include "OpenGLShaderProgram.h"
#include "OpenGLSharedState.h"
2015-12-07 22:32:55 +00:00
#include "OpenGLVertexArray.h"
#include "WaterRenderer.h"
2013-12-24 10:48:06 +00:00
#include "Scenery.h"
#include "WaterDefinition.h"
#include "SurfaceMaterial.h"
#include "NoiseFunctionSimplex.h"
#include "FloatNode.h"
#include "FloatDiff.h"
#include "IntNode.h"
2013-12-21 23:41:19 +00:00
OpenGLWater::OpenGLWater(OpenGLRenderer *renderer) : OpenGLPart(renderer) {
2015-08-20 22:25:34 +00:00
enabled = true;
2013-12-21 23:41:19 +00:00
program = createShader("water");
program->addVertexSource("water");
program->addFragmentSource("atmosphere");
program->addFragmentSource("tonemapping");
2013-12-24 14:00:32 +00:00
program->addFragmentSource("fadeout");
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-12-07 22:32:55 +00:00
void OpenGLWater::initialize() {
// Watch for definition changes
renderer->getScenery()->getTerrain()->propWaterHeight()->addWatcher(this, true);
2015-09-07 23:00:34 +00:00
renderer->getScenery()->getWater()->propReflection()->addWatcher(this, true);
renderer->getScenery()->getWater()->propModel()->addWatcher(this, false);
2013-12-21 23:41:19 +00:00
}
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);
renderer->getSharedState()->set("simplexSampler", NoiseFunctionSimplex::getNormalTexture(), true, true);
2013-12-21 23:41:19 +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
}
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") {
update();
}
}
2015-08-20 22:25:34 +00:00
void OpenGLWater::setEnabled(bool enabled) {
2015-08-20 22:25:34 +00:00
this->enabled = enabled;
}