From: Daniel Karbach Date: Wed, 11 Mar 2015 16:44:23 +0000 (+0100) Subject: minor optimization of noise generator X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=e70967c971f77a4ac0f5c074e6eb94bdd0e2b7ab;p=blank.git minor optimization of noise generator --- diff --git a/src/noise.cpp b/src/noise.cpp index d192b27..1e1c6d8 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -3,8 +3,14 @@ #include -namespace blank { +namespace { + +constexpr float one_third = 1.0f/3.0f; +constexpr float one_sixth = 1.0f/6.0f; +} + +namespace blank { SimplexNoise::SimplexNoise(unsigned int seed) : grad({ @@ -25,17 +31,18 @@ SimplexNoise::SimplexNoise(unsigned int seed) for (size_t i = 0; i < 256; ++i) { val = 2346765 * val + 6446345; perm[i] = val % 256; + perm[i + 256] = perm[i]; } } float SimplexNoise::operator ()(const glm::vec3 &in) const { - float skew = (in.x + in.y + in.z) / 3.0f; + float skew = (in.x + in.y + in.z) * one_third; - glm::vec3 skewed(std::floor(in.x + skew), std::floor(in.y + skew), std::floor(in.z + skew)); - float tr = (skewed.x + skewed.y + skewed.z) / 6.0f; + glm::vec3 skewed(glm::floor(in + skew)); + float tr = (skewed.x + skewed.y + skewed.z) * one_sixth; - glm::vec3 unskewed(skewed.x - tr, skewed.y - tr, skewed.z - tr); + glm::vec3 unskewed(skewed - tr); glm::vec3 offset[4]; offset[0] = in - unskewed; @@ -63,14 +70,14 @@ float SimplexNoise::operator ()(const glm::vec3 &in) const { third = { 1.0f, 1.0f, 0.0f }; } - offset[1] = offset[0] - second + glm::vec3(1.0f/6.0f); - offset[2] = offset[0] - third + glm::vec3(1.0f/3.0f); - offset[3] = offset[0] - glm::vec3(0.5f); + offset[1] = offset[0] - second + one_sixth; + offset[2] = offset[0] - third + one_third; + offset[3] = offset[0] - 0.5f; - size_t index[3] = { - unsigned(skewed.x) % 256, - unsigned(skewed.y) % 256, - unsigned(skewed.z) % 256, + unsigned char index[3] = { + (unsigned char)(skewed.x), + (unsigned char)(skewed.y), + (unsigned char)(skewed.z), }; size_t corner[4] = { Perm(index[0] + Perm(index[1] + Perm(index[2]))), @@ -95,7 +102,7 @@ float SimplexNoise::operator ()(const glm::vec3 &in) const { unsigned char SimplexNoise::Perm(size_t idx) const { - return perm[idx % 256]; + return perm[idx]; } const glm::vec3 &SimplexNoise::Grad(size_t idx) const { diff --git a/src/noise.hpp b/src/noise.hpp index 30bf527..c081fc6 100644 --- a/src/noise.hpp +++ b/src/noise.hpp @@ -19,7 +19,7 @@ private: const glm::vec3 &Grad(size_t idx) const; private: - unsigned char perm[256]; + unsigned char perm[512]; glm::vec3 grad[12]; };