noise: add turbulence (displacement) combiner

This commit is contained in:
Danny Robson 2015-06-02 20:31:25 +10:00
parent bc5888bfcf
commit 7dcba618c0
4 changed files with 116 additions and 2 deletions

View File

@ -159,6 +159,8 @@ UTIL_FILES = \
noise/lerp.hpp \ noise/lerp.hpp \
noise/lut.cpp \ noise/lut.cpp \
noise/lut.hpp \ noise/lut.hpp \
noise/turbulence.hpp \
noise/turbulence.ipp \
options.cpp \ options.cpp \
options.hpp \ options.hpp \
pascal.cpp \ pascal.cpp \

48
noise/turbulence.hpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#ifndef __UTIL_NOISE_TURBULENCE_HPP
#define __UTIL_NOISE_TURBULENCE_HPP
#include "../point.hpp"
#include "../vector.hpp"
#include <cstdint>
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 <typename T, typename D, typename P>
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

49
noise/turbulence.ipp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#ifdef __UTIL_NOISE_TURBULENCE_IPP
#error
#endif
#define __UTIL_NOISE_TURBULENCE_IPP
#include "../hash.hpp"
namespace util { namespace noise {
///////////////////////////////////////////////////////////////////////////
template <typename T, typename D, typename P>
turbulence<T,D,P>::turbulence (seed_t _seed,
vector<2,T> _scale):
data (_seed),
perturb { wang64 (_seed), wang64 (wang64 (_seed)) },
scale (_scale)
{ ; }
///////////////////////////////////////////////////////////////////////////
template <typename T, typename D, typename P>
constexpr T
turbulence<T,D,P>::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 ());
}
} }

View File

@ -10,6 +10,7 @@
#include "noise/basis/value.hpp" #include "noise/basis/value.hpp"
#include "noise/basis/perlin.hpp" #include "noise/basis/perlin.hpp"
#include "noise/basis/worley.hpp" #include "noise/basis/worley.hpp"
#include "noise/turbulence.hpp"
#include "extent.hpp" #include "extent.hpp"
@ -28,6 +29,7 @@ main (void)
uint64_t seed = time (nullptr); uint64_t seed = time (nullptr);
// setup the noise generator // setup the noise generator
#if 0
//util::noise::fractal::fbm<float, util::noise::basis::worley<float>> b (seed); //util::noise::fractal::fbm<float, util::noise::basis::worley<float>> b (seed);
//util::noise::fractal::rmf<float, util::noise::basis::worley<float>> b (seed); //util::noise::fractal::rmf<float, util::noise::basis::worley<float>> b (seed);
//util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::cubic>> b (seed); //util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::cubic>> b (seed);
@ -37,9 +39,22 @@ main (void)
b.octaves (8); b.octaves (8);
b.frequency (10.f / size.w); b.frequency (10.f / size.w);
//b.lacunarity = 2.f; b.lacunarity = 2.f;
//b.H = 1.0f; b.H = 1.0f;
b.seed (seed); b.seed (seed);
#else
util::noise::turbulence<
float,
util::noise::fractal::hetero<float, util::noise::basis::perlin<float,util::lerp::cubic>>,
util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::quintic>>
> 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 // generate the values. offset positions slightly to avoid simple axis issues with perlin basis
{ {