diff --git a/noise/fractal.cpp b/noise/fractal.cpp index 5d19e34f..4da7f851 100644 --- a/noise/fractal.cpp +++ b/noise/fractal.cpp @@ -25,6 +25,7 @@ using util::noise::fractal; using util::noise::fbm; using util::noise::rmf; +using util::noise::hmf; /////////////////////////////////////////////////////////////////////////////// @@ -167,7 +168,7 @@ template T rmf::operator() (T x, T y) const { const T offset = 1; - const T H = 1; + const T H = 1.f; T exponents[octaves]; for (size_t i = 0; i < octaves; ++i) @@ -220,3 +221,54 @@ template struct util::noise::rmf>; template struct util::noise::rmf>; + +//----------------------------------------------------------------------------- +template +hmf::hmf (): + H (0.25f), + octaves (6), + frequency (0.1f), + lacunarity (2), + offset (0.7f), + amplitude (1), + gain (1) +{ ; } + + +//----------------------------------------------------------------------------- +template +T +hmf::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 signal = 0; + T weight = 1; + + x *= frequency; + y *= frequency; + + for (size_t i = 0; i < octaves; ++i) { + signal = (basis (x, y) + offset) * exponents[i]; + result += weight * signal; + + weight *= gain * signal; + if (weight > 1) + weight = 1; + + x *= lacunarity; + y *= lacunarity; + } + + return result; +} + + +template struct util::noise::hmf>; +template struct util::noise::hmf>; +template struct util::noise::hmf>; +template struct util::noise::hmf>; +template struct util::noise::hmf>; diff --git a/noise/fractal.hpp b/noise/fractal.hpp index db6b4661..b4b24e4d 100644 --- a/noise/fractal.hpp +++ b/noise/fractal.hpp @@ -93,11 +93,11 @@ namespace util { static constexpr T DEFAULT_GAIN = 2; rmf (unsigned octaves, - T frequency, - T lacunarity, - T amplitude, - T gain, - seed_t seed); + T frequency, + T lacunarity, + T amplitude, + T gain, + seed_t seed); rmf (); unsigned octaves; @@ -111,6 +111,30 @@ namespace util { B basis; virtual T operator() (T x, T y) const override; }; + + + /////////////////////////////////////////////////////////////////////// + template + struct hmf : public fractal { + using seed_t = typename basis::seed_t; + + hmf (); + + 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; + }; } }