Added FBM 3D & 4D and HybridMultiFractal 2D + cleaned code

this commit will change the scale of values produced by fbm2d. This will
probably not happen again. As a consequence, fbm values will always be
constrained between -1 and 1, but do not perfectly stick to that scale.
There is no easy solution, if the user wants the best dynamic between -1
and 1, he should adjust manually the value by multiplying by a gain slightly superior to 1.


Former-commit-id: ebdba9e9f4bbb972abe355c07ec9f8bce42329b9
This commit is contained in:
Remi Beges 2012-11-09 18:38:50 +01:00
parent 7bd6202389
commit 8f04f3e6a0
10 changed files with 270 additions and 28 deletions

View File

@ -29,7 +29,7 @@ class NAZARA_API NzComplexNoiseBase
float m_lacunarity; float m_lacunarity;
float m_hurst; float m_hurst;
float m_octaves; float m_octaves;
std::array<float, 30> exponent_array; std::array<float, 30> m_exponent_array;
float m_sum; float m_sum;
private: private:
bool m_parametersModified; bool m_parametersModified;

View File

@ -4,8 +4,8 @@
#pragma once #pragma once
#ifndef FBM2DNOISE_HPP #ifndef FBM2D_HPP
#define FBM2DNOISE_HPP #define FBM2D_HPP
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp> #include <Nazara/Noise/ComplexNoiseBase.hpp>
@ -25,4 +25,4 @@ class NAZARA_API NzFBM2D : public NzAbstract2DNoise, public NzComplexNoiseBase
nzNoises m_noiseType; nzNoises m_noiseType;
}; };
#endif // FBM2DNOISE_HPP #endif // FBM2D_HPP

View File

@ -0,0 +1,29 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef FBM3D_HPP
#define FBM3D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include <Nazara/Noise/Abstract3DNoise.hpp>
class NAZARA_API NzFBM3D : public NzAbstract3DNoise, public NzComplexNoiseBase
{
public:
NzFBM3D(nzNoises source, int seed);
float GetValue(float x, float y, float z, float resolution);
~NzFBM3D();
protected:
private:
NzAbstract3DNoise* m_source;
float m_value;
float m_remainder;
nzNoises m_noiseType;
};
#endif // FBM3D_HPP

View File

@ -0,0 +1,29 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef FBM4D_HPP
#define FBM4D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include <Nazara/Noise/Abstract4DNoise.hpp>
class NAZARA_API NzFBM4D : public NzAbstract4DNoise, public NzComplexNoiseBase
{
public:
NzFBM4D(nzNoises source, int seed);
float GetValue(float x, float y, float z, float w, float resolution);
~NzFBM4D();
protected:
private:
NzAbstract4DNoise* m_source;
float m_value;
float m_remainder;
nzNoises m_noiseType;
};
#endif // FBM4D_HPP

View File

@ -0,0 +1,29 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef HYBRIDMULTIFRACTAL2D_HPP
#define HYBRIDMULTIFRACTAL2D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include <Nazara/Noise/Abstract2DNoise.hpp>
class NAZARA_API NzHybridMultiFractal2D : public NzAbstract2DNoise, public NzComplexNoiseBase
{
public:
NzHybridMultiFractal2D(nzNoises source, int seed);
float GetValue(float x, float y, float resolution);
~NzHybridMultiFractal2D();
protected:
private:
NzAbstract2DNoise* m_source;
float m_value;
float m_remainder;
nzNoises m_noiseType;
};
#endif // HYBRIDMULTIFRACTAL2D_HPP

View File

@ -17,13 +17,13 @@ NzComplexNoiseBase::NzComplexNoiseBase()
for (int i(0) ; i < m_octaves; ++i) for (int i(0) ; i < m_octaves; ++i)
{ {
exponent_array[i] = 0; m_exponent_array[i] = 0;
} }
} }
const std::array<float, 30>& NzComplexNoiseBase::GetExponentArray() const const std::array<float, 30>& NzComplexNoiseBase::GetExponentArray() const
{ {
return exponent_array; return m_exponent_array;
} }
float NzComplexNoiseBase::GetLacunarity() const float NzComplexNoiseBase::GetLacunarity() const
@ -80,11 +80,10 @@ void NzComplexNoiseBase::RecomputeExponentArray()
for (int i(0) ; i < static_cast<int>(m_octaves) ; ++i) for (int i(0) ; i < static_cast<int>(m_octaves) ; ++i)
{ {
exponent_array[i] = std::pow( frequency, -m_hurst ); m_exponent_array[i] = std::pow( frequency, -m_hurst );
frequency *= m_lacunarity; frequency *= m_lacunarity;
//m_sum += 1.0f/exponent_array[i];//A tester m_sum += m_exponent_array[i];
m_sum += exponent_array[i];
} }
m_parametersModified = false; m_parametersModified = false;

View File

@ -32,31 +32,20 @@ float NzFBM2D::GetValue(float x, float y, float resolution)
m_value = 0.0; m_value = 0.0;
for (int i(0); i < this->m_octaves; ++i) for (int i(0); i < m_octaves; ++i)
{ {
m_value += m_source->GetValue(x,y,resolution) * this->exponent_array[i]; m_value += m_source->GetValue(x,y,resolution) * m_exponent_array[i];
resolution *= this->m_lacunarity; resolution *= m_lacunarity;
} }
//cout<<m_value<<endl;//"|"<<this->m_sum<<endl; m_remainder = m_octaves - static_cast<int>(m_octaves);
//m_remainder = this->m_octaves - static_cast<int>(this->m_octaves);
//if(!NzNumberEquals(remainder, static_cast<float>(0.0))) if(!NzNumberEquals(m_remainder, static_cast<float>(0.0)))
// m_value += remainder * Get2DSimplexNoiseValue(x,y,resolution) * exponent_array[(int)m_octaves-1]; m_value += m_remainder * m_source->GetValue(x,y,resolution) * m_exponent_array[static_cast<int>(m_octaves-1)];
//0.65 is an experimental value to make the noise stick closer to [-1 , 1] return m_value/this->m_sum;
return m_value / (this->m_sum * 0.65);
} }
NzFBM2D::~NzFBM2D() NzFBM2D::~NzFBM2D()
{ {
switch(m_noiseType) delete m_source;
{
case PERLIN:
delete dynamic_cast<NzPerlin2D*>(m_source);
break;
default:
delete dynamic_cast<NzSimplex2D*>(m_source);
break;
}
} }

View File

@ -0,0 +1,51 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/FBM3D.hpp>
#include <Nazara/Noise/Perlin3D.hpp>
#include <Nazara/Noise/Simplex3D.hpp>
#include <Nazara/Noise/Debug.hpp>
NzFBM3D::NzFBM3D(nzNoises source, int seed)
{
switch(source)
{
case PERLIN:
m_source = new NzPerlin3D();
break;
default:
m_source = new NzSimplex3D();
break;
}
m_source->SetNewSeed(seed);
m_source->ShufflePermutationTable();
m_noiseType = source;
}
float NzFBM3D::GetValue(float x, float y, float z, float resolution)
{
this->RecomputeExponentArray();
m_value = 0.0;
for (int i(0); i < m_octaves; ++i)
{
m_value += m_source->GetValue(x,y,z,resolution) * m_exponent_array[i];
resolution *= m_lacunarity;
}
m_remainder = m_octaves - static_cast<int>(m_octaves);
if(!NzNumberEquals(m_remainder, static_cast<float>(0.0)))
m_value += m_remainder * m_source->GetValue(x,y,z,resolution) * m_exponent_array[static_cast<int>(m_octaves-1)];
return m_value/this->m_sum;
}
NzFBM3D::~NzFBM3D()
{
delete m_source;
}

View File

@ -0,0 +1,51 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/FBM4D.hpp>
#include <Nazara/Noise/Perlin4D.hpp>
#include <Nazara/Noise/Simplex4D.hpp>
#include <Nazara/Noise/Debug.hpp>
NzFBM4D::NzFBM4D(nzNoises source, int seed)
{
switch(source)
{
case PERLIN:
m_source = new NzPerlin4D();
break;
default:
m_source = new NzSimplex4D();
break;
}
m_source->SetNewSeed(seed);
m_source->ShufflePermutationTable();
m_noiseType = source;
}
float NzFBM4D::GetValue(float x, float y, float z, float w, float resolution)
{
this->RecomputeExponentArray();
m_value = 0.0;
for (int i(0); i < m_octaves; ++i)
{
m_value += m_source->GetValue(x,y,z,w,resolution) * m_exponent_array[i];
resolution *= m_lacunarity;
}
m_remainder = m_octaves - static_cast<int>(m_octaves);
if(!NzNumberEquals(m_remainder, static_cast<float>(0.0)))
m_value += m_remainder * m_source->GetValue(x,y,z,w,resolution) * m_exponent_array[static_cast<int>(m_octaves-1)];
return m_value/this->m_sum;
}
NzFBM4D::~NzFBM4D()
{
delete m_source;
}

View File

@ -0,0 +1,65 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/HybridMultiFractal2D.hpp>
#include <Nazara/Noise/Perlin2D.hpp>
#include <Nazara/Noise/Simplex2D.hpp>
#include <Nazara/Noise/Debug.hpp>
NzHybridMultiFractal2D::NzHybridMultiFractal2D(nzNoises source, int seed)
{
switch(source)
{
case PERLIN:
m_source = new NzPerlin2D();
break;
default:
m_source = new NzSimplex2D();
break;
}
m_source->SetNewSeed(seed);
m_source->ShufflePermutationTable();
m_noiseType = source;
}
float NzHybridMultiFractal2D::GetValue(float x, float y, float resolution)
{
this->RecomputeExponentArray();
float offset = 1.0f;
m_value = (m_source->GetValue(x,y,resolution) + offset) * m_exponent_array[0];
float weight = m_value;
float signal;
resolution *= m_lacunarity;
for(int i(1) ; i < m_octaves; ++i)
{
if(weight > 1.0)
weight = 1.0;
signal = (m_source->GetValue(x,y,resolution) + offset) * m_exponent_array[i];
m_value += weight * signal;
weight *= signal;
resolution *= m_lacunarity;
}
m_remainder = m_octaves - static_cast<int>(m_octaves);
if(remainder != 0)
m_value += m_remainder * m_source->GetValue(x,y,resolution) * m_exponent_array[static_cast<int>(m_octaves-1)];
return m_value/this->m_sum;
}
NzHybridMultiFractal2D::~NzHybridMultiFractal2D()
{
delete m_source;
}