From 7dcba618c0dd0e601d68e56d14a5b181d8178db3 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 2 Jun 2015 20:31:25 +1000 Subject: [PATCH] noise: add turbulence (displacement) combiner --- Makefile.am | 2 ++ noise/turbulence.hpp | 48 +++++++++++++++++++++++++++++++++++++++++++ noise/turbulence.ipp | 49 ++++++++++++++++++++++++++++++++++++++++++++ tools/noise.cpp | 19 +++++++++++++++-- 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 noise/turbulence.hpp create mode 100644 noise/turbulence.ipp diff --git a/Makefile.am b/Makefile.am index bce0fd9c..7879dfa5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -159,6 +159,8 @@ UTIL_FILES = \ noise/lerp.hpp \ noise/lut.cpp \ noise/lut.hpp \ + noise/turbulence.hpp \ + noise/turbulence.ipp \ options.cpp \ options.hpp \ pascal.cpp \ diff --git a/noise/turbulence.hpp b/noise/turbulence.hpp new file mode 100644 index 00000000..f24057ce --- /dev/null +++ b/noise/turbulence.hpp @@ -0,0 +1,48 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * Copyright 2012-2015 Danny Robson + */ + +#ifndef __UTIL_NOISE_TURBULENCE_HPP +#define __UTIL_NOISE_TURBULENCE_HPP + +#include "../point.hpp" +#include "../vector.hpp" + +#include + +namespace util { namespace noise { + /// perturb the evaluation point of a noise function by the results of + /// a per-dimension noise function + /// + /// assumes the pertubation function is roughly symetrical around 0. + /// nothing will explode if it isn't, but you'll see strong directional + /// artefacts with higher scaling factors. + template + struct turbulence { + using seed_t = uint64_t; + turbulence (seed_t, vector<2,T> scale); + + constexpr T operator() (point<2,T>) const; + + D data; + P perturb[2]; + + vector<2,T> scale; + }; +} } + +#include "turbulence.ipp" + +#endif diff --git a/noise/turbulence.ipp b/noise/turbulence.ipp new file mode 100644 index 00000000..072a274b --- /dev/null +++ b/noise/turbulence.ipp @@ -0,0 +1,49 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * Copyright 2012-2015 Danny Robson + */ + +#ifdef __UTIL_NOISE_TURBULENCE_IPP +#error +#endif +#define __UTIL_NOISE_TURBULENCE_IPP + +#include "../hash.hpp" + + +namespace util { namespace noise { + /////////////////////////////////////////////////////////////////////////// + template + turbulence::turbulence (seed_t _seed, + vector<2,T> _scale): + data (_seed), + perturb { wang64 (_seed), wang64 (wang64 (_seed)) }, + scale (_scale) + { ; } + + + /////////////////////////////////////////////////////////////////////////// + template + constexpr T + turbulence::operator() (point<2,T> p) const + { + vector<2,T> n = { + perturb[0] (p), + perturb[1] (p) + }; + + // scale by the data frequency so that we match scale + return data (p + n * scale / data.frequency ()); + } +} } diff --git a/tools/noise.cpp b/tools/noise.cpp index 99e84e7a..969edb6b 100644 --- a/tools/noise.cpp +++ b/tools/noise.cpp @@ -10,6 +10,7 @@ #include "noise/basis/value.hpp" #include "noise/basis/perlin.hpp" #include "noise/basis/worley.hpp" +#include "noise/turbulence.hpp" #include "extent.hpp" @@ -28,6 +29,7 @@ main (void) uint64_t seed = time (nullptr); // setup the noise generator +#if 0 //util::noise::fractal::fbm> b (seed); //util::noise::fractal::rmf> b (seed); //util::noise::fractal::fbm> b (seed); @@ -37,9 +39,22 @@ main (void) b.octaves (8); b.frequency (10.f / size.w); - //b.lacunarity = 2.f; - //b.H = 1.0f; + b.lacunarity = 2.f; + b.H = 1.0f; b.seed (seed); +#else + util::noise::turbulence< + float, + util::noise::fractal::hetero>, + util::noise::fractal::fbm> + > b (seed, { 0.13f, 0.13f }); + + b.data.frequency (10.f / size.w); + b.perturb[0].octaves (4); + b.perturb[1].octaves (4); + b.perturb[0].frequency (10.f / size.w); + b.perturb[1].frequency (10.f / size.w); +#endif // generate the values. offset positions slightly to avoid simple axis issues with perlin basis {