Replaced NoiseGenerator by NoiseState in WaterDefinition

This commit is contained in:
Michaël Lemaire 2014-01-21 21:51:11 +01:00
parent e79692bb5d
commit 3e72fc7bf8
8 changed files with 53 additions and 39 deletions

View file

@ -10,6 +10,10 @@ FractalNoise::FractalNoise()
ridge = 0.0;
}
FractalNoise::~FractalNoise()
{
}
void FractalNoise::setScaling(double scaling, double height)
{
this->scaling = scaling < 0.00000001 ? 0.0 : 1.0 / scaling;

View file

@ -15,6 +15,7 @@ class BASICSSHARED_EXPORT FractalNoise
{
public:
FractalNoise();
virtual ~FractalNoise();
void setScaling(double scaling, double height=1.0);
void setStep(double scaling_factor, double height_factor=1.0);

View file

@ -480,6 +480,15 @@ double noiseSimplexGet4DValue(double x, double y, double z, double w)
return 13.5 * (n0 + n1 + n2 + n3 + n4) + 0.5;
}
double NoiseFunctionSimplex::getBase2d(double x, double y) const
{
return noiseSimplexGet2DValue(x, y);
}
double NoiseFunctionSimplex::getBase3d(double x, double y, double z) const
{
return noiseSimplexGet3DValue(x, y, z);
}
static Texture2D *_valueTexture = NULL;

View file

@ -3,11 +3,16 @@
#include "basics_global.h"
#include "FractalNoise.h"
namespace paysages {
namespace basics {
class NoiseFunctionSimplex
class NoiseFunctionSimplex:public FractalNoise
{
virtual double getBase2d(double x, double y) const override;
virtual double getBase3d(double x, double y, double z) const override;
public:
static const Texture2D *getValueTexture();
static const Texture2D *getNormalTexture();

View file

@ -1,7 +1,7 @@
#include "WaterDefinition.h"
#include "PackStream.h"
#include "NoiseGenerator.h"
#include "NoiseState.h"
#include "Color.h"
#include "SurfaceMaterial.h"
@ -11,7 +11,7 @@ WaterDefinition::WaterDefinition(BaseDefinition* parent):
material = new SurfaceMaterial;
depth_color = new Color;
foam_material = new SurfaceMaterial;
_waves_noise = new NoiseGenerator;
noise_state = new NoiseState();
transparency_depth = 0.0;
transparency = 0.0;
@ -29,7 +29,7 @@ WaterDefinition::~WaterDefinition()
delete material;
delete depth_color;
delete foam_material;
delete _waves_noise;
delete noise_state;
}
void WaterDefinition::save(PackStream* stream) const
@ -51,7 +51,7 @@ void WaterDefinition::save(PackStream* stream) const
stream->write(&foam_coverage);
foam_material->save(stream);
_waves_noise->save(stream);
noise_state->save(stream);
}
void WaterDefinition::load(PackStream* stream)
@ -73,7 +73,7 @@ void WaterDefinition::load(PackStream* stream)
stream->read(&foam_coverage);
foam_material->load(stream);
_waves_noise->load(stream);
noise_state->load(stream);
validate();
}
@ -95,34 +95,20 @@ void WaterDefinition::copy(BaseDefinition* _destination) const
destination->turbulence = turbulence;
destination->foam_coverage = foam_coverage;
*destination->foam_material = *foam_material;
_waves_noise->copy(destination->_waves_noise);
noise_state->copy(destination->noise_state);
}
void WaterDefinition::validate()
{
BaseDefinition::validate();
double scaling = this->scaling * 0.3;
_waves_noise->clearLevels();
if (waves_height > 0.0)
{
_waves_noise->addLevelsSimple(2, scaling, -waves_height * scaling * 0.015, waves_height * scaling * 0.015, 0.5);
}
if (detail_height > 0.0)
{
_waves_noise->addLevelsSimple(3, scaling * 0.1, -detail_height * scaling * 0.015, detail_height * scaling * 0.015, 0.5);
}
_waves_noise->setFunctionParams(NoiseGenerator::NOISE_FUNCTION_SIMPLEX, -turbulence, 0.0);
_waves_noise->validate();
material->validate();
foam_material->validate();
}
void WaterDefinition::applyPreset(WaterPreset preset)
{
_waves_noise->randomizeOffsets();
noise_state->randomizeOffsets();
if (preset == WATER_PRESET_LAKE)
{

View file

@ -44,7 +44,7 @@ public:
double foam_coverage;
SurfaceMaterial* foam_material;
NoiseGenerator* _waves_noise;
NoiseState* noise_state;
};
}

View file

@ -7,26 +7,33 @@
#include "LightComponent.h"
#include "Scenery.h"
#include "SurfaceMaterial.h"
#include "NoiseFunctionSimplex.h"
WaterRenderer::WaterRenderer(SoftwareRenderer* parent):
parent(parent)
{
noise = new NoiseFunctionSimplex();
}
WaterRenderer::~WaterRenderer()
{
delete noise;
}
void WaterRenderer::update()
{
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);
}
static inline double _getHeight(WaterDefinition* definition, double x, double z)
static inline double _getHeight(FractalNoise* noise, double x, double z)
{
return definition->_waves_noise->get2DTotal(x, z);
return noise->get2d(0.00001, x, z);
}
static inline Vector3 _getNormal(WaterDefinition* definition, Vector3 base, double detail)
static inline Vector3 _getNormal(FractalNoise* noise, Vector3 base, double detail)
{
Vector3 back, right;
double x, z;
@ -35,12 +42,12 @@ static inline Vector3 _getNormal(WaterDefinition* definition, Vector3 base, doub
z = base.z;
back.x = x;
back.y = _getHeight(definition, x, z + detail);
back.y = _getHeight(noise, x, z + detail);
back.z = z + detail;
back = back.sub(base);
right.x = x + detail;
right.y = _getHeight(definition, x + detail, z);
right.y = _getHeight(noise, x + detail, z);
right.z = z;
right = right.sub(base);
@ -72,7 +79,7 @@ static inline Vector3 _refractRay(Vector3 incoming, Vector3 normal)
}
}
static inline Color _getFoamMask(SoftwareRenderer* renderer, WaterDefinition* definition, Vector3 location, Vector3 normal, double detail)
static inline Color _getFoamMask(SoftwareRenderer* renderer, WaterDefinition* definition, FractalNoise* noise, Vector3 location, Vector3 normal, double detail)
{
Color result;
double foam_factor, normal_diff, location_offset;
@ -81,26 +88,26 @@ static inline Color _getFoamMask(SoftwareRenderer* renderer, WaterDefinition* de
foam_factor = 0.0;
location.x += location_offset;
normal_diff = 1.0 - normal.dotProduct(_getNormal(definition, location, detail));
normal_diff = 1.0 - normal.dotProduct(_getNormal(noise, location, detail));
if (normal_diff > foam_factor)
{
foam_factor = normal_diff;
}
location.x -= location_offset * 2.0;
normal_diff = 1.0 - normal.dotProduct(_getNormal(definition, location, detail));
normal_diff = 1.0 - normal.dotProduct(_getNormal(noise, location, detail));
if (normal_diff > foam_factor)
{
foam_factor = normal_diff;
}
location.x += location_offset;
location.z -= location_offset;
normal_diff = 1.0 - normal.dotProduct(_getNormal(definition, location, detail));
normal_diff = 1.0 - normal.dotProduct(_getNormal(noise, location, detail));
if (normal_diff > foam_factor)
{
foam_factor = normal_diff;
}
location.z += location_offset * 2.0;
normal_diff = 1.0 - normal.dotProduct(_getNormal(definition, location, detail));
normal_diff = 1.0 - normal.dotProduct(_getNormal(noise, location, detail));
if (normal_diff > foam_factor)
{
foam_factor = normal_diff;
@ -139,12 +146,13 @@ static inline Color _getFoamMask(SoftwareRenderer* renderer, WaterDefinition* de
HeightInfo WaterRenderer::getHeightInfo()
{
WaterDefinition* definition = parent->getScenery()->getWater();
HeightInfo info;
double noise_minvalue, noise_maxvalue;
info.base_height = 0.0;
definition->_waves_noise->getRange(&noise_minvalue, &noise_maxvalue);
// TODO
//noise->getRange(&noise_minvalue, &noise_maxvalue);
noise_minvalue = noise_maxvalue = 0.0;
info.min_height = info.base_height + noise_minvalue;
info.max_height = info.base_height + noise_maxvalue;
@ -153,7 +161,7 @@ HeightInfo WaterRenderer::getHeightInfo()
double WaterRenderer::getHeight(double x, double z)
{
return _getHeight(parent->getScenery()->getWater(), x, z);
return _getHeight(noise, x, z);
}
WaterRenderer::WaterResult WaterRenderer::getResult(double x, double z)
@ -166,7 +174,7 @@ WaterRenderer::WaterResult WaterRenderer::getResult(double x, double z)
double detail, depth;
location.x = x;
location.y = _getHeight(definition, x, z);
location.y = _getHeight(noise, x, z);
location.z = z;
result.location = location;
@ -176,7 +184,7 @@ WaterRenderer::WaterResult WaterRenderer::getResult(double x, double z)
detail = 0.00001;
}
normal = _getNormal(definition, location, detail);
normal = _getNormal(noise, location, detail);
look_direction = location.sub(parent->getCameraLocation(location)).normalize();
/* Reflection */
@ -222,7 +230,7 @@ WaterRenderer::WaterResult WaterRenderer::getResult(double x, double z)
color.b += result.reflected.b * definition->reflection + result.refracted.b * definition->transparency;
/* Merge with foam */
foam = _getFoamMask(parent, definition, location, normal, detail);
foam = _getFoamMask(parent, definition, noise, location, normal, detail);
color.mask(foam);
/* Bring color to the camera */

View file

@ -38,6 +38,7 @@ public:
private:
SoftwareRenderer* parent;
FractalNoise* noise;
};
}