n/fractal: add musgrave's heterogenous terrain

This commit is contained in:
Danny Robson 2015-05-27 01:17:24 +10:00
parent 46a87c0b11
commit 980f5d5671
2 changed files with 81 additions and 0 deletions

View File

@ -26,6 +26,7 @@ using util::noise::fractal;
using util::noise::fbm;
using util::noise::rmf;
using util::noise::hmf;
using util::noise::hetero;
///////////////////////////////////////////////////////////////////////////////
@ -272,3 +273,58 @@ template struct util::noise::hmf<float, util::noise::gradient<float,util::lerp::
template struct util::noise::hmf<float, util::noise::gradient<float,util::lerp::quintic>>;
template struct util::noise::hmf<float, util::noise::value<float,util::lerp::linear>>;
template struct util::noise::hmf<float, util::noise::value<float,util::lerp::quintic>>;
//-----------------------------------------------------------------------------
template <typename T, typename B>
hetero<T,B>::hetero():
H (0.75f),
octaves (6),
frequency (0.1f),
lacunarity (2),
offset (0.7f),
amplitude (1),
gain (1)
{ ; }
//-----------------------------------------------------------------------------
template <typename T, typename B>
T
hetero<T,B>::operator() (T x, T y) const
{
T exponents[octaves];
for (size_t i = 0; i < octaves; ++i)
exponents[i] = std::pow (std::pow (lacunarity, float (i)), -H);
T result = 0;
T increment = 0;
x *= frequency;
y *= frequency;
result = basis (x, y) + offset;
x *= lacunarity;
y *= lacunarity;
for (size_t i = 0; i < octaves; ++i) {
increment = basis (x, y) + offset;
increment *= exponents[i];
increment *= result;
result += increment;
x *= lacunarity;
y *= lacunarity;
}
return result;
}
template struct util::noise::hetero<float, util::noise::cellular<float>>;
template struct util::noise::hetero<float, util::noise::gradient<float,util::lerp::linear>>;
template struct util::noise::hetero<float, util::noise::gradient<float,util::lerp::quintic>>;
template struct util::noise::hetero<float, util::noise::value<float,util::lerp::linear>>;
template struct util::noise::hetero<float, util::noise::value<float,util::lerp::quintic>>;

View File

@ -135,6 +135,31 @@ namespace util {
virtual T operator() (T x, T y) const override;
};
///////////////////////////////////////////////////////////////////////
/// Heterogeneous procedural terrain fucntion: stats by altitude method
template <typename T, typename B>
struct hetero : public fractal<T> {
using seed_t = typename basis<T>::seed_t;
hetero ();
T H;
unsigned octaves;
T frequency;
T lacunarity;
T offset;
T amplitude;
T gain;
B basis;
virtual T operator() (T x, T y) const override;
};
}
}