X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnoise.hpp;h=3c4609e566a041d838a626096ca0c849bad8fd53;hb=7554debb33158b1507c13f9d6fb5969345a570cd;hp=c081fc6a64f842dc45042385f5a4c761da219380;hpb=e70967c971f77a4ac0f5c074e6eb94bdd0e2b7ab;p=blank.git diff --git a/src/noise.hpp b/src/noise.hpp index c081fc6..3c4609e 100644 --- a/src/noise.hpp +++ b/src/noise.hpp @@ -1,29 +1,99 @@ #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) noexcept; + + // get the next bit + bool operator ()() noexcept; + + template + void operator ()(T &out) noexcept { + 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 { public: - explicit SimplexNoise(unsigned int seed); + explicit SimplexNoise(unsigned int seed) noexcept; - float operator ()(const glm::vec3 &) const; + float operator ()(const glm::vec3 &) const noexcept; private: - unsigned char Perm(size_t idx) const; - const glm::vec3 &Grad(size_t idx) const; + unsigned char Perm(size_t idx) const noexcept; + unsigned char Perm12(size_t idx) const noexcept; + const glm::vec3 &Grad(unsigned char idx) const noexcept; private: unsigned char perm[512]; + unsigned char perm12[512]; glm::vec3 grad[12]; }; + +/// implementation of Worley noise (aka Cell or Voroni noise) +class WorleyNoise { + +public: + explicit WorleyNoise(unsigned int seed) noexcept; + + float operator ()(const glm::vec3 &) const noexcept; + +private: + const unsigned int seed; + const int num_points; + +}; + + +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