A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PerlinNoise.hpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //----------------------------------------------------------------------------------------
  2. //
  3. // siv::PerlinNoise
  4. // Perlin noise library for modern C++
  5. //
  6. // Copyright (C) 2013-2018 Ryo Suzuki <reputeless@gmail.com>
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files(the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions :
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. //----------------------------------------------------------------------------------------
  27. # pragma once
  28. # include <cstdint>
  29. # include <numeric>
  30. # include <algorithm>
  31. # include <random>
  32. namespace siv
  33. {
  34. class PerlinNoise
  35. {
  36. private:
  37. std::uint8_t p[512];
  38. static double Fade(double t) noexcept
  39. {
  40. return t * t * t * (t * (t * 6 - 15) + 10);
  41. }
  42. static double Lerp(double t, double a, double b) noexcept
  43. {
  44. return a + t * (b - a);
  45. }
  46. static double Grad(std::uint8_t hash, double x, double y, double z) noexcept
  47. {
  48. const std::uint8_t h = hash & 15;
  49. const double u = h < 8 ? x : y;
  50. const double v = h < 4 ? y : h == 12 || h == 14 ? x : z;
  51. return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
  52. }
  53. public:
  54. explicit PerlinNoise(std::uint32_t seed = std::default_random_engine::default_seed)
  55. {
  56. reseed(seed);
  57. }
  58. template <class URNG>
  59. explicit PerlinNoise(URNG& urng)
  60. {
  61. reseed(urng);
  62. }
  63. void reseed(std::uint32_t seed)
  64. {
  65. for (size_t i = 0; i < 256; ++i)
  66. {
  67. p[i] = static_cast<std::uint8_t>(i);
  68. }
  69. std::shuffle(std::begin(p), std::begin(p) + 256, std::default_random_engine(seed));
  70. for (size_t i = 0; i < 256; ++i)
  71. {
  72. p[256 + i] = p[i];
  73. }
  74. }
  75. template <class URNG>
  76. void reseed(URNG& urng)
  77. {
  78. for (size_t i = 0; i < 256; ++i)
  79. {
  80. p[i] = static_cast<std::uint8_t>(i);
  81. }
  82. std::shuffle(std::begin(p), std::begin(p) + 256, urng);
  83. for (size_t i = 0; i < 256; ++i)
  84. {
  85. p[256 + i] = p[i];
  86. }
  87. }
  88. double noise(double x) const
  89. {
  90. return noise(x, 0.0, 0.0);
  91. }
  92. double noise(double x, double y) const
  93. {
  94. return noise(x, y, 0.0);
  95. }
  96. double noise(double x, double y, double z) const
  97. {
  98. const std::int32_t X = static_cast<std::int32_t>(std::floor(x)) & 255;
  99. const std::int32_t Y = static_cast<std::int32_t>(std::floor(y)) & 255;
  100. const std::int32_t Z = static_cast<std::int32_t>(std::floor(z)) & 255;
  101. x -= std::floor(x);
  102. y -= std::floor(y);
  103. z -= std::floor(z);
  104. const double u = Fade(x);
  105. const double v = Fade(y);
  106. const double w = Fade(z);
  107. const std::int32_t A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z;
  108. const std::int32_t B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z;
  109. return Lerp(w, Lerp(v, Lerp(u, Grad(p[AA], x, y, z),
  110. Grad(p[BA], x - 1, y, z)),
  111. Lerp(u, Grad(p[AB], x, y - 1, z),
  112. Grad(p[BB], x - 1, y - 1, z))),
  113. Lerp(v, Lerp(u, Grad(p[AA + 1], x, y, z - 1),
  114. Grad(p[BA + 1], x - 1, y, z - 1)),
  115. Lerp(u, Grad(p[AB + 1], x, y - 1, z - 1),
  116. Grad(p[BB + 1], x - 1, y - 1, z - 1))));
  117. }
  118. double octaveNoise(double x, std::int32_t octaves) const
  119. {
  120. double result = 0.0;
  121. double amp = 1.0;
  122. for (std::int32_t i = 0; i < octaves; ++i)
  123. {
  124. result += noise(x) * amp;
  125. x *= 2.0;
  126. amp *= 0.5;
  127. }
  128. return result;
  129. }
  130. double octaveNoise(double x, double y, std::int32_t octaves) const
  131. {
  132. double result = 0.0;
  133. double amp = 1.0;
  134. for (std::int32_t i = 0; i < octaves; ++i)
  135. {
  136. result += noise(x, y) * amp;
  137. x *= 2.0;
  138. y *= 2.0;
  139. amp *= 0.5;
  140. }
  141. return result;
  142. }
  143. double octaveNoise(double x, double y, double z, std::int32_t octaves) const
  144. {
  145. double result = 0.0;
  146. double amp = 1.0;
  147. for (std::int32_t i = 0; i < octaves; ++i)
  148. {
  149. result += noise(x, y, z) * amp;
  150. x *= 2.0;
  151. y *= 2.0;
  152. z *= 2.0;
  153. amp *= 0.5;
  154. }
  155. return result;
  156. }
  157. double noise0_1(double x) const
  158. {
  159. return noise(x) * 0.5 + 0.5;
  160. }
  161. double noise0_1(double x, double y) const
  162. {
  163. return noise(x, y) * 0.5 + 0.5;
  164. }
  165. double noise0_1(double x, double y, double z) const
  166. {
  167. return noise(x, y, z) * 0.5 + 0.5;
  168. }
  169. double octaveNoise0_1(double x, std::int32_t octaves) const
  170. {
  171. return octaveNoise(x, octaves) * 0.5 + 0.5;
  172. }
  173. double octaveNoise0_1(double x, double y, std::int32_t octaves) const
  174. {
  175. return octaveNoise(x, y, octaves) * 0.5 + 0.5;
  176. }
  177. double octaveNoise0_1(double x, double y, double z, std::int32_t octaves) const
  178. {
  179. return octaveNoise(x, y, z, octaves) * 0.5 + 0.5;
  180. }
  181. };
  182. }