X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnoise.hpp;h=3c4609e566a041d838a626096ca0c849bad8fd53;hb=7554debb33158b1507c13f9d6fb5969345a570cd;hp=166a60a71058b998d60df732f752259b993beea4;hpb=c877ddd21f402381d88a6bebdd5c7c0b4ac28ba9;p=blank.git diff --git a/src/noise.hpp b/src/noise.hpp index 166a60a..3c4609e 100644 --- a/src/noise.hpp +++ b/src/noise.hpp @@ -12,13 +12,13 @@ class GaloisLFSR { public: // seed should be non-zero - explicit GaloisLFSR(std::uint64_t seed); + explicit GaloisLFSR(std::uint64_t seed) noexcept; // get the next bit - bool operator ()(); + bool operator ()() noexcept; template - void operator ()(T &out) { + void operator ()(T &out) noexcept { constexpr int num_bits = std::numeric_limits::digits + std::numeric_limits::is_signed; @@ -35,20 +35,23 @@ private: }; + /// (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]; }; @@ -58,9 +61,9 @@ private: class WorleyNoise { public: - explicit WorleyNoise(unsigned int seed); + explicit WorleyNoise(unsigned int seed) noexcept; - float operator ()(const glm::vec3 &) const; + float operator ()(const glm::vec3 &) const noexcept; private: const unsigned int seed; @@ -68,6 +71,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