2013-12-08 19:54:34 +00:00
|
|
|
#include "WaterRenderer.h"
|
2013-02-27 16:38:27 +00:00
|
|
|
|
2013-12-08 19:54:34 +00:00
|
|
|
#include "SoftwareRenderer.h"
|
|
|
|
#include "TerrainRenderer.h"
|
2013-11-13 19:07:35 +00:00
|
|
|
#include "WaterDefinition.h"
|
2013-11-03 14:46:39 +00:00
|
|
|
#include "NoiseGenerator.h"
|
2013-12-08 19:54:34 +00:00
|
|
|
#include "LightComponent.h"
|
|
|
|
#include "Scenery.h"
|
|
|
|
#include "SurfaceMaterial.h"
|
2014-01-21 20:51:11 +00:00
|
|
|
#include "NoiseFunctionSimplex.h"
|
2013-02-27 16:38:27 +00:00
|
|
|
|
2013-12-08 19:54:34 +00:00
|
|
|
WaterRenderer::WaterRenderer(SoftwareRenderer* parent):
|
|
|
|
parent(parent)
|
2013-02-27 16:38:27 +00:00
|
|
|
{
|
2014-01-21 20:51:11 +00:00
|
|
|
noise = new NoiseFunctionSimplex();
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 19:54:34 +00:00
|
|
|
WaterRenderer::~WaterRenderer()
|
2013-02-27 16:38:27 +00:00
|
|
|
{
|
2014-01-21 20:51:11 +00:00
|
|
|
delete noise;
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-17 22:45:09 +00:00
|
|
|
void WaterRenderer::update()
|
|
|
|
{
|
2014-01-21 20:51:11 +00:00
|
|
|
WaterDefinition* definition = parent->getScenery()->getWater();
|
|
|
|
noise->setState(*definition->noise_state);
|
|
|
|
noise->setScaling(definition->scaling * 0.3, definition->waves_height * 0.05);
|
|
|
|
noise->setStep(0.3);
|
2013-12-17 22:45:09 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 20:51:11 +00:00
|
|
|
static inline double _getHeight(FractalNoise* noise, double x, double z)
|
2013-02-27 16:38:27 +00:00
|
|
|
{
|
2014-01-21 20:51:11 +00:00
|
|
|
return noise->get2d(0.00001, x, z);
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 20:51:11 +00:00
|
|
|
static inline Vector3 _getNormal(FractalNoise* noise, Vector3 base, double detail)
|
2013-02-27 16:38:27 +00:00
|
|
|
{
|
|
|
|
Vector3 back, right;
|
|
|
|
double x, z;
|
|
|
|
|
|
|
|
x = base.x;
|
|
|
|
z = base.z;
|
|
|
|
|
|
|
|
back.x = x;
|
2014-01-21 20:51:11 +00:00
|
|
|
back.y = _getHeight(noise, x, z + detail);
|
2013-02-27 16:38:27 +00:00
|
|
|
back.z = z + detail;
|
2013-12-11 10:32:10 +00:00
|
|
|
back = back.sub(base);
|
2013-02-27 16:38:27 +00:00
|
|
|
|
|
|
|
right.x = x + detail;
|
2014-01-21 20:51:11 +00:00
|
|
|
right.y = _getHeight(noise, x + detail, z);
|
2013-02-27 16:38:27 +00:00
|
|
|
right.z = z;
|
2013-12-11 10:32:10 +00:00
|
|
|
right = right.sub(base);
|
2013-02-27 16:38:27 +00:00
|
|
|
|
2013-12-11 10:32:10 +00:00
|
|
|
return back.crossProduct(right).normalize();
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline Vector3 _reflectRay(Vector3 incoming, Vector3 normal)
|
|
|
|
{
|
|
|
|
double c;
|
|
|
|
|
2013-12-11 10:32:10 +00:00
|
|
|
c = normal.dotProduct(incoming.scale(-1.0));
|
|
|
|
return incoming.add(normal.scale(2.0 * c));
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline Vector3 _refractRay(Vector3 incoming, Vector3 normal)
|
|
|
|
{
|
|
|
|
double c1, c2, f;
|
|
|
|
|
|
|
|
f = 1.0 / 1.33;
|
2013-12-11 10:32:10 +00:00
|
|
|
c1 = normal.dotProduct(incoming.scale(-1.0));
|
2013-02-27 16:38:27 +00:00
|
|
|
c2 = sqrt(1.0 - pow(f, 2.0) * (1.0 - pow(c1, 2.0)));
|
|
|
|
if (c1 >= 0.0)
|
|
|
|
{
|
2013-12-11 10:32:10 +00:00
|
|
|
return incoming.scale(f).add(normal.scale(f * c1 - c2));
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-11 10:32:10 +00:00
|
|
|
return incoming.scale(f).add(normal.scale(c2 - f * c1));
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-21 20:51:11 +00:00
|
|
|
static inline Color _getFoamMask(SoftwareRenderer* renderer, WaterDefinition* definition, FractalNoise* noise, Vector3 location, Vector3 normal, double detail)
|
2013-02-27 16:38:27 +00:00
|
|
|
{
|
2013-03-07 14:07:12 +00:00
|
|
|
Color result;
|
2013-02-27 16:38:27 +00:00
|
|
|
double foam_factor, normal_diff, location_offset;
|
|
|
|
|
|
|
|
location_offset = 2.0 * detail;
|
|
|
|
|
|
|
|
foam_factor = 0.0;
|
|
|
|
location.x += location_offset;
|
2014-01-21 20:51:11 +00:00
|
|
|
normal_diff = 1.0 - normal.dotProduct(_getNormal(noise, location, detail));
|
2013-02-27 16:38:27 +00:00
|
|
|
if (normal_diff > foam_factor)
|
|
|
|
{
|
|
|
|
foam_factor = normal_diff;
|
|
|
|
}
|
|
|
|
location.x -= location_offset * 2.0;
|
2014-01-21 20:51:11 +00:00
|
|
|
normal_diff = 1.0 - normal.dotProduct(_getNormal(noise, location, detail));
|
2013-02-27 16:38:27 +00:00
|
|
|
if (normal_diff > foam_factor)
|
|
|
|
{
|
|
|
|
foam_factor = normal_diff;
|
|
|
|
}
|
|
|
|
location.x += location_offset;
|
|
|
|
location.z -= location_offset;
|
2014-01-21 20:51:11 +00:00
|
|
|
normal_diff = 1.0 - normal.dotProduct(_getNormal(noise, location, detail));
|
2013-02-27 16:38:27 +00:00
|
|
|
if (normal_diff > foam_factor)
|
|
|
|
{
|
|
|
|
foam_factor = normal_diff;
|
|
|
|
}
|
|
|
|
location.z += location_offset * 2.0;
|
2014-01-21 20:51:11 +00:00
|
|
|
normal_diff = 1.0 - normal.dotProduct(_getNormal(noise, location, detail));
|
2013-02-27 16:38:27 +00:00
|
|
|
if (normal_diff > foam_factor)
|
|
|
|
{
|
|
|
|
foam_factor = normal_diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
foam_factor *= 10.0;
|
|
|
|
if (foam_factor > 1.0)
|
|
|
|
{
|
|
|
|
foam_factor = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foam_factor <= 1.0 - definition->foam_coverage)
|
|
|
|
{
|
2013-03-07 14:07:12 +00:00
|
|
|
return COLOR_TRANSPARENT;
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
foam_factor = (foam_factor - (1.0 - definition->foam_coverage)) * definition->foam_coverage;
|
|
|
|
|
2013-03-07 14:07:12 +00:00
|
|
|
/* TODO Re-use base lighting status */
|
2013-12-08 17:36:39 +00:00
|
|
|
result = renderer->applyLightingToSurface(location, normal, *definition->foam_material);
|
2013-03-07 14:55:37 +00:00
|
|
|
result.r *= 2.0;
|
|
|
|
result.g *= 2.0;
|
|
|
|
result.b *= 2.0;
|
2013-02-27 16:38:27 +00:00
|
|
|
|
|
|
|
/* TODO This should be configurable */
|
|
|
|
if (foam_factor > 0.2)
|
|
|
|
{
|
|
|
|
result.a = 0.8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.a = 0.8 * (foam_factor / 0.2);
|
|
|
|
}
|
2013-03-07 14:07:12 +00:00
|
|
|
|
|
|
|
return result;
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 19:54:34 +00:00
|
|
|
HeightInfo WaterRenderer::getHeightInfo()
|
2013-02-27 16:38:27 +00:00
|
|
|
{
|
|
|
|
HeightInfo info;
|
|
|
|
double noise_minvalue, noise_maxvalue;
|
|
|
|
|
2014-01-06 19:22:00 +00:00
|
|
|
info.base_height = 0.0;
|
2014-01-21 20:51:11 +00:00
|
|
|
// TODO
|
|
|
|
//noise->getRange(&noise_minvalue, &noise_maxvalue);
|
|
|
|
noise_minvalue = noise_maxvalue = 0.0;
|
2013-05-11 21:34:30 +00:00
|
|
|
info.min_height = info.base_height + noise_minvalue;
|
|
|
|
info.max_height = info.base_height + noise_maxvalue;
|
2013-02-27 16:38:27 +00:00
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2013-12-08 19:54:34 +00:00
|
|
|
double WaterRenderer::getHeight(double x, double z)
|
2013-02-27 16:38:27 +00:00
|
|
|
{
|
2014-01-21 20:51:11 +00:00
|
|
|
return _getHeight(noise, x, z);
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 19:54:34 +00:00
|
|
|
WaterRenderer::WaterResult WaterRenderer::getResult(double x, double z)
|
2013-02-27 16:38:27 +00:00
|
|
|
{
|
2013-12-08 19:54:34 +00:00
|
|
|
WaterDefinition* definition = parent->getScenery()->getWater();
|
2013-02-27 16:38:27 +00:00
|
|
|
WaterResult result;
|
|
|
|
RayCastingResult refracted;
|
2013-03-03 17:05:30 +00:00
|
|
|
Vector3 location, normal, look_direction;
|
2013-03-07 14:07:12 +00:00
|
|
|
Color color, foam;
|
2014-01-06 19:22:00 +00:00
|
|
|
double detail, depth;
|
2013-02-27 16:38:27 +00:00
|
|
|
|
|
|
|
location.x = x;
|
2014-01-21 20:51:11 +00:00
|
|
|
location.y = _getHeight(noise, x, z);
|
2013-02-27 16:38:27 +00:00
|
|
|
location.z = z;
|
|
|
|
result.location = location;
|
|
|
|
|
2013-12-09 10:59:57 +00:00
|
|
|
detail = parent->getPrecision(location) * 0.1;
|
2013-02-27 16:38:27 +00:00
|
|
|
if (detail < 0.00001)
|
|
|
|
{
|
|
|
|
detail = 0.00001;
|
|
|
|
}
|
|
|
|
|
2014-01-21 20:51:11 +00:00
|
|
|
normal = _getNormal(noise, location, detail);
|
2013-12-11 10:32:10 +00:00
|
|
|
look_direction = location.sub(parent->getCameraLocation(location)).normalize();
|
2013-03-03 17:05:30 +00:00
|
|
|
|
|
|
|
/* Reflection */
|
|
|
|
if (definition->reflection == 0.0)
|
|
|
|
{
|
|
|
|
result.reflected = COLOR_BLACK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-08 19:54:34 +00:00
|
|
|
result.reflected = parent->rayWalking(location, _reflectRay(look_direction, normal), 1, 0, 1, 1).hit_color;
|
2013-03-03 17:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Transparency/refraction */
|
|
|
|
if (definition->transparency == 0.0)
|
2013-02-27 16:38:27 +00:00
|
|
|
{
|
2013-03-03 17:05:30 +00:00
|
|
|
result.refracted = COLOR_BLACK;
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-13 19:07:35 +00:00
|
|
|
Color depth_color = *definition->depth_color;
|
2013-12-08 19:54:34 +00:00
|
|
|
refracted = parent->rayWalking(location, _refractRay(look_direction, normal), 1, 0, 1, 1);
|
2013-12-11 10:32:10 +00:00
|
|
|
depth = location.sub(refracted.hit_location).getNorm();
|
2013-12-11 09:24:35 +00:00
|
|
|
depth_color.limitPower(refracted.hit_color.getPower());
|
2013-03-03 17:05:30 +00:00
|
|
|
if (depth > definition->transparency_depth)
|
|
|
|
{
|
2013-03-09 11:06:39 +00:00
|
|
|
result.refracted = depth_color;
|
2013-03-03 17:05:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
depth /= definition->transparency_depth;
|
2013-03-09 11:06:39 +00:00
|
|
|
result.refracted.r = refracted.hit_color.r * (1.0 - depth) + depth_color.r * depth;
|
|
|
|
result.refracted.g = refracted.hit_color.g * (1.0 - depth) + depth_color.g * depth;
|
|
|
|
result.refracted.b = refracted.hit_color.b * (1.0 - depth) + depth_color.b * depth;
|
2013-03-03 17:05:30 +00:00
|
|
|
result.refracted.a = 1.0;
|
|
|
|
}
|
2013-02-27 16:38:27 +00:00
|
|
|
}
|
|
|
|
|
2013-03-03 17:05:30 +00:00
|
|
|
/* Lighting from environment */
|
2013-12-08 19:54:34 +00:00
|
|
|
color = parent->applyLightingToSurface(location, normal, *definition->material);
|
2013-02-27 16:38:27 +00:00
|
|
|
|
2013-03-03 17:05:30 +00:00
|
|
|
color.r += result.reflected.r * definition->reflection + result.refracted.r * definition->transparency;
|
|
|
|
color.g += result.reflected.g * definition->reflection + result.refracted.g * definition->transparency;
|
|
|
|
color.b += result.reflected.b * definition->reflection + result.refracted.b * definition->transparency;
|
2013-02-27 16:38:27 +00:00
|
|
|
|
2013-03-03 17:05:30 +00:00
|
|
|
/* Merge with foam */
|
2014-01-21 20:51:11 +00:00
|
|
|
foam = _getFoamMask(parent, definition, noise, location, normal, detail);
|
2013-12-11 09:24:35 +00:00
|
|
|
color.mask(foam);
|
2013-02-27 16:38:27 +00:00
|
|
|
|
2013-03-03 17:05:30 +00:00
|
|
|
/* Bring color to the camera */
|
2013-12-08 19:54:34 +00:00
|
|
|
color = parent->applyMediumTraversal(location, color);
|
2013-02-27 16:38:27 +00:00
|
|
|
|
2015-08-19 17:14:59 +00:00
|
|
|
result.base = *definition->material->base;
|
2013-02-27 16:38:27 +00:00
|
|
|
result.final = color;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2013-12-17 22:45:09 +00:00
|
|
|
|
|
|
|
bool WaterRenderer::applyLightFilter(LightComponent &light, const Vector3 &at)
|
|
|
|
{
|
|
|
|
WaterDefinition* definition = parent->getScenery()->getWater();
|
|
|
|
double factor;
|
|
|
|
|
2014-01-06 19:22:00 +00:00
|
|
|
if (at.y < 0.0)
|
2013-12-17 22:45:09 +00:00
|
|
|
{
|
|
|
|
if (light.direction.y <= -0.00001)
|
|
|
|
{
|
2014-01-06 19:22:00 +00:00
|
|
|
factor = -at.y / (-light.direction.y * definition->lighting_depth);
|
2013-12-17 22:45:09 +00:00
|
|
|
if (factor > 1.0)
|
|
|
|
{
|
|
|
|
factor = 1.0;
|
|
|
|
}
|
|
|
|
factor = 1.0 - factor;
|
|
|
|
|
|
|
|
light.color.r *= factor;
|
|
|
|
light.color.g *= factor;
|
|
|
|
light.color.b *= factor;
|
|
|
|
light.reflection *= factor;
|
|
|
|
|
2014-08-22 15:34:07 +00:00
|
|
|
return factor > 0.0;
|
2013-12-17 22:45:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
light.color = COLOR_BLACK;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|