noise: parameterise fractal summation types
This commit is contained in:
parent
c24387bf8f
commit
9b0abd8f7a
14
noise.cpp
14
noise.cpp
@ -24,25 +24,29 @@
|
|||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
template <typename T>
|
||||||
void
|
void
|
||||||
util::noise::fill (image::buffer<float> &pixels,
|
util::noise::fill (image::buffer<T> &pixels,
|
||||||
const util::noise::fractal &gen)
|
const util::noise::fractal<T> &gen)
|
||||||
{
|
{
|
||||||
size_t h = pixels.h, s = pixels.s, w = pixels.w;
|
size_t h = pixels.h, s = pixels.s, w = pixels.w;
|
||||||
float *data = pixels.data ();
|
T *data = pixels.data ();
|
||||||
|
|
||||||
for (size_t y = 0; y < h; ++y)
|
for (size_t y = 0; y < h; ++y)
|
||||||
for (size_t x = 0; x < w; ++x)
|
for (size_t x = 0; x < w; ++x)
|
||||||
data[y * s + x] = gen (double(x), double(y));
|
data[y * s + x] = gen (x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template void util::noise::fill (image::buffer<float>&, const util::noise::fractal<float>&);
|
||||||
|
template void util::noise::fill (image::buffer<double>&, const util::noise::fractal<double>&);
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void
|
void
|
||||||
util::noise::image2d (uint8_t *restrict pixels,
|
util::noise::image2d (uint8_t *restrict pixels,
|
||||||
size_t width,
|
size_t width,
|
||||||
size_t height,
|
size_t height,
|
||||||
const util::noise::fractal &p) {
|
const util::noise::fractal<float> &p) {
|
||||||
for (size_t y = 0; y < height; ++y)
|
for (size_t y = 0; y < height; ++y)
|
||||||
for (size_t x = 0; x < width; ++x) {
|
for (size_t x = 0; x < width; ++x) {
|
||||||
double v = p.eval (x, y);
|
double v = p.eval (x, y);
|
||||||
|
@ -27,9 +27,10 @@
|
|||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
namespace noise {
|
namespace noise {
|
||||||
void fill (image::buffer<float>&, const util::noise::fractal&);
|
template <typename T>
|
||||||
|
void fill (image::buffer<T>&, const util::noise::fractal<T>&);
|
||||||
|
|
||||||
void image2d (uint8_t *restrict pixels, size_t width, size_t height, const util::noise::fractal&);
|
void image2d (uint8_t *restrict pixels, size_t width, size_t height, const util::noise::fractal<float>&);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,10 +21,16 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
|
using util::noise::fractal;
|
||||||
|
using util::noise::fbm;
|
||||||
|
using util::noise::musgrave;
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
util::noise::fractal::fractal (unsigned _octaves,
|
template <typename T>
|
||||||
double _frequency,
|
fractal<T>::fractal (unsigned _octaves,
|
||||||
double _lacunarity):
|
T _frequency,
|
||||||
|
T _lacunarity):
|
||||||
octaves (_octaves),
|
octaves (_octaves),
|
||||||
frequency (_frequency),
|
frequency (_frequency),
|
||||||
lacunarity (_lacunarity)
|
lacunarity (_lacunarity)
|
||||||
@ -32,54 +38,64 @@ util::noise::fractal::fractal (unsigned _octaves,
|
|||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
util::noise::fractal::fractal ():
|
template <typename T>
|
||||||
|
fractal<T>::fractal ():
|
||||||
octaves (1),
|
octaves (1),
|
||||||
frequency (0.1),
|
frequency (T(0.1)),
|
||||||
lacunarity (0.6)
|
lacunarity (T(0.6))
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
util::noise::fractal::~fractal ()
|
template <typename T>
|
||||||
|
fractal<T>::~fractal ()
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
double
|
template <typename T>
|
||||||
util::noise::fractal::eval (double, double) const
|
T
|
||||||
|
fractal<T>::eval (T, T) const
|
||||||
{ unreachable (); }
|
{ unreachable (); }
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
namespace util { namespace noise {
|
||||||
|
template struct fractal<float>;
|
||||||
|
template struct fractal<double>;
|
||||||
|
} }
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
template <typename B>
|
template <typename T, typename B>
|
||||||
util::noise::fbm<B>::fbm (unsigned _octaves,
|
fbm<T,B>::fbm (unsigned _octaves,
|
||||||
double _frequency,
|
T _frequency,
|
||||||
double _lacunarity,
|
T _lacunarity,
|
||||||
seed_t _seed):
|
seed_t _seed):
|
||||||
fractal (_octaves, _frequency, _lacunarity),
|
fractal<T> (_octaves, _frequency, _lacunarity),
|
||||||
basis (_seed)
|
basis (_seed)
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template <typename B>
|
template <typename T, typename B>
|
||||||
util::noise::fbm<B>::fbm ()
|
fbm<T,B>::fbm ()
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template <typename B>
|
template <typename T, typename B>
|
||||||
double
|
T
|
||||||
util::noise::fbm<B>::eval (double x, double y) const {
|
fbm<T,B>::eval (T x, T y) const {
|
||||||
double total = 0.0;
|
T total = 0;
|
||||||
double f = this->frequency;
|
T f = this->frequency;
|
||||||
double a = 1.0;
|
T a = 1;
|
||||||
double a_sum = 0.0;
|
T a_sum = 0;
|
||||||
|
|
||||||
for (size_t i = 0; i < this->octaves; ++i) {
|
for (size_t i = 0; i < this->octaves; ++i) {
|
||||||
total += basis.eval (x * f, y * f) * a;
|
total += basis.eval (x * f, y * f) * a;
|
||||||
|
|
||||||
f *= 2.0;
|
f *= 2;
|
||||||
|
|
||||||
a_sum += a;
|
a_sum += a;
|
||||||
a *= this->lacunarity;
|
a *= this->lacunarity;
|
||||||
@ -90,65 +106,71 @@ util::noise::fbm<B>::eval (double x, double y) const {
|
|||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template struct util::noise::fbm<util::noise::cellular<double>>;
|
template struct util::noise::fbm<float, util::noise::cellular<float>>;
|
||||||
template struct util::noise::fbm<util::noise::gradient<double,util::lerp::linear>>;
|
template struct util::noise::fbm<float, util::noise::gradient<float,util::lerp::linear>>;
|
||||||
template struct util::noise::fbm<util::noise::gradient<double,util::lerp::quintic>>;
|
template struct util::noise::fbm<float, util::noise::gradient<float,util::lerp::quintic>>;
|
||||||
template struct util::noise::fbm<util::noise::value<double,util::lerp::linear>>;
|
template struct util::noise::fbm<float, util::noise::value<float,util::lerp::linear>>;
|
||||||
template struct util::noise::fbm<util::noise::value<double,util::lerp::quintic>>;
|
template struct util::noise::fbm<float, util::noise::value<float,util::lerp::quintic>>;
|
||||||
|
|
||||||
|
template struct util::noise::fbm<double, util::noise::cellular<double>>;
|
||||||
|
template struct util::noise::fbm<double, util::noise::gradient<double,util::lerp::linear>>;
|
||||||
|
template struct util::noise::fbm<double, util::noise::gradient<double,util::lerp::quintic>>;
|
||||||
|
template struct util::noise::fbm<double, util::noise::value<double,util::lerp::linear>>;
|
||||||
|
template struct util::noise::fbm<double, util::noise::value<double,util::lerp::quintic>>;
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
template <typename B>
|
template <typename T, typename B>
|
||||||
util::noise::musgrave<B>::musgrave (unsigned _octaves,
|
musgrave<T,B>::musgrave (unsigned _octaves,
|
||||||
double _frequency,
|
T _frequency,
|
||||||
double _lacunarity,
|
T _lacunarity,
|
||||||
seed_t _seed):
|
seed_t _seed):
|
||||||
fractal (_octaves, _frequency, _lacunarity),
|
fractal<T> (_octaves, _frequency, _lacunarity),
|
||||||
basis (_seed)
|
basis (_seed)
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template <typename B>
|
template <typename T, typename B>
|
||||||
util::noise::musgrave<B>::musgrave ()
|
musgrave<T,B>::musgrave ()
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template <typename B>
|
template <typename T, typename B>
|
||||||
double
|
T
|
||||||
util::noise::musgrave<B>::eval (double x, double y) const {
|
musgrave<T,B>::eval (T x, T y) const {
|
||||||
double total = 0.0;
|
T total = 0;
|
||||||
double f = this->frequency;
|
T f = this->frequency;
|
||||||
double a = 1.0;
|
T a = 1;
|
||||||
|
|
||||||
double weight = 1.0;
|
T weight = 1;
|
||||||
double offset = 1.0;
|
T offset = 1;
|
||||||
double gain = 2.0;
|
T gain = 2;
|
||||||
|
|
||||||
double signal;
|
T signal;
|
||||||
|
|
||||||
signal = basis.eval (x * f, y * f);
|
signal = basis.eval (x * f, y * f);
|
||||||
signal = fabs (signal);
|
signal = std::fabs (signal);
|
||||||
signal = offset - signal;
|
signal = offset - signal;
|
||||||
signal *= signal;
|
signal *= signal;
|
||||||
total = signal;
|
total = signal;
|
||||||
|
|
||||||
for (size_t i = 1; i < this->octaves; ++i) {
|
for (size_t i = 1; i < this->octaves; ++i) {
|
||||||
f *= 2.0;
|
f *= 2;
|
||||||
a *= this->lacunarity;
|
a *= this->lacunarity;
|
||||||
|
|
||||||
weight = signal * gain;
|
weight = signal * gain;
|
||||||
weight = max (0.0, min (1.0, weight));
|
weight = limit (weight, 0, 1);
|
||||||
|
|
||||||
signal = basis.eval (x * f, y * f);
|
signal = basis.eval (x * f, y * f);
|
||||||
signal = fabs (signal);
|
signal = std::fabs (signal);
|
||||||
signal = offset - signal;
|
signal = offset - signal;
|
||||||
signal *= signal;
|
signal *= signal;
|
||||||
|
|
||||||
signal *= weight;
|
signal *= weight;
|
||||||
total += signal * a;
|
total += signal * a;
|
||||||
total = min (1.0, max (0.0, total));
|
total = limit (total, 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
@ -156,9 +178,15 @@ util::noise::musgrave<B>::eval (double x, double y) const {
|
|||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template struct util::noise::musgrave<util::noise::cellular<double>>;
|
template struct util::noise::musgrave<float, util::noise::cellular<float>>;
|
||||||
template struct util::noise::musgrave<util::noise::gradient<double,util::lerp::linear>>;
|
template struct util::noise::musgrave<float, util::noise::gradient<float,util::lerp::linear>>;
|
||||||
template struct util::noise::musgrave<util::noise::gradient<double,util::lerp::quintic>>;
|
template struct util::noise::musgrave<float, util::noise::gradient<float,util::lerp::quintic>>;
|
||||||
template struct util::noise::musgrave<util::noise::value<double,util::lerp::linear>>;
|
template struct util::noise::musgrave<float, util::noise::value<float,util::lerp::linear>>;
|
||||||
template struct util::noise::musgrave<util::noise::value<double,util::lerp::quintic>>;
|
template struct util::noise::musgrave<float, util::noise::value<float,util::lerp::quintic>>;
|
||||||
|
|
||||||
|
template struct util::noise::musgrave<double, util::noise::cellular<double>>;
|
||||||
|
template struct util::noise::musgrave<double, util::noise::gradient<double,util::lerp::linear>>;
|
||||||
|
template struct util::noise::musgrave<double, util::noise::gradient<double,util::lerp::quintic>>;
|
||||||
|
template struct util::noise::musgrave<double, util::noise::value<double,util::lerp::linear>>;
|
||||||
|
template struct util::noise::musgrave<double, util::noise::value<double,util::lerp::quintic>>;
|
||||||
|
|
||||||
|
@ -22,51 +22,52 @@
|
|||||||
namespace util {
|
namespace util {
|
||||||
namespace noise {
|
namespace noise {
|
||||||
/// Base noise summation
|
/// Base noise summation
|
||||||
|
template <typename T>
|
||||||
struct fractal {
|
struct fractal {
|
||||||
fractal (unsigned octaves,
|
fractal (unsigned octaves,
|
||||||
double frequency,
|
T frequency,
|
||||||
double lacunarity);
|
T lacunarity);
|
||||||
fractal ();
|
fractal ();
|
||||||
virtual ~fractal ();
|
virtual ~fractal ();
|
||||||
|
|
||||||
unsigned octaves;
|
unsigned octaves;
|
||||||
double frequency;
|
T frequency;
|
||||||
double lacunarity;
|
T lacunarity;
|
||||||
|
|
||||||
virtual double operator() (double x, double y) const { return eval (x, y); };
|
T operator() (T x, T y) const { return eval (x, y); };
|
||||||
virtual double eval (double x, double y) const = 0;
|
virtual T eval (T x, T y) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Fractal Brownian Motion summation.
|
/// Fractal Brownian Motion summation.
|
||||||
template <typename B>
|
template <typename T, typename B>
|
||||||
struct fbm : public fractal {
|
struct fbm : public fractal<T> {
|
||||||
using seed_t = typename basis<double>::seed_t;
|
using seed_t = typename basis<T>::seed_t;
|
||||||
|
|
||||||
fbm (unsigned octaves,
|
fbm (unsigned octaves,
|
||||||
double frequency,
|
T frequency,
|
||||||
double lacunarity,
|
T lacunarity,
|
||||||
seed_t seed);
|
seed_t seed);
|
||||||
fbm ();
|
fbm ();
|
||||||
|
|
||||||
B basis;
|
B basis;
|
||||||
virtual double eval (double x, double y) const;
|
virtual T eval (T x, T y) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Rigid Multifractal noise summation.
|
/// Rigid Multifractal noise summation.
|
||||||
template <typename B>
|
template <typename T, typename B>
|
||||||
struct musgrave : public fractal {
|
struct musgrave : public fractal<T> {
|
||||||
using seed_t = typename basis<double>::seed_t;
|
using seed_t = typename basis<T>::seed_t;
|
||||||
|
|
||||||
musgrave (unsigned octaves,
|
musgrave (unsigned octaves,
|
||||||
double frequency,
|
T frequency,
|
||||||
double lacunarity,
|
T lacunarity,
|
||||||
seed_t seed);
|
seed_t seed);
|
||||||
musgrave ();
|
musgrave ();
|
||||||
|
|
||||||
B basis;
|
B basis;
|
||||||
virtual double eval (double x, double y) const;
|
virtual T eval (T x, T y) const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ main (void)
|
|||||||
//using basis_t = util::noise::gradient<lerp_t>;
|
//using basis_t = util::noise::gradient<lerp_t>;
|
||||||
//using noise_t = util::noise::fbm<basis_t>;
|
//using noise_t = util::noise::fbm<basis_t>;
|
||||||
|
|
||||||
using noise_t = util::noise::fbm<util::noise::gradient<double,util::lerp::quintic>>;
|
using noise_t = util::noise::fbm<float,util::noise::gradient<float,util::lerp::quintic>>;
|
||||||
//using noise_t = util::noise::musgrave<util::noise::gradient<double,util::lerp::quintic>>;
|
//using noise_t = util::noise::musgrave<float,util::noise::gradient<float,util::lerp::quintic>>;
|
||||||
//using noise_t = util::noise::fbm<util::noise::cellular<double>>;
|
//using noise_t = util::noise::fbm<float,util::noise::cellular<float>>;
|
||||||
|
|
||||||
util::noise::fill (img, noise_t {});
|
util::noise::fill (img, noise_t {});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user