2012-05-16 15:01:41 +10:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-11-12 20:41:46 +11:00
|
|
|
#include "perlin.hpp"
|
|
|
|
|
2012-04-11 15:25:10 +10:00
|
|
|
#include "bitwise.hpp"
|
2011-11-12 20:41:46 +11:00
|
|
|
#include "debug.hpp"
|
2012-04-11 15:25:10 +10:00
|
|
|
#include "hash.hpp"
|
|
|
|
#include "lerp.hpp"
|
2011-11-12 20:41:46 +11:00
|
|
|
#include "maths.hpp"
|
2012-04-11 15:25:10 +10:00
|
|
|
#include "random.hpp"
|
2011-11-12 20:41:46 +11:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
|
2012-05-17 14:18:39 +10:00
|
|
|
#define LERP(a,b,t) lerp::quintic (a, b, t)
|
|
|
|
|
|
|
|
|
2012-05-17 14:37:56 +10:00
|
|
|
// 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);
|
|
|
|
}
|
2012-04-11 15:25:10 +10:00
|
|
|
|
|
|
|
|
2012-05-17 14:37:56 +10:00
|
|
|
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;
|
2011-11-12 20:41:46 +11:00
|
|
|
|
2012-05-17 14:37:56 +10:00
|
|
|
// 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);
|
2011-11-12 20:41:46 +11:00
|
|
|
|
2012-05-17 14:37:56 +10:00
|
|
|
// Interpolate on one dimension, then the other.
|
|
|
|
return LERP (LERP (p0, p1, x_fac),
|
|
|
|
LERP (p2, p3, x_fac),
|
|
|
|
y_fac);
|
|
|
|
}
|
2011-11-12 20:41:46 +11:00
|
|
|
|
2012-05-17 14:37:56 +10:00
|
|
|
|
|
|
|
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));
|
2011-11-12 20:41:46 +11:00
|
|
|
}
|
|
|
|
|
2012-05-17 14:37:56 +10:00
|
|
|
return (1.0 + total) / 2.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
util::perlin::perlin ():
|
|
|
|
octaves (3),
|
|
|
|
frequency (25.0),
|
|
|
|
persistence (0.5),
|
|
|
|
seed (util::random<size_t> ())
|
|
|
|
{ ; }
|
|
|
|
|
2011-11-12 20:41:46 +11:00
|
|
|
|
2012-05-17 14:37:56 +10:00
|
|
|
util::perlin::perlin (unsigned _octaves,
|
|
|
|
double _frequency,
|
|
|
|
double _persistence,
|
|
|
|
seed_t _seed):
|
|
|
|
octaves (_octaves),
|
|
|
|
frequency (_frequency),
|
|
|
|
persistence (_persistence),
|
|
|
|
seed (_seed)
|
|
|
|
{ ; }
|
2011-11-12 20:41:46 +11:00
|
|
|
|
2012-05-17 14:37:56 +10:00
|
|
|
|
|
|
|
void
|
|
|
|
util::perlin2d (uint8_t *restrict pixels,
|
|
|
|
size_t width,
|
|
|
|
size_t height,
|
|
|
|
const perlin &p) {
|
2011-11-12 20:41:46 +11:00
|
|
|
for (size_t y = 0; y < height; ++y)
|
|
|
|
for (size_t x = 0; x < width; ++x) {
|
2012-05-17 14:37:56 +10:00
|
|
|
double v = p.sample (x / double (width), y / double (height));
|
2012-05-17 14:18:39 +10:00
|
|
|
pixels[x + y * width] = v * std::numeric_limits<uint8_t>::max ();
|
2011-11-12 20:41:46 +11:00
|
|
|
}
|
|
|
|
}
|