2012-05-23 17:01:30 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2012-05-23 17:01:30 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-20 13:44:27 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2012-05-23 17:01:30 +10:00
|
|
|
*
|
|
|
|
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2015-05-28 10:56:06 +10:00
|
|
|
#include "fractal.hpp"
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-28 10:56:06 +10:00
|
|
|
#include "basis/value.hpp"
|
|
|
|
#include "basis/perlin.hpp"
|
|
|
|
#include "basis/worley.hpp"
|
|
|
|
#include "lerp.hpp"
|
|
|
|
|
|
|
|
#include "../debug.hpp"
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
#include <algorithm>
|
2015-05-26 16:31:30 +10:00
|
|
|
#include <cmath>
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
|
2015-05-18 22:02:10 +10:00
|
|
|
using util::noise::fractal;
|
|
|
|
using util::noise::fbm;
|
2015-05-26 16:31:30 +10:00
|
|
|
using util::noise::rmf;
|
2015-05-27 01:16:58 +10:00
|
|
|
using util::noise::hmf;
|
2015-05-27 01:17:24 +10:00
|
|
|
using util::noise::hetero;
|
2015-05-18 22:02:10 +10:00
|
|
|
|
|
|
|
|
2012-05-23 17:01:30 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-05-18 22:02:10 +10:00
|
|
|
template <typename T>
|
2015-05-26 16:31:30 +10:00
|
|
|
fractal<T>::fractal ():
|
|
|
|
seed (rand ())
|
2012-05-23 17:01:30 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 22:02:10 +10:00
|
|
|
template <typename T>
|
2015-05-26 16:31:30 +10:00
|
|
|
fractal<T>::fractal (seed_t _seed):
|
|
|
|
seed (_seed)
|
2012-05-24 21:25:18 +10:00
|
|
|
{ ; }
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 22:02:10 +10:00
|
|
|
template <typename T>
|
|
|
|
fractal<T>::~fractal ()
|
2012-05-23 17:01:30 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 22:02:10 +10:00
|
|
|
template <typename T>
|
|
|
|
T
|
2015-05-28 10:17:19 +10:00
|
|
|
fractal<T>::operator() (util::point<2,T>) const
|
2015-05-18 22:05:39 +10:00
|
|
|
{
|
|
|
|
unreachable ();
|
|
|
|
}
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
|
2015-05-18 22:02:10 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
namespace util { namespace noise {
|
|
|
|
template struct fractal<float>;
|
|
|
|
template struct fractal<double>;
|
|
|
|
} }
|
|
|
|
|
|
|
|
|
2012-05-23 17:01:30 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-05-18 22:02:10 +10:00
|
|
|
template <typename T, typename B>
|
|
|
|
fbm<T,B>::fbm (unsigned _octaves,
|
|
|
|
T _frequency,
|
|
|
|
T _lacunarity,
|
2015-05-26 16:31:30 +10:00
|
|
|
T _amplitude,
|
|
|
|
T _gain,
|
2015-05-18 22:02:10 +10:00
|
|
|
seed_t _seed):
|
2015-05-26 16:31:30 +10:00
|
|
|
fractal<T> (_seed),
|
|
|
|
octaves (_octaves),
|
|
|
|
frequency (_frequency),
|
|
|
|
lacunarity (_lacunarity),
|
|
|
|
amplitude (_amplitude),
|
|
|
|
gain (_gain),
|
2012-05-25 15:19:07 +10:00
|
|
|
basis (_seed)
|
2015-05-26 16:31:30 +10:00
|
|
|
{
|
|
|
|
CHECK_NEQ (octaves, 0);
|
|
|
|
CHECK_NEQ (frequency, 0);
|
|
|
|
CHECK_NEQ (amplitude, 0);
|
|
|
|
}
|
2012-05-25 15:19:07 +10:00
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 22:02:10 +10:00
|
|
|
template <typename T, typename B>
|
2015-05-26 16:31:30 +10:00
|
|
|
fbm<T,B>::fbm ():
|
2015-05-26 16:53:24 +10:00
|
|
|
fbm<T,B> (DEFAULT_OCTAVES,
|
|
|
|
DEFAULT_FREQUENCY,
|
|
|
|
DEFAULT_LACUNARITY,
|
|
|
|
DEFAULT_AMPLITUDE,
|
|
|
|
DEFAULT_GAIN,
|
|
|
|
rand ())
|
2012-05-23 17:01:30 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 22:02:10 +10:00
|
|
|
template <typename T, typename B>
|
|
|
|
T
|
2015-05-28 10:17:19 +10:00
|
|
|
fbm<T,B>::operator() (util::point<2,T> p) const {
|
2015-05-18 22:02:10 +10:00
|
|
|
T total = 0;
|
2015-05-26 16:31:30 +10:00
|
|
|
T f = frequency;
|
|
|
|
T a = amplitude;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-26 16:31:30 +10:00
|
|
|
for (size_t i = 0; i < octaves; ++i) {
|
2015-05-28 10:17:19 +10:00
|
|
|
total += basis (p * f) * a;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-26 16:31:30 +10:00
|
|
|
f *= lacunarity;
|
|
|
|
a *= gain;
|
2012-05-23 17:01:30 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-28 10:56:06 +10:00
|
|
|
template struct util::noise::fbm<float, util::noise::basis::worley<float>>;
|
|
|
|
template struct util::noise::fbm<float, util::noise::basis::perlin<float,util::lerp::linear>>;
|
|
|
|
template struct util::noise::fbm<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
|
|
|
|
template struct util::noise::fbm<float, util::noise::basis::value<float,util::lerp::trunc>>;
|
|
|
|
template struct util::noise::fbm<float, util::noise::basis::value<float,util::lerp::linear>>;
|
|
|
|
template struct util::noise::fbm<float, util::noise::basis::value<float,util::lerp::quintic>>;
|
2015-05-18 22:02:10 +10:00
|
|
|
|
2015-05-28 10:56:06 +10:00
|
|
|
template struct util::noise::fbm<double, util::noise::basis::worley<double>>;
|
|
|
|
template struct util::noise::fbm<double, util::noise::basis::perlin<double,util::lerp::linear>>;
|
|
|
|
template struct util::noise::fbm<double, util::noise::basis::perlin<double,util::lerp::quintic>>;
|
|
|
|
template struct util::noise::fbm<double, util::noise::basis::value<double,util::lerp::trunc>>;
|
|
|
|
template struct util::noise::fbm<double, util::noise::basis::value<double,util::lerp::linear>>;
|
|
|
|
template struct util::noise::fbm<double, util::noise::basis::value<double,util::lerp::quintic>>;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-05-18 22:02:10 +10:00
|
|
|
template <typename T, typename B>
|
2015-05-26 16:31:30 +10:00
|
|
|
rmf<T,B>::rmf (unsigned _octaves,
|
2015-05-18 22:02:10 +10:00
|
|
|
T _frequency,
|
|
|
|
T _lacunarity,
|
2015-05-26 16:31:30 +10:00
|
|
|
T _amplitude,
|
|
|
|
T _gain,
|
2015-05-18 22:02:10 +10:00
|
|
|
seed_t _seed):
|
2015-05-26 16:31:30 +10:00
|
|
|
fractal<T> (_seed),
|
|
|
|
octaves (_octaves),
|
|
|
|
frequency (_frequency),
|
|
|
|
lacunarity (_lacunarity),
|
|
|
|
amplitude (_amplitude),
|
|
|
|
gain (_gain),
|
2012-05-25 15:19:07 +10:00
|
|
|
basis (_seed)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 22:02:10 +10:00
|
|
|
template <typename T, typename B>
|
2015-05-26 16:31:30 +10:00
|
|
|
rmf<T,B>::rmf ():
|
2015-05-26 16:53:24 +10:00
|
|
|
rmf<T,B> (DEFAULT_OCTAVES,
|
|
|
|
DEFAULT_FREQUENCY,
|
|
|
|
DEFAULT_LACUNARITY,
|
|
|
|
DEFAULT_AMPLITUDE,
|
|
|
|
DEFAULT_GAIN,
|
|
|
|
rand ())
|
2012-05-23 17:01:30 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 22:02:10 +10:00
|
|
|
template <typename T, typename B>
|
|
|
|
T
|
2015-05-28 10:17:19 +10:00
|
|
|
rmf<T,B>::operator() (util::point<2,T> p) const {
|
2015-05-26 16:53:50 +10:00
|
|
|
const T offset = 1;
|
2015-05-27 01:16:58 +10:00
|
|
|
const T H = 1.f;
|
2015-05-26 16:31:30 +10:00
|
|
|
|
2015-05-26 16:53:50 +10:00
|
|
|
T exponents[octaves];
|
|
|
|
for (size_t i = 0; i < octaves; ++i)
|
|
|
|
exponents[i] = std::pow (std::pow (lacunarity, float (i)), -H);
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-26 16:53:50 +10:00
|
|
|
T signal = 0;
|
|
|
|
T result = 0;
|
|
|
|
T weight = 1;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-28 10:17:19 +10:00
|
|
|
p *= frequency;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-26 16:31:30 +10:00
|
|
|
for (size_t i = 0; i < octaves; ++i) {
|
2015-05-26 16:53:50 +10:00
|
|
|
// generates ridged noise
|
2015-05-28 10:17:19 +10:00
|
|
|
signal = basis (p);
|
2015-05-26 16:31:30 +10:00
|
|
|
signal = std::fabs (signal);
|
|
|
|
signal = offset - signal;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-26 16:53:50 +10:00
|
|
|
// sharpens the ridges
|
2015-05-26 16:31:30 +10:00
|
|
|
signal *= signal;
|
2015-05-26 16:53:50 +10:00
|
|
|
|
|
|
|
// influence by sharpness of previous iteration
|
2015-05-26 16:31:30 +10:00
|
|
|
signal *= weight;
|
2015-04-13 18:06:08 +10:00
|
|
|
|
2015-05-26 16:53:50 +10:00
|
|
|
// contribute to the weight
|
2012-05-23 17:01:30 +10:00
|
|
|
weight = signal * gain;
|
2015-05-18 22:02:10 +10:00
|
|
|
weight = limit (weight, 0, 1);
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-26 16:53:50 +10:00
|
|
|
// record and continue
|
2015-05-26 16:31:30 +10:00
|
|
|
result += signal * exponents[i];
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-28 10:17:19 +10:00
|
|
|
p *= lacunarity;
|
2012-05-23 17:01:30 +10:00
|
|
|
}
|
|
|
|
|
2015-05-26 16:31:30 +10:00
|
|
|
return result;
|
2012-05-23 17:01:30 +10:00
|
|
|
}
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-28 10:56:06 +10:00
|
|
|
template struct util::noise::rmf<float, util::noise::basis::worley<float>>;
|
|
|
|
template struct util::noise::rmf<float, util::noise::basis::perlin<float,util::lerp::linear>>;
|
|
|
|
template struct util::noise::rmf<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
|
|
|
|
template struct util::noise::rmf<float, util::noise::basis::value<float,util::lerp::linear>>;
|
|
|
|
template struct util::noise::rmf<float, util::noise::basis::value<float,util::lerp::quintic>>;
|
2015-05-26 16:31:30 +10:00
|
|
|
|
2015-05-28 10:56:06 +10:00
|
|
|
template struct util::noise::rmf<double, util::noise::basis::worley<double>>;
|
|
|
|
template struct util::noise::rmf<double, util::noise::basis::perlin<double,util::lerp::linear>>;
|
|
|
|
template struct util::noise::rmf<double, util::noise::basis::perlin<double,util::lerp::quintic>>;
|
|
|
|
template struct util::noise::rmf<double, util::noise::basis::value<double,util::lerp::linear>>;
|
|
|
|
template struct util::noise::rmf<double, util::noise::basis::value<double,util::lerp::quintic>>;
|
2012-05-23 17:01:30 +10:00
|
|
|
|
2015-05-27 01:16:58 +10:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T, typename B>
|
|
|
|
hmf<T,B>::hmf ():
|
|
|
|
H (0.25f),
|
|
|
|
octaves (6),
|
|
|
|
frequency (0.1f),
|
|
|
|
lacunarity (2),
|
|
|
|
offset (0.7f),
|
|
|
|
amplitude (1),
|
|
|
|
gain (1)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T, typename B>
|
|
|
|
T
|
2015-05-28 10:17:19 +10:00
|
|
|
hmf<T,B>::operator() (util::point<2,T> p) const
|
2015-05-27 01:16:58 +10:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2015-05-28 10:17:19 +10:00
|
|
|
p *= frequency;
|
2015-05-27 01:16:58 +10:00
|
|
|
|
|
|
|
for (size_t i = 0; i < octaves; ++i) {
|
2015-05-28 10:17:19 +10:00
|
|
|
signal = (basis (p) + offset) * exponents[i];
|
2015-05-27 01:16:58 +10:00
|
|
|
result += weight * signal;
|
|
|
|
|
|
|
|
weight *= gain * signal;
|
|
|
|
if (weight > 1)
|
|
|
|
weight = 1;
|
|
|
|
|
2015-05-28 10:17:19 +10:00
|
|
|
p *= lacunarity;
|
2015-05-27 01:16:58 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-28 10:56:06 +10:00
|
|
|
template struct util::noise::hmf<float, util::noise::basis::worley<float>>;
|
|
|
|
template struct util::noise::hmf<float, util::noise::basis::perlin<float,util::lerp::linear>>;
|
|
|
|
template struct util::noise::hmf<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
|
|
|
|
template struct util::noise::hmf<float, util::noise::basis::value<float,util::lerp::linear>>;
|
|
|
|
template struct util::noise::hmf<float, util::noise::basis::value<float,util::lerp::quintic>>;
|
2015-05-27 01:17:24 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
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
|
2015-05-28 10:17:19 +10:00
|
|
|
hetero<T,B>::operator() (util::point<2,T> p) const
|
2015-05-27 01:17:24 +10:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2015-05-28 10:17:19 +10:00
|
|
|
p *= frequency;
|
|
|
|
result = basis (p) + offset;
|
|
|
|
p *= lacunarity;
|
2015-05-27 01:17:24 +10:00
|
|
|
|
|
|
|
for (size_t i = 0; i < octaves; ++i) {
|
2015-05-28 10:17:19 +10:00
|
|
|
increment = basis (p) + offset;
|
2015-05-27 01:17:24 +10:00
|
|
|
increment *= exponents[i];
|
|
|
|
increment *= result;
|
|
|
|
|
|
|
|
result += increment;
|
|
|
|
|
2015-05-28 10:17:19 +10:00
|
|
|
p *= lacunarity;
|
2015-05-27 01:17:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-28 10:56:06 +10:00
|
|
|
template struct util::noise::hetero<float, util::noise::basis::worley<float>>;
|
|
|
|
template struct util::noise::hetero<float, util::noise::basis::perlin<float,util::lerp::linear>>;
|
|
|
|
template struct util::noise::hetero<float, util::noise::basis::perlin<float,util::lerp::quintic>>;
|
|
|
|
template struct util::noise::hetero<float, util::noise::basis::value<float,util::lerp::linear>>;
|
|
|
|
template struct util::noise::hetero<float, util::noise::basis::value<float,util::lerp::quintic>>;
|