2015-05-18 14:11:08 +10:00
|
|
|
#include "image.hpp"
|
|
|
|
#include "noise.hpp"
|
2015-06-01 16:20:50 +10:00
|
|
|
|
|
|
|
#include "noise/fractal/fbm.hpp"
|
|
|
|
#include "noise/fractal/hetero.hpp"
|
2015-06-11 19:34:35 +10:00
|
|
|
#include "noise/fractal/hmf.hpp"
|
|
|
|
#include "noise/fractal/rmf.hpp"
|
|
|
|
#include "noise/fractal/runtime.hpp"
|
2015-05-18 14:42:28 +10:00
|
|
|
#include "noise/lerp.hpp"
|
2015-05-29 15:55:36 +10:00
|
|
|
#include "noise/basis/constant.hpp"
|
2015-05-28 10:56:06 +10:00
|
|
|
#include "noise/basis/value.hpp"
|
2015-07-24 01:37:36 +10:00
|
|
|
#include "noise/basis/patch.hpp"
|
2015-05-28 10:56:06 +10:00
|
|
|
#include "noise/basis/perlin.hpp"
|
|
|
|
#include "noise/basis/worley.hpp"
|
2015-06-02 20:31:25 +10:00
|
|
|
#include "noise/turbulence.hpp"
|
2015-06-11 19:34:35 +10:00
|
|
|
#include "noise/basis/runtime.hpp"
|
2015-07-01 01:03:34 +10:00
|
|
|
#include "noise/midpoint.hpp"
|
2015-05-26 16:37:41 +10:00
|
|
|
#include "extent.hpp"
|
2015-06-02 23:00:12 +10:00
|
|
|
#include "colour.hpp"
|
2015-06-03 23:24:26 +10:00
|
|
|
#include "netpbm.hpp"
|
2015-06-04 14:40:47 +10:00
|
|
|
#include "types.hpp"
|
2015-06-11 19:34:35 +10:00
|
|
|
#include "cmdopt.hpp"
|
2015-09-25 15:16:15 +10:00
|
|
|
#include "hash.hpp"
|
2015-06-30 22:23:57 +10:00
|
|
|
#include "region.hpp"
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-06-01 18:57:00 +10:00
|
|
|
template struct util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::cubic>>;
|
|
|
|
template struct util::noise::fractal::hmf<float, util::noise::basis::value<float,util::lerp::cubic>>;
|
|
|
|
template struct util::noise::fractal::rmf<float, util::noise::basis::constant<float>>;
|
|
|
|
template struct util::noise::fractal::hetero<float, util::noise::basis::worley<float,2>>;
|
|
|
|
|
2015-06-04 14:40:47 +10:00
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
enum basis_t {
|
|
|
|
VALUE,
|
|
|
|
PERLIN,
|
2015-07-24 01:37:36 +10:00
|
|
|
WORLEY,
|
|
|
|
PATCH
|
2015-06-11 19:34:35 +10:00
|
|
|
};
|
2015-06-04 20:06:53 +10:00
|
|
|
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
enum fractal_t {
|
|
|
|
FBM,
|
|
|
|
HMF,
|
|
|
|
RMF,
|
|
|
|
HETERO,
|
|
|
|
};
|
2015-06-04 17:16:46 +10:00
|
|
|
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
enum lerp_t {
|
|
|
|
LINEAR,
|
|
|
|
CUBIC,
|
|
|
|
QUINTIC,
|
|
|
|
COSINE,
|
|
|
|
TRUNC
|
|
|
|
};
|
2015-06-04 17:16:46 +10:00
|
|
|
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::istream&
|
|
|
|
operator>> (std::istream &is, basis_t &b)
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
is >> name;
|
2015-06-04 17:16:46 +10:00
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
b = name == "value" ? VALUE :
|
|
|
|
name == "perlin" ? PERLIN :
|
|
|
|
name == "worley" ? WORLEY :
|
2015-07-24 01:37:36 +10:00
|
|
|
name == "patch" ? PATCH :
|
2015-06-11 19:34:35 +10:00
|
|
|
(is.setstate (std::istream::failbit), b);
|
2015-06-04 17:16:46 +10:00
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
return is;
|
2015-06-04 17:16:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::ostream&
|
|
|
|
operator<< (std::ostream &os, basis_t b)
|
|
|
|
{
|
|
|
|
switch (b) {
|
2015-07-24 01:37:36 +10:00
|
|
|
case VALUE: os << "value"; return os;
|
|
|
|
case PERLIN: os << "perlin"; return os;
|
|
|
|
case WORLEY: os << "worley"; return os;
|
|
|
|
case PATCH: os << "patch"; return os;
|
2015-06-11 19:34:35 +10:00
|
|
|
|
|
|
|
default:
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
}
|
2015-06-04 14:40:47 +10:00
|
|
|
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::istream&
|
|
|
|
operator>> (std::istream &is, fractal_t &f)
|
2015-06-04 14:40:47 +10:00
|
|
|
{
|
2015-06-11 19:34:35 +10:00
|
|
|
std::string name;
|
|
|
|
is >> name;
|
2015-06-04 14:40:47 +10:00
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
f = name == "fbm" ? FBM :
|
|
|
|
name == "hmf" ? HMF :
|
|
|
|
name == "rmf" ? RMF :
|
|
|
|
name == "hetero" ? HETERO :
|
|
|
|
(is.setstate (std::istream::failbit), f);
|
2015-06-04 14:40:47 +10:00
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
return is;
|
2015-06-04 14:40:47 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::ostream&
|
|
|
|
operator<< (std::ostream &os, fractal_t f)
|
2015-06-04 15:13:58 +10:00
|
|
|
{
|
2015-06-11 19:34:35 +10:00
|
|
|
switch (f) {
|
|
|
|
case FBM: os << "fbm"; return os;
|
|
|
|
case HMF: os << "hmf"; return os;
|
|
|
|
case RMF: os << "rmf"; return os;
|
|
|
|
case HETERO: os << "hetero"; return os;
|
|
|
|
|
|
|
|
default:
|
|
|
|
unreachable ();
|
|
|
|
};
|
|
|
|
}
|
2015-06-04 15:13:58 +10:00
|
|
|
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::istream&
|
|
|
|
operator>> (std::istream &is, lerp_t &l)
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
is >> name;
|
2015-06-04 15:13:58 +10:00
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
l = name == "linear" ? LINEAR :
|
|
|
|
name == "cubic" ? CUBIC :
|
|
|
|
name == "quintic" ? QUINTIC :
|
|
|
|
name == "cosine" ? COSINE :
|
|
|
|
name == "trunc" ? TRUNC :
|
|
|
|
(is.setstate (std::istream::failbit), l);
|
2015-06-04 15:13:58 +10:00
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
return is;
|
2015-06-04 15:13:58 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::ostream&
|
|
|
|
operator<< (std::ostream &os, lerp_t &l)
|
|
|
|
{
|
|
|
|
switch (l) {
|
|
|
|
case LINEAR: os << "linear"; return os;
|
|
|
|
case CUBIC: os << "cubic"; return os;
|
|
|
|
case QUINTIC: os << "quintic"; return os;
|
|
|
|
case COSINE: os << "cosine"; return os;
|
|
|
|
case TRUNC: os << "trunc"; return os;
|
|
|
|
|
|
|
|
default:
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
}
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2015-06-04 17:16:46 +10:00
|
|
|
|
2015-07-01 01:03:34 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
2015-07-01 00:15:49 +10:00
|
|
|
{
|
2015-06-10 21:28:52 +10:00
|
|
|
// setup default variables
|
2015-06-03 23:24:26 +10:00
|
|
|
#ifdef ENABLE_DEBUGGING
|
2015-06-10 21:28:52 +10:00
|
|
|
util::extent2u res {320, 240};
|
2015-06-03 17:37:42 +10:00
|
|
|
#else
|
2015-06-10 21:28:52 +10:00
|
|
|
util::extent2u res {1920, 1080};
|
2015-06-03 17:37:42 +10:00
|
|
|
#endif
|
2015-05-18 14:11:08 +10:00
|
|
|
|
2015-09-25 15:16:15 +10:00
|
|
|
srand (time (nullptr));
|
2015-06-02 16:13:12 +10:00
|
|
|
uint64_t seed = time (nullptr);
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
basis_t basis = PERLIN;
|
|
|
|
fractal_t fractal = FBM;
|
|
|
|
lerp_t lerp = QUINTIC;
|
|
|
|
unsigned octaves = 8;
|
2015-08-28 21:09:21 +10:00
|
|
|
float H = std::numeric_limits<float>::quiet_NaN ();
|
|
|
|
float lacunarity = std::numeric_limits<float>::quiet_NaN ();
|
|
|
|
float amplitude = std::numeric_limits<float>::quiet_NaN ();
|
|
|
|
float gain = std::numeric_limits<float>::quiet_NaN ();
|
|
|
|
float offset = std::numeric_limits<float>::quiet_NaN ();
|
2015-06-11 19:34:35 +10:00
|
|
|
float scale = 1.f;
|
|
|
|
float turbulence = 0.f;
|
2015-06-15 17:48:49 +10:00
|
|
|
unsigned single = 0;
|
2015-07-28 18:14:10 +10:00
|
|
|
float width = 0;
|
2015-06-11 19:34:35 +10:00
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
// fill variables from arguments
|
|
|
|
util::cmdopt::parser args;
|
2015-07-28 18:14:10 +10:00
|
|
|
args.add<util::cmdopt::option::value<size_t>> ('w', "width", "output image width", res.w);
|
|
|
|
args.add<util::cmdopt::option::value<size_t>> ('h', "height", "output image height", res.h);
|
|
|
|
args.add<util::cmdopt::option::value<uint64_t>> ('s', "seed", "random seed", seed);
|
|
|
|
args.add<util::cmdopt::option::value<basis_t>> ('b', "basis", "primary basis function", basis);
|
|
|
|
args.add<util::cmdopt::option::value<fractal_t>> ('f', "fractal", "primary fractal function", fractal);
|
|
|
|
args.add<util::cmdopt::option::value<lerp_t>> ('l', "lerp", "interpolation algorithm", lerp);
|
|
|
|
args.add<util::cmdopt::option::value<unsigned>> ('o', "octaves", "total fractal iterations", octaves);
|
|
|
|
args.add<util::cmdopt::option::count<unsigned>> ('1', "single", "single octave", single);
|
|
|
|
args.add<util::cmdopt::option::value<float>> ('H', "hurst", "Hurst exponent", H);
|
2015-08-28 21:09:21 +10:00
|
|
|
args.add<util::cmdopt::option::value<float>> ('G', "gain", "octave gain", gain);
|
|
|
|
args.add<util::cmdopt::option::value<float>> ('A', "amplitude", "base amplitude", amplitude);
|
|
|
|
args.add<util::cmdopt::option::value<float>> ('L', "lacunarity", "frequency multiplier", lacunarity);
|
2015-07-28 18:14:10 +10:00
|
|
|
args.add<util::cmdopt::option::value<float>> ('x', "scale", "frequency multiplier", scale);
|
2015-08-28 21:09:21 +10:00
|
|
|
args.add<util::cmdopt::option::value<float>> ('O', "offset", "hetero offset", offset);
|
2015-07-28 18:14:10 +10:00
|
|
|
args.add<util::cmdopt::option::value<float>> ('t', "turbulence", "turbulence scale", turbulence);
|
|
|
|
args.add<util::cmdopt::option::value<float>> ('W', "patch-width", "patch blur width", width);
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
args.scan (argc, argv);
|
|
|
|
|
2015-09-11 19:55:52 +10:00
|
|
|
#if !defined(ENABLE_DEBUGGING) and !defined(PLATFORM_WIN32)
|
|
|
|
if (isatty (fileno (stdout))) {
|
|
|
|
std::cerr << "cowardly refusing to dump binary data to console\n";
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-02 20:31:25 +10:00
|
|
|
util::noise::turbulence<
|
|
|
|
float,
|
2015-06-11 19:34:35 +10:00
|
|
|
util::noise::fractal::runtime<
|
|
|
|
float,
|
|
|
|
util::noise::basis::runtime<float>
|
|
|
|
>,
|
|
|
|
util::noise::fractal::fbm<
|
|
|
|
float,
|
|
|
|
util::noise::basis::perlin<
|
|
|
|
float,
|
|
|
|
util::lerp::cubic
|
|
|
|
>
|
|
|
|
>
|
|
|
|
> t (seed, { turbulence, turbulence });
|
|
|
|
|
|
|
|
auto &f = t.data;
|
|
|
|
|
|
|
|
switch (fractal) {
|
|
|
|
using namespace util::noise;
|
|
|
|
|
|
|
|
case FBM: f.reset<fractal::fbm <float,basis::runtime<float>>> (seed); break;
|
|
|
|
case HMF: f.reset<fractal::hmf <float,basis::runtime<float>>> (seed); break;
|
|
|
|
case RMF: f.reset<fractal::rmf <float,basis::runtime<float>>> (seed); break;
|
2015-08-28 21:09:21 +10:00
|
|
|
case HETERO: {
|
|
|
|
auto &child = f.reset<fractal::hetero<float,basis::runtime<float>>> (seed);
|
2015-09-09 19:44:51 +10:00
|
|
|
if (!std::isnan (offset))
|
2015-08-28 21:09:21 +10:00
|
|
|
child.offset (offset);
|
|
|
|
break;
|
|
|
|
}
|
2015-06-11 19:34:35 +10:00
|
|
|
|
|
|
|
default:
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &b = f.basis ();
|
|
|
|
switch (basis) {
|
|
|
|
using namespace util::noise;
|
|
|
|
|
|
|
|
case PERLIN: {
|
|
|
|
switch (lerp) {
|
|
|
|
case LINEAR: b.reset<basis::perlin<float,util::lerp::linear>> (seed); break;
|
|
|
|
case CUBIC: b.reset<basis::perlin<float,util::lerp::cubic>> (seed); break;
|
|
|
|
case QUINTIC: b.reset<basis::perlin<float,util::lerp::quintic>> (seed); break;
|
|
|
|
case COSINE: b.reset<basis::perlin<float,util::lerp::cosine>> (seed); break;
|
|
|
|
case TRUNC: b.reset<basis::perlin<float,util::lerp::trunc>> (seed); break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case VALUE: {
|
|
|
|
switch (lerp) {
|
|
|
|
case LINEAR: b.reset<basis::value<float,util::lerp::linear>> (seed); break;
|
|
|
|
case CUBIC: b.reset<basis::value<float,util::lerp::cubic>> (seed); break;
|
|
|
|
case QUINTIC: b.reset<basis::value<float,util::lerp::quintic>> (seed); break;
|
|
|
|
case COSINE: b.reset<basis::value<float,util::lerp::cosine>> (seed); break;
|
|
|
|
case TRUNC: b.reset<basis::value<float,util::lerp::trunc>> (seed); break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WORLEY: {
|
|
|
|
b.reset<util::noise::basis::worley<float>> (seed);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-24 01:37:36 +10:00
|
|
|
case PATCH: {
|
2015-07-28 18:14:10 +10:00
|
|
|
b.reset<util::noise::basis::patch<float>> (seed, width);
|
2015-07-24 01:37:36 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
default:
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
t.seed (seed);
|
|
|
|
f.octaves (octaves);
|
|
|
|
f.frequency (scale / res.w);
|
2015-08-28 21:09:21 +10:00
|
|
|
if (!std::isnan (H)) f.H (H);
|
|
|
|
if (!std::isnan (lacunarity)) f.lacunarity (lacunarity);
|
|
|
|
if (!std::isnan (amplitude)) f.amplitude (amplitude);
|
|
|
|
if (!std::isnan (gain)) f.gain (gain);
|
|
|
|
|
|
|
|
|
2015-06-11 19:34:35 +10:00
|
|
|
t.perturb[0].frequency ( scale / res.w);
|
|
|
|
t.perturb[1].frequency ( scale / res.w);
|
|
|
|
|
|
|
|
util::image::buffer<float> img (res);
|
2015-05-18 14:11:08 +10:00
|
|
|
|
2015-06-15 17:48:49 +10:00
|
|
|
// XXX: offset slightly to avoid origin artefacts in some basis functions
|
2015-09-25 15:16:15 +10:00
|
|
|
const auto OFFSET = util::vector2f {
|
|
|
|
(util::hash::mix ( seed) & 0xFFFF) / float (0xFFFF),
|
|
|
|
(util::hash::mix (util::hash::mix (seed)) & 0xFFFF) / float (0xFFFF)
|
|
|
|
} / f.frequency ();
|
2015-06-15 17:48:49 +10:00
|
|
|
|
2015-05-29 15:55:36 +10:00
|
|
|
{
|
2015-06-15 17:48:49 +10:00
|
|
|
for (size_t y = 0; y < res.h; ++y)
|
|
|
|
for (size_t x = 0; x < res.w; ++x)
|
2015-09-25 15:16:15 +10:00
|
|
|
img[{x, y}] = t (util::point2f {float (x), float (y)} + OFFSET);
|
2015-06-15 17:48:49 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// working on the assumption that all octave images are based on summation,
|
|
|
|
// subtract the image with one less octave from our current image to leave
|
|
|
|
// us with the highest octave contribution only. this is hideously
|
|
|
|
// inefficient, but it's not an operation we care about in general.
|
|
|
|
if (single && f.octaves () != 1) {
|
|
|
|
auto oldoctaves = f.octaves ();
|
2015-06-17 03:09:22 +10:00
|
|
|
f.octaves (oldoctaves - 1);
|
2015-06-15 17:48:49 +10:00
|
|
|
auto prev = img.clone ();
|
2015-05-29 15:55:36 +10:00
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
for (size_t y = 0; y < res.h; ++y)
|
2015-06-11 19:34:35 +10:00
|
|
|
for (size_t x = 0; x < res.w; ++x)
|
2015-09-25 15:16:15 +10:00
|
|
|
prev[{x,y}] = t (util::point2f {float (x), float (y)} + OFFSET);
|
2015-06-15 17:48:49 +10:00
|
|
|
|
|
|
|
CHECK_EQ (img.stride (), prev.stride ());
|
|
|
|
for (size_t i = 0; i < img.size (); ++i)
|
|
|
|
img[i] -= prev[i];
|
|
|
|
|
|
|
|
f.octaves (oldoctaves);
|
2015-05-29 15:55:36 +10:00
|
|
|
}
|
2015-05-18 14:11:08 +10:00
|
|
|
|
2015-05-26 16:37:41 +10:00
|
|
|
// rescale into the range [0, 1]
|
|
|
|
auto range = std::minmax_element (img.begin (), img.end ());
|
2015-08-28 21:09:21 +10:00
|
|
|
auto inc = *range.first;
|
|
|
|
auto div = *range.second - *range.first;
|
2015-05-26 16:37:41 +10:00
|
|
|
|
2015-07-28 18:14:10 +10:00
|
|
|
std::cerr << '[' << *range.first << ',' << *range.second << "]\n";
|
2015-08-28 21:09:21 +10:00
|
|
|
std::transform (img.begin (), img.end (), img.begin (), [inc,div] (auto i) { return (i - inc) / div; });
|
2015-06-01 17:21:51 +10:00
|
|
|
|
2015-06-04 14:40:47 +10:00
|
|
|
// write the images to disk
|
2015-06-11 19:34:35 +10:00
|
|
|
util::pgm::write (img.cast<uint8_t> (), std::cout);
|
2015-05-18 14:11:08 +10:00
|
|
|
}
|