X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnoise.hpp;h=26e37668b00f609fdfd2747a2ed6a3e044031729;hb=38abfe4f5342f20b56052ac3090694eabf028d16;hp=4c26de92eadf0ae3bacd679cc069e0337bf27861;hpb=42dea80c2cffb75a5721d1cbf0cdd6277f4d520d;p=blank.git diff --git a/src/noise.hpp b/src/noise.hpp index 4c26de9..26e3766 100644 --- a/src/noise.hpp +++ b/src/noise.hpp @@ -1,11 +1,41 @@ #ifndef BLANK_NOISE_HPP_ #define BLANK_NOISE_HPP_ +#include +#include #include namespace blank { +class GaloisLFSR { + +public: + // seed should be non-zero + explicit GaloisLFSR(std::uint64_t seed); + + // get the next bit + bool operator ()(); + + template + void operator ()(T &out) { + constexpr int num_bits = + std::numeric_limits::digits + + std::numeric_limits::is_signed; + for (int i = 0; i < num_bits; ++i) { + operator ()(); + } + out = static_cast(state); + } + +private: + std::uint64_t state; + // bits 64, 63, 61, and 60 set to 1 (counting from 1 lo to hi) + static constexpr std::uint64_t mask = 0xD800000000000000; + +}; + + /// (3D only) adaptation of Stefan Gustavson's SimplexNoise java class class SimplexNoise { @@ -39,6 +69,29 @@ private: }; + +template +float OctaveNoise( + const Noise &noise, + const glm::vec3 &in, + int num, + float persistence, + float frequency = 1.0f, + float amplitude = 1.0f, + float growth = 2.0f +) { + float total = 0.0f; + float max = 0.0f; + for (int i = 0; i < num; ++i) { + total += noise(in * frequency) * amplitude; + max += amplitude; + amplitude *= persistence; + frequency *= growth; + } + + return total / max; +} + } #endif