10float Value(uint32_t seed, int32_t x, int32_t z) {
12 return static_cast<float>(
Hash(seed, x, z) >> 8) * (2.0f / 16777215.0f) -
16float Smooth(
float t) {
return t * t * (3.0f - 2.0f * t); }
18float Mix(
float a,
float b,
float t) {
return a + (b - a) * t; }
22uint32_t
Hash(uint32_t seed, int32_t x, int32_t z) {
23 uint32_t hash = seed ^ (
static_cast<uint32_t
>(x) * 0x9E3779B1u) ^
24 (
static_cast<uint32_t
>(z) * 0x85EBCA77u);
34float Noise(uint32_t seed,
float x,
float z) {
35 const float left = std::floor(x);
36 const float top = std::floor(z);
37 const auto i =
static_cast<int32_t
>(left);
38 const auto j =
static_cast<int32_t
>(top);
39 const float across = Smooth(x - left);
40 const float down = Smooth(z - top);
41 const float upper =
Mix(Value(seed, i, j), Value(seed, i + 1, j), across);
43 Mix(Value(seed, i, j + 1), Value(seed, i + 1, j + 1), across);
44 return Mix(upper, lower, down);
47float Fractal(uint32_t seed,
float x,
float z,
int octaves) {
50 float strength = 1.0f;
52 for (
int octave = 0; octave < octaves; ++octave) {
53 sum += strength *
Noise(seed + octave, x * scale, z * scale);
float Fractal(uint32_t seed, float x, float z, int octaves)
uint32_t Hash(uint32_t seed, int32_t x, int32_t z)
float Noise(uint32_t seed, float x, float z)
Value noise: from -1 to 1, random at whole numbers, and smooth between them.
void Mix(Samples &mix, const Samples &sound, float at, float gain)