From c877ddd21f402381d88a6bebdd5c7c0b4ac28ba9 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 23 Mar 2015 17:33:02 +0100 Subject: [PATCH] added Galois LFSR PRNG not in use yet beware of seed with many zeros especially 0, which will only ever produce zeroes --- TODO | 6 ++++++ src/noise.cpp | 18 ++++++++++++++++++ src/noise.hpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/TODO b/TODO index dfe8332..244e938 100644 --- a/TODO +++ b/TODO @@ -75,3 +75,9 @@ world generator that is not boring entity/world collision entities should be stopped from entering solid parts of the world + +better noise + + current simplex noise implementation repeats itself pretty quickly + also there seems to be a (imo) better implementation here: + http://flafla2.github.io/2014/08/09/perlinnoise.html diff --git a/src/noise.cpp b/src/noise.cpp index c86f833..6d59224 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -12,6 +12,24 @@ constexpr float one_sixth = 1.0f/6.0f; namespace blank { +GaloisLFSR::GaloisLFSR(std::uint64_t seed) +: state(seed) { + +} + +bool GaloisLFSR::operator ()() { + bool result = state & 1; + state >>= 1; + if (result) { + state |= 0x8000000000000000; + state ^= mask; + } else { + state &= 0x7FFFFFFFFFFFFFFF; + } + return result; +} + + SimplexNoise::SimplexNoise(unsigned int seed) : grad({ { 1.0f, 1.0f, 0.0f }, diff --git a/src/noise.hpp b/src/noise.hpp index 4c26de9..166a60a 100644 --- a/src/noise.hpp +++ b/src/noise.hpp @@ -1,11 +1,40 @@ #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 { -- 2.39.2