Move noise functions into their own directory
This commit is contained in:
parent
c9242467d8
commit
bcab28826c
10
Makefile.am
10
Makefile.am
@ -65,8 +65,14 @@ UTIL_FILES = \
|
||||
memory.cpp \
|
||||
memory.hpp \
|
||||
nocopy.hpp \
|
||||
perlin.cpp \
|
||||
perlin.hpp \
|
||||
noise.cpp \
|
||||
noise.hpp \
|
||||
noise/basis.cpp \
|
||||
noise/basis.hpp \
|
||||
noise/fractal.cpp \
|
||||
noise/fractal.hpp \
|
||||
noise/lut.cpp \
|
||||
noise/lut.hpp \
|
||||
platform.hpp \
|
||||
point.cpp \
|
||||
point.hpp \
|
||||
|
@ -70,7 +70,7 @@ AS_CXX_COMPILER_FLAG([-pedantic], [COMMON_CFLAGS="$COMMON_CFLAGS -peda
|
||||
AS_CXX_COMPILER_FLAG([-fno-common ], [COMMON_CFLAGS="$COMMON_CFLAGS -fno-common "])
|
||||
AS_CXX_COMPILER_FLAG([-fno-nonansi-builtins],
|
||||
[COMMON_CFLAGS="$COMMON_CFLAGS -fno-nonansi-builtins"])
|
||||
AS_CXX_COMPILER_FLAG([-fno-rtti], [COMMON_CFLAGS="$COMMON_CFLAGS -fno-rtti"])
|
||||
##AS_CXX_COMPILER_FLAG([-fno-rtti], [COMMON_CFLAGS="$COMMON_CFLAGS -fno-rtti"])
|
||||
|
||||
|
||||
##
|
||||
|
44
noise.cpp
Normal file
44
noise.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This file is part of libgim.
|
||||
*
|
||||
* libgim is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
|
||||
#include "noise.hpp"
|
||||
|
||||
#include "range.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void
|
||||
util::noise::image2d (uint8_t *restrict pixels,
|
||||
size_t width,
|
||||
size_t height,
|
||||
const util::noise::fractal &p) {
|
||||
util::range<double> r(0.0,0.0);
|
||||
|
||||
for (size_t y = 0; y < height; ++y)
|
||||
for (size_t x = 0; x < width; ++x) {
|
||||
double v = p.eval (x / double (width) * 2, y / double (height) * 2);
|
||||
r.expand (v);
|
||||
v += 1.0;
|
||||
v /= 2.0;
|
||||
pixels[x + y * width] = v * std::numeric_limits<uint8_t>::max ();
|
||||
}
|
||||
|
||||
std::cerr << r << "\n";
|
||||
}
|
@ -23,30 +23,13 @@
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "noise/basis.hpp"
|
||||
#include "noise/fractal.hpp"
|
||||
|
||||
namespace util {
|
||||
struct perlin {
|
||||
typedef size_t seed_t;
|
||||
|
||||
unsigned octaves;
|
||||
double frequency;
|
||||
double persistence;
|
||||
seed_t seed;
|
||||
|
||||
perlin ();
|
||||
perlin (unsigned octaves,
|
||||
double frequency,
|
||||
double persistence,
|
||||
seed_t seed);
|
||||
|
||||
double sample (double x, double y) const;
|
||||
|
||||
protected:
|
||||
double generate (intmax_t x, intmax_t y) const;
|
||||
double eval (double x, double y) const;
|
||||
};
|
||||
|
||||
void perlin2d (uint8_t *restrict pixels, size_t width, size_t height, const perlin&);
|
||||
namespace noise {
|
||||
void image2d (uint8_t *restrict pixels, size_t width, size_t height, const util::noise::fractal&);
|
||||
}
|
||||
}
|
||||
|
||||
|
148
noise/basis.cpp
Normal file
148
noise/basis.cpp
Normal file
@ -0,0 +1,148 @@
|
||||
/*
|
||||
* This file is part of libgim.
|
||||
*
|
||||
* libgim is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "noise/basis.hpp"
|
||||
#include "noise/lut.hpp"
|
||||
#include "../vector.hpp"
|
||||
#include "../random.hpp"
|
||||
|
||||
using namespace util::noise;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Generate a type from [-UNIT..UNIT]
|
||||
template <typename T>
|
||||
T
|
||||
generate (intmax_t x, intmax_t y, basis::seed_t);
|
||||
|
||||
|
||||
template <>
|
||||
double
|
||||
generate (intmax_t x, intmax_t y, basis::seed_t seed) {
|
||||
size_t idx = permute (x, y, seed);
|
||||
return LUT[idx];
|
||||
}
|
||||
|
||||
|
||||
template <>
|
||||
util::vector2
|
||||
generate (intmax_t x, intmax_t y, basis::seed_t seed) {
|
||||
auto u = permute (x, y, seed);
|
||||
auto v = permute (u ^ seed);
|
||||
|
||||
return util::vector2 (LUT[u], LUT[v]);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
basis::basis (seed_t _seed):
|
||||
seed (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
basis::basis ():
|
||||
seed (util::random<seed_t> ())
|
||||
{ ; }
|
||||
|
||||
|
||||
basis::~basis ()
|
||||
{ ; }
|
||||
|
||||
|
||||
double
|
||||
basis::eval (double, double) const
|
||||
{ unreachable (); }
|
||||
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <lerp_function L>
|
||||
value<L>::value (seed_t _seed):
|
||||
basis (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
template <lerp_function L>
|
||||
value<L>::value ()
|
||||
{ ; }
|
||||
|
||||
|
||||
template <lerp_function L>
|
||||
double
|
||||
value<L>::eval (double x, double y) const {
|
||||
intmax_t x_int = intmax_t (x);
|
||||
double x_fac = x - x_int;
|
||||
intmax_t y_int = intmax_t (y);
|
||||
double y_fac = y - y_int;
|
||||
|
||||
// Generate the four corner values
|
||||
double p0 = generate<double> (x_int, y_int, this->seed);
|
||||
double p1 = generate<double> (x_int + 1, y_int, this->seed);
|
||||
double p2 = generate<double> (x_int, y_int + 1, this->seed);
|
||||
double p3 = generate<double> (x_int + 1, y_int + 1, this->seed);
|
||||
|
||||
// Interpolate on one dimension, then the other.
|
||||
return L (L (p0, p1, x_fac),
|
||||
L (p2, p3, x_fac),
|
||||
y_fac);
|
||||
}
|
||||
|
||||
template struct value<lerp::linear>;
|
||||
template struct value<lerp::cubic>;
|
||||
template struct value<lerp::quintic>;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <lerp_function L>
|
||||
gradient<L>::gradient (seed_t _seed):
|
||||
basis (_seed)
|
||||
{ ; }
|
||||
|
||||
template <lerp_function L>
|
||||
gradient<L>::gradient ()
|
||||
{ ; }
|
||||
|
||||
|
||||
template <lerp_function L>
|
||||
double
|
||||
gradient<L>::eval (double x, double y) const {
|
||||
intmax_t x_int = intmax_t (x);
|
||||
double x_fac = x - x_int;
|
||||
intmax_t y_int = intmax_t (y);
|
||||
double y_fac = y - y_int;
|
||||
|
||||
// Generate the four corner values
|
||||
vector2 p0 = generate<vector2> (x_int, y_int, this->seed);
|
||||
vector2 p1 = generate<vector2> (x_int + 1, y_int, this->seed);
|
||||
vector2 p2 = generate<vector2> (x_int, y_int + 1, this->seed);
|
||||
vector2 p3 = generate<vector2> (x_int + 1, y_int + 1, this->seed);
|
||||
|
||||
double v0 = p0.x * x_fac + p0.y * y_fac;
|
||||
double v1 = p1.x * (x_fac - 1.0) + p1.y * y_fac;
|
||||
double v2 = p2.x * x_fac + p2.y * (y_fac - 1.0);
|
||||
double v3 = p3.x * (x_fac - 1.0) + p3.y * (y_fac - 1.0);
|
||||
|
||||
return L (L (v0, v1, x_fac),
|
||||
L (v2, v3, x_fac),
|
||||
y_fac);
|
||||
}
|
||||
|
||||
template struct gradient<lerp::linear>;
|
||||
template struct gradient<lerp::cubic>;
|
||||
template struct gradient<lerp::quintic>;
|
||||
|
62
noise/basis.hpp
Normal file
62
noise/basis.hpp
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* This file is part of libgim.
|
||||
*
|
||||
* libgim is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_BASIS_HPP
|
||||
#define __UTIL_NOISE_BASIS_HPP
|
||||
|
||||
#include <cstdlib>
|
||||
#include "../lerp.hpp"
|
||||
|
||||
namespace util {
|
||||
namespace noise {
|
||||
typedef double (*lerp_function)(double, double, double);
|
||||
|
||||
struct basis {
|
||||
typedef size_t seed_t;
|
||||
|
||||
basis (seed_t);
|
||||
basis ();
|
||||
virtual ~basis ();
|
||||
|
||||
seed_t seed;
|
||||
virtual double eval (double x, double y) const = 0;
|
||||
};
|
||||
|
||||
|
||||
template <lerp_function L> struct value : public basis {
|
||||
value (seed_t);
|
||||
value ();
|
||||
virtual double eval (double x, double y) const;
|
||||
};
|
||||
|
||||
template <lerp_function L> struct gradient : public basis {
|
||||
gradient (seed_t);
|
||||
gradient ();
|
||||
virtual double eval (double x, double y) const;
|
||||
};
|
||||
|
||||
struct cellular : public basis {
|
||||
cellular (seed_t);
|
||||
cellular ();
|
||||
virtual double eval (double x, double y) const;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
130
noise/fractal.cpp
Normal file
130
noise/fractal.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* This file is part of libgim.
|
||||
*
|
||||
* libgim is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "noise/fractal.hpp"
|
||||
|
||||
#include "debug.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
util::noise::fractal::fractal (unsigned _octaves,
|
||||
double _frequency,
|
||||
double _lacunarity):
|
||||
octaves (_octaves),
|
||||
frequency (_frequency),
|
||||
lacunarity (_lacunarity)
|
||||
{ ; }
|
||||
|
||||
|
||||
util::noise::fractal::fractal ()
|
||||
{ ;}
|
||||
|
||||
|
||||
util::noise::fractal::~fractal ()
|
||||
{ ; }
|
||||
|
||||
|
||||
double
|
||||
util::noise::fractal::eval (double, double) const
|
||||
{ unreachable (); }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename B>
|
||||
util::noise::fbm<B>::fbm ()
|
||||
{ ; }
|
||||
|
||||
|
||||
template <typename B>
|
||||
double
|
||||
util::noise::fbm<B>::eval (double x, double y) const {
|
||||
double total = 0.0;
|
||||
double f = this->frequency;
|
||||
double a = this->lacunarity;
|
||||
|
||||
for (size_t i = 0; i < this->octaves; ++i) {
|
||||
total += basis.eval (x * f, y * f) * a;
|
||||
total = max (-1.0, min (1.0, total));
|
||||
|
||||
f *= 2.0;
|
||||
a *= this->lacunarity;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
|
||||
template struct util::noise::fbm<util::noise::gradient<lerp::linear>>;
|
||||
template struct util::noise::fbm<util::noise::gradient<lerp::quintic>>;
|
||||
template struct util::noise::fbm<util::noise::value<lerp::linear>>;
|
||||
template struct util::noise::fbm<util::noise::value<lerp::quintic>>;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename B>
|
||||
util::noise::musgrave<B>::musgrave ()
|
||||
{ ; }
|
||||
|
||||
|
||||
template <typename B>
|
||||
double
|
||||
util::noise::musgrave<B>::eval (double x, double y) const {
|
||||
double total = 0.0;
|
||||
double f = this->frequency;
|
||||
double a = 1.0;
|
||||
|
||||
double weight = 1.0;
|
||||
double offset = 1.0;
|
||||
double gain = 2.0;
|
||||
|
||||
double signal;
|
||||
|
||||
signal = basis.eval (x * f, y * f);
|
||||
signal = fabs (signal);
|
||||
signal = offset - signal;
|
||||
signal *= signal;
|
||||
total = signal;
|
||||
|
||||
for (size_t i = 1; i < this->octaves; ++i) {
|
||||
f *= 2.0;
|
||||
a *= this->lacunarity;
|
||||
|
||||
weight = signal * gain;
|
||||
weight = max (0.0, min (1.0, weight));
|
||||
|
||||
signal = basis.eval (x * f, y * f);
|
||||
signal = fabs (signal);
|
||||
signal = offset - signal;
|
||||
signal *= signal;
|
||||
|
||||
signal *= weight;
|
||||
total += signal * a;
|
||||
total = min (1.0, max (0.0, total));
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
template struct util::noise::musgrave<util::noise::gradient<lerp::linear>>;
|
||||
template struct util::noise::musgrave<util::noise::gradient<lerp::quintic>>;
|
||||
template struct util::noise::musgrave<util::noise::value<lerp::linear>>;
|
||||
template struct util::noise::musgrave<util::noise::value<lerp::quintic>>;
|
||||
|
68
noise/fractal.hpp
Normal file
68
noise/fractal.hpp
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* This file is part of libgim.
|
||||
*
|
||||
* libgim is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_FRACTAL_HPP
|
||||
#define __UTIL_NOISE_FRACTAL_HPP
|
||||
|
||||
#include "basis.hpp"
|
||||
|
||||
namespace util {
|
||||
namespace noise {
|
||||
struct fractal {
|
||||
fractal (unsigned octaves,
|
||||
double frequency,
|
||||
double lacunarity);
|
||||
fractal ();
|
||||
virtual ~fractal ();
|
||||
|
||||
unsigned octaves;
|
||||
double frequency;
|
||||
double lacunarity;
|
||||
|
||||
virtual double eval (double x, double y) const = 0;
|
||||
};
|
||||
|
||||
|
||||
template <typename B>
|
||||
struct fbm : public fractal {
|
||||
fbm (unsigned octaves,
|
||||
double frequency,
|
||||
double lacunarity,
|
||||
basis::seed_t seed);
|
||||
fbm ();
|
||||
|
||||
B basis;
|
||||
virtual double eval (double x, double y) const;
|
||||
};
|
||||
|
||||
template <typename B>
|
||||
struct musgrave : public fractal {
|
||||
musgrave (unsigned octaves,
|
||||
double frequency,
|
||||
double lacunarity,
|
||||
basis::seed_t seed);
|
||||
musgrave ();
|
||||
|
||||
B basis;
|
||||
virtual double eval (double x, double y) const;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
123
noise/lut.cpp
Normal file
123
noise/lut.cpp
Normal file
@ -0,0 +1,123 @@
|
||||
/*
|
||||
* This file is part of libgim.
|
||||
*
|
||||
* libgim is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "lut.hpp"
|
||||
|
||||
const size_t util::noise::PERM[256] = {
|
||||
114, 247, 195, 115, 164, 64, 53, 51,
|
||||
82, 75, 244, 19, 255, 1, 43, 77,
|
||||
126, 141, 77, 23, 169, 7, 29, 72,
|
||||
92, 97, 69, 211, 218, 71, 47, 25,
|
||||
178, 10, 135, 139, 152, 38, 51, 90,
|
||||
206, 98, 130, 32, 192, 6, 2, 34,
|
||||
74, 50, 162, 111, 188, 7, 24, 27,
|
||||
22, 87, 49, 67, 202, 35, 22, 79,
|
||||
253, 197, 73, 94, 182, 39, 37, 51,
|
||||
121, 85, 17, 18, 109, 63, 48, 66,
|
||||
173, 245, 183, 201, 68, 21, 81, 70,
|
||||
200, 0, 191, 86, 52, 80, 29, 76,
|
||||
15, 180, 41, 58, 128, 2, 38, 33,
|
||||
132, 119, 60, 242, 6, 46, 7, 99,
|
||||
237, 108, 30, 93, 153, 12, 43, 16,
|
||||
163, 209, 219, 117, 83, 13, 26, 14,
|
||||
55, 159, 190, 81, 224, 78, 8, 76,
|
||||
118, 203, 8, 193, 248, 84, 13, 48,
|
||||
240, 38, 65, 227, 160, 9, 36, 36,
|
||||
122, 185, 33, 123, 158, 56, 47, 88,
|
||||
246, 215, 231, 131, 5, 24, 4, 25,
|
||||
230, 174, 189, 168, 241, 56, 31, 20,
|
||||
166, 186, 184, 155, 29, 42, 25, 0,
|
||||
35, 105, 221, 39, 216, 96, 28, 23,
|
||||
28, 61, 196, 101, 149, 13, 65, 16,
|
||||
175, 194, 44, 34, 187, 95, 99, 20,
|
||||
45, 112, 252, 11, 59, 26, 12, 17,
|
||||
171, 14, 70, 54, 142, 4, 67, 46,
|
||||
4, 127, 20, 133, 205, 54, 43, 3,
|
||||
110, 249, 172, 161, 140, 57, 40, 89,
|
||||
3, 150, 210, 232, 57, 45, 44, 54,
|
||||
91, 79, 250, 62, 37, 98, 36, 34,
|
||||
};
|
||||
|
||||
|
||||
const double util::noise::LUT[256] = {
|
||||
-0.8984982139196056, 0.4750710231804418, -0.6519676730721629, 0.34818237268881935,
|
||||
-0.18205970986171605, 0.3451314956848195, 0.6414228668402755, -0.804052019344714,
|
||||
0.6838765435328942, 0.04489123586160382, 0.9387123107757223, -0.8286734231526627,
|
||||
-0.5752448478162355, -0.7177473684716851, 0.9935836223400552, 0.7280624253259176,
|
||||
0.17859563727209138, -0.03606344150253982, -0.15882231678491388, 0.4578698195539206,
|
||||
-0.4154225972126202, -0.5194028373429245, 0.24561279025507043, -0.8817931383760225,
|
||||
0.051621615744299465, -0.432105334458762, 0.1565615530978024, 0.6597775199873575,
|
||||
0.22566777177285857, -0.30483300012361036, -0.6958206141316641, -0.2930693331180243,
|
||||
-0.7690503602769119, -0.7171818210252867, 0.8800261811087657, 0.8601395971416754,
|
||||
0.5784335763333026, 0.060900655607116994, 0.2837112260220951, -0.08799217109739876,
|
||||
-0.2384174709999518, -0.770742115936714, 0.9217986500940929, 0.33319037162928433,
|
||||
0.2338527159073147, 0.9326260912305022, -0.3277100775267676, -0.6923414380512052,
|
||||
0.2743829582530313, 0.021903777340825714, -0.99594589251304, 0.08310122202568193,
|
||||
0.031989194381878194, 0.322274671882117, 0.15855440115773267, 0.7221472232498565,
|
||||
-0.5369740081438523, -0.013966337693399833, -0.3797229432191469, -0.1449216443374528,
|
||||
0.4780175549902941, 0.3253792901693007, 0.8302471035699828, -0.19705446486920075,
|
||||
-0.05145564508096312, 0.07804387053425987, -0.43766576462644347, -0.6841198355261591,
|
||||
0.7137706240638149, -0.05484055085953887, -0.8299960004843008, 0.18050577055894035,
|
||||
-0.8541390034524383, -0.26341201926055424, 0.7023808019022437, 0.6230332593788754,
|
||||
-0.2959807962046559, 0.8971411582261257, -0.37285148660593714, -0.9689413803647513,
|
||||
0.36271555405510414, -0.2369795401687047, -0.057686227499222476, 0.45924926449705117,
|
||||
0.5909634897508582, -0.8641968475739907, -0.9868274716835026, 0.15367421531182823,
|
||||
0.47868338795465704, 0.22205133716275216, 0.9342712841005696, 0.574755268557622,
|
||||
-0.30781440722062237, 0.08940264916288165, -0.7364035085406784, -0.42804724032315544,
|
||||
-0.3028896080136707, 0.08043452763728154, 0.35597931924492854, 0.8218045295996388,
|
||||
-0.2639539020450863, 0.14018881766976743, -0.9679978866175825, 0.917534812674309,
|
||||
0.8315532029453319, -0.26657576198722377, 0.5292911314793278, 0.05106195480414244,
|
||||
-0.20703582048726044, 0.9363562930545231, 0.07180492189859389, -0.9307494388376307,
|
||||
0.48401747690392316, -0.47666025737165385, -0.2995451643535043, 0.9645238474628381,
|
||||
-0.9869506025182451, -0.6267193760632264, 0.9417871950627361, -0.3712682888227079,
|
||||
0.17116236816682484, 0.1637861802304843, 0.5979849303287379, -0.7943555560703199,
|
||||
-0.8605593297161265, 0.24271029593235993, 0.8838806440116482, 0.6563769551024692,
|
||||
0.9357703879808301, -0.76818344297623, 0.7661179391954425, 0.426530438580041,
|
||||
-0.7280736750184795, 0.5258880971264928, -0.3736324615581794, -0.3726200928816683,
|
||||
-0.652698944621871, 0.1312826210242899, 0.022999070967927526, 0.8362192026388207,
|
||||
-0.20524579974666657, -0.39989736807434273, 0.097666397673847, 0.25215779272486416,
|
||||
-0.6800982672863105, 0.8013507802109068, 0.969941009294419, -0.2760313084806676,
|
||||
0.8961286070268608, 0.9455740223217473, -0.3908254141167051, 0.8195441725353367,
|
||||
-0.571435969794009, 0.18987091511038434, -0.04181589713692069, -0.963609489444007,
|
||||
-0.5281459088656999, 0.984266295584705, 0.5027560371807003, -0.1597738785923013,
|
||||
0.3411131481434917, 0.12429799530598684, -0.5159001480993852, 0.8865722840594523,
|
||||
0.32969200413777755, 0.26288894768315707, -0.054978081511632526, -0.37725903459638177,
|
||||
0.18451775548204163, -0.6691455193869638, 0.9285268978526233, -0.49526622636914563,
|
||||
-0.12647685137552322, -0.08438978664878105, -0.4263493078967011, 0.04134592579064478,
|
||||
0.5682513712371484, 0.3144579798890128, -0.5560826395168046, -0.7651643315076799,
|
||||
-0.4919051361482554, 0.4326849315222068, 0.9745607803884215, 0.5459754840343867,
|
||||
-0.3314574932052319, 0.10102331343981996, -0.8762141515646111, -0.8182536989519216,
|
||||
0.5460251598764976, 0.11958795888450968, -0.7226482729866581, -0.5362654501182944,
|
||||
0.9461689136255056, 0.7302075935427872, 0.2067222384707743, -0.5515294144965857,
|
||||
0.5261119777013636, -0.6058524684579083, -0.7984088236296627, 0.5515069318003252,
|
||||
0.6842126925238694, 0.9653367596972298, -0.4209547631571078, -0.200564767100335,
|
||||
-0.45471383032946067, -0.5407480745656619, 0.7853038244196648, 0.08741550867662506,
|
||||
0.46188948442462063, 0.32857850460858384, 0.2519455394025394, -0.5647397901411311,
|
||||
-0.8010538532270075, 0.03511555124942123, 0.8126644809142252, -0.6165437893290324,
|
||||
-0.769144309230668, 0.5811756737759881, 0.04347549540355833, -0.1516485473331033,
|
||||
-0.8025604153817143, 0.2078203269944925, -0.8281743083984969, -0.5483796887270644,
|
||||
-0.67075976237068, 0.08046291156170216, -0.726116057042915, 0.6064692851498683,
|
||||
-0.2326853771750068, -0.1199137086641211, -0.007902895438233637, -0.024018850623878762,
|
||||
0.23518281506286076, 0.8739589142046267, 0.9461972684232018, -0.6298769685383707,
|
||||
-0.37298229425868623, -0.0443367550367304, -0.9688376819157003, 0.09999263734937558,
|
||||
-0.3975824040014859, 0.3353645267506893, -0.2693845909559329, -0.1133673242389015,
|
||||
-0.8453467042866694, 0.5313565920953589, -0.5313028936659494, 0.23266080060836347,
|
||||
0.19579971665018436, 0.9392643282850877, 0.08508300565389537, -0.43964224026992116,
|
||||
0.5862798492854766, 0.7001806971895554, 0.9050597336377173, -0.5789943930802939,
|
||||
};
|
48
noise/lut.hpp
Normal file
48
noise/lut.hpp
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This file is part of libgim.
|
||||
*
|
||||
* libgim is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_NOISE_LUT_HPP
|
||||
#define __UTIL_NOISE_LUT_HPP
|
||||
|
||||
#include <cstdlib>
|
||||
#include <type_traits>
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
namespace util {
|
||||
namespace noise {
|
||||
extern const size_t PERM[256];
|
||||
extern const double LUT [256];
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_integral<T>::value, size_t>::type
|
||||
permute (T idx) {
|
||||
return PERM[(size_t)idx % elems (PERM)];
|
||||
}
|
||||
|
||||
template <typename T1, typename T2, typename ...R>
|
||||
size_t permute (T1 t1, T2 t2, R ...r) {
|
||||
auto p = permute (t1);
|
||||
p += permute (t2, r...);
|
||||
return p % elems (PERM);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
111
perlin.cpp
111
perlin.cpp
@ -1,111 +0,0 @@
|
||||
/*
|
||||
* This file is part of libgim.
|
||||
*
|
||||
* libgim is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
|
||||
#include "perlin.hpp"
|
||||
|
||||
#include "bitwise.hpp"
|
||||
#include "debug.hpp"
|
||||
#include "hash.hpp"
|
||||
#include "lerp.hpp"
|
||||
#include "maths.hpp"
|
||||
#include "random.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
|
||||
#define LERP(a,b,t) lerp::quintic (a, b, t)
|
||||
|
||||
|
||||
// Just a random generator [-1.0, 1.0]
|
||||
double
|
||||
util::perlin::generate (intmax_t x, intmax_t y) const {
|
||||
intmax_t n = x + 257 * y;
|
||||
n = (n << 13U) ^ n ^ (intmax_t)seed;
|
||||
return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
|
||||
}
|
||||
|
||||
|
||||
double
|
||||
util::perlin::eval (double x, double y) const {
|
||||
intmax_t x_int = intmax_t (x);
|
||||
double x_fac = x - x_int;
|
||||
intmax_t y_int = intmax_t (y);
|
||||
double y_fac = y - y_int;
|
||||
|
||||
// Generate the four corner values
|
||||
double p0 = generate (x_int, y_int);
|
||||
double p1 = generate (x_int + 1, y_int);
|
||||
double p2 = generate (x_int, y_int + 1);
|
||||
double p3 = generate (x_int + 1, y_int + 1);
|
||||
|
||||
// Interpolate on one dimension, then the other.
|
||||
return LERP (LERP (p0, p1, x_fac),
|
||||
LERP (p2, p3, x_fac),
|
||||
y_fac);
|
||||
}
|
||||
|
||||
|
||||
double
|
||||
util::perlin::sample (double x, double y) const {
|
||||
double total = 0.0;
|
||||
|
||||
for (size_t i = 0; i < octaves; ++i) {
|
||||
double f = frequency * powf (2.0, i);
|
||||
double amplitude = powf (persistence, i);
|
||||
|
||||
total += eval (x * f, y * f) * amplitude;
|
||||
total = max (-1.0, min (1.0, total));
|
||||
}
|
||||
|
||||
return (1.0 + total) / 2.0;
|
||||
}
|
||||
|
||||
|
||||
util::perlin::perlin ():
|
||||
octaves (3),
|
||||
frequency (25.0),
|
||||
persistence (0.5),
|
||||
seed (util::random<size_t> ())
|
||||
{ ; }
|
||||
|
||||
|
||||
util::perlin::perlin (unsigned _octaves,
|
||||
double _frequency,
|
||||
double _persistence,
|
||||
seed_t _seed):
|
||||
octaves (_octaves),
|
||||
frequency (_frequency),
|
||||
persistence (_persistence),
|
||||
seed (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
void
|
||||
util::perlin2d (uint8_t *restrict pixels,
|
||||
size_t width,
|
||||
size_t height,
|
||||
const perlin &p) {
|
||||
for (size_t y = 0; y < height; ++y)
|
||||
for (size_t x = 0; x < width; ++x) {
|
||||
double v = p.sample (x / double (width), y / double (height));
|
||||
pixels[x + y * width] = v * std::numeric_limits<uint8_t>::max ();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user