diff --git a/Source/Noise.cpp b/Source/Noise.cpp index 5138077..f31e335 100644 --- a/Source/Noise.cpp +++ b/Source/Noise.cpp @@ -49,49 +49,40 @@ static inline float lerp (float a0, float a1, float w) return (1.0f - w) * a0 + w * a1; } -float Noise::gen1 (float z, float torsion) +inline float gen1 (float *gradients, float z, float torsion) { - float g1, g2, d2; - float result = 0; + const int z1 = static_cast(z); + const float dz1 = z - z1; + const float dz2 = dz1 - 1.0f; - int z1 = (int) z; - int z2 = (z1 + 1); + float g1 = gradients[z1] + torsion; + float g2 = gradients[z1 + 1] + torsion; - float dz1 = z - z1; - float dz2 = dz1 - 1.0f; + float d1 = 1.0f - (dz1 * dz1); + d1 = d1 * d1 * d1 * d1; - g1 = gradients[z1]; - g2 = gradients[z2]; + float d2 = 1.0f - (dz2 * dz2); + d2 = d2 * d2 * d2 * d2; - g1 = g1 + torsion; if (g1 > 1.0f) { g1 = 2.0f - g1; } - if (g1 < -1.0f) { g1 = -2.0f - g1; } + else if (g1 < -1.0f) { g1 = -2.0f - g1; } - g2 = g2 + torsion; if (g2 > 1.0f) { g2 = 2.0f - g2; } - if (g2 < -1.0f) { g2 = -2.0f - g2; } - - d2 = 1.0 - (dz1 * dz1); - d2 = d2 * d2 * d2 * d2; - result += d2 * (g1 * dz1); - - d2 = 1.0 - (dz2 * dz2); - d2 = d2 * d2 * d2 * d2; - result += d2 * (g2 * dz2); + else if (g2 < -1.0f) { g2 = -2.0f - g2; } - return result * 3.33; // temp normalization value + return (d1 * (g1 * dz1) + d2 * (g2 * dz2)) * 3.33f; // temp normalization value } float Noise::gen (float z, int octaves, float persistence, float torsion, float warping) { float result = 0; float multiplier = 1.0f; - float offset = gen1 (z, torsion) * warping; + float offset = gen1 (gradients, z, torsion) * warping; for (int octave = 0; octave < octaves; ++octave) { - float t = fmod(torsion, 4); - float value = gen1 (z * (1 << octave) + offset, t) * multiplier; + float t = fmodf(torsion, 4); + float value = gen1 (gradients, z * (1 << octave) + offset, t) * multiplier; offset = value * warping; result += value; multiplier *= persistence; diff --git a/Source/Noise.h b/Source/Noise.h index 7576751..f3334f2 100644 --- a/Source/Noise.h +++ b/Source/Noise.h @@ -35,6 +35,4 @@ class Noise float gradients[65536]; std::mt19937 random; uint32_t seed_; - - float gen1 (float z, float torsion); };