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"
|
|
|
|
#include "WaterRenderer.h"
|
2013-12-24 10:48:06 +00:00
|
|
|
#include "Scenery.h"
|
|
|
|
#include "WaterDefinition.h"
|
|
|
|
#include "SurfaceMaterial.h"
|
2014-01-05 19:37:51 +00:00
|
|
|
#include "NoiseFunctionSimplex.h"
|
2015-08-17 20:55:30 +00:00
|
|
|
#include "FloatNode.h"
|
|
|
|
#include "FloatDiff.h"
|
2013-12-21 23:41:19 +00:00
|
|
|
|
|
|
|
OpenGLWater::OpenGLWater(OpenGLRenderer *renderer):
|
|
|
|
OpenGLPart(renderer)
|
|
|
|
{
|
|
|
|
vertices = new float[4 * 3];
|
2015-08-20 22:25:34 +00:00
|
|
|
enabled = true;
|
2013-12-21 23:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OpenGLWater::~OpenGLWater()
|
|
|
|
{
|
|
|
|
delete[] vertices;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLWater::initialize()
|
|
|
|
{
|
|
|
|
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");
|
2014-01-21 20:41:15 +00:00
|
|
|
program->addFragmentSource("noise");
|
2013-12-21 23:41:19 +00:00
|
|
|
program->addFragmentSource("water");
|
|
|
|
|
|
|
|
setVertex(0, -1.0f, 0.0f, -1.0f);
|
|
|
|
setVertex(1, -1.0f, 0.0f, 1.0f);
|
|
|
|
setVertex(2, 1.0f, 0.0f, -1.0f);
|
|
|
|
setVertex(3, 1.0f, 0.0f, 1.0f);
|
2015-08-17 20:55:30 +00:00
|
|
|
|
|
|
|
// Watch for definition changes
|
|
|
|
renderer->getScenery()->getTerrain()->propWaterHeight()->addWatcher(this, true);
|
2013-12-21 23:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLWater::update()
|
|
|
|
{
|
2015-08-19 17:14:59 +00:00
|
|
|
Color water_color = *renderer->getScenery()->getWater()->material->base;
|
2013-12-24 10:48:06 +00:00
|
|
|
renderer->getSharedState()->set("waterColor", water_color);
|
2014-01-05 19:37:51 +00:00
|
|
|
|
2014-01-05 21:20:15 +00:00
|
|
|
double water_reflection = renderer->getScenery()->getWater()->reflection;
|
|
|
|
renderer->getSharedState()->set("waterReflection", water_reflection);
|
|
|
|
|
2014-01-05 20:52:09 +00:00
|
|
|
renderer->getSharedState()->set("simplexSampler", NoiseFunctionSimplex::getNormalTexture(), true, true);
|
2013-12-21 23:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLWater::render()
|
|
|
|
{
|
2015-08-20 22:25:34 +00:00
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
program->drawTriangleStrip(vertices, 4);
|
|
|
|
}
|
2013-12-21 23:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLWater::setVertex(int i, float x, float y, float z)
|
|
|
|
{
|
|
|
|
vertices[i * 3] = x;
|
|
|
|
vertices[i * 3 + 1] = y;
|
|
|
|
vertices[i * 3 + 2] = z;
|
|
|
|
}
|
2015-08-17 20:55:30 +00:00
|
|
|
|
|
|
|
void OpenGLWater::nodeChanged(const DefinitionNode *node, const DefinitionDiff *)
|
|
|
|
{
|
|
|
|
if (node->getPath() == "/terrain/water_height")
|
|
|
|
{
|
|
|
|
renderer->getSharedState()->set("waterOffset", renderer->getScenery()->getTerrain()->getWaterOffset());
|
|
|
|
}
|
|
|
|
}
|
2015-08-20 22:25:34 +00:00
|
|
|
|
|
|
|
void OpenGLWater::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
this->enabled = enabled;
|
|
|
|
}
|