noise.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Minetest
  3. * Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. * Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification, are
  8. * permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright notice, this list of
  10. * conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  12. * of conditions and the following disclaimer in the documentation and/or other materials
  13. * provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  23. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #pragma once
  26. #include "irr_v3d.h"
  27. #include "exceptions.h"
  28. #include "util/string.h"
  29. #if defined(RANDOM_MIN)
  30. #undef RANDOM_MIN
  31. #endif
  32. #if defined(RANDOM_MAX)
  33. #undef RANDOM_MAX
  34. #endif
  35. extern FlagDesc flagdesc_noiseparams[];
  36. // Note: this class is not polymorphic so that its high level of
  37. // optimizability may be preserved in the common use case
  38. class PseudoRandom {
  39. public:
  40. const static u32 RANDOM_RANGE = 32767;
  41. inline PseudoRandom(s32 seed_=0)
  42. {
  43. seed(seed_);
  44. }
  45. inline void seed(s32 seed)
  46. {
  47. m_next = seed;
  48. }
  49. inline u32 next()
  50. {
  51. m_next = static_cast<u32>(m_next) * 1103515245U + 12345U;
  52. // Signed division is required due to backwards compatibility
  53. return static_cast<u32>(m_next / 65536) % (RANDOM_RANGE + 1U);
  54. }
  55. inline s32 range(s32 min, s32 max)
  56. {
  57. if (max < min)
  58. throw PrngException("Invalid range (max < min)");
  59. /*
  60. Here, we ensure the range is not too large relative to RANDOM_MAX,
  61. as otherwise the effects of bias would become noticeable. Unlike
  62. PcgRandom, we cannot modify this RNG's range as it would change the
  63. output of this RNG for reverse compatibility.
  64. */
  65. if (static_cast<u32>(max - min) > (RANDOM_RANGE + 1) / 5)
  66. throw PrngException("Range too large");
  67. return (next() % (max - min + 1)) + min;
  68. }
  69. // Allow save and restore of state
  70. inline s32 getState() const
  71. {
  72. return m_next;
  73. }
  74. private:
  75. s32 m_next;
  76. };
  77. class PcgRandom {
  78. public:
  79. const static s32 RANDOM_MIN = -0x7fffffff - 1;
  80. const static s32 RANDOM_MAX = 0x7fffffff;
  81. const static u32 RANDOM_RANGE = 0xffffffff;
  82. PcgRandom(u64 state=0x853c49e6748fea9bULL, u64 seq=0xda3e39cb94b95bdbULL);
  83. void seed(u64 state, u64 seq=0xda3e39cb94b95bdbULL);
  84. u32 next();
  85. u32 range(u32 bound);
  86. s32 range(s32 min, s32 max);
  87. void bytes(void *out, size_t len);
  88. s32 randNormalDist(s32 min, s32 max, int num_trials=6);
  89. // Allow save and restore of state
  90. void getState(u64 state[2]) const;
  91. void setState(const u64 state[2]);
  92. private:
  93. u64 m_state;
  94. u64 m_inc;
  95. };
  96. #define NOISE_FLAG_DEFAULTS 0x01
  97. #define NOISE_FLAG_EASED 0x02
  98. #define NOISE_FLAG_ABSVALUE 0x04
  99. //// TODO(hmmmm): implement these!
  100. #define NOISE_FLAG_POINTBUFFER 0x08
  101. #define NOISE_FLAG_SIMPLEX 0x10
  102. struct NoiseParams {
  103. float offset = 0.0f;
  104. float scale = 1.0f;
  105. v3f spread = v3f(250, 250, 250);
  106. s32 seed = 12345;
  107. u16 octaves = 3;
  108. float persist = 0.6f;
  109. float lacunarity = 2.0f;
  110. u32 flags = NOISE_FLAG_DEFAULTS;
  111. NoiseParams() = default;
  112. NoiseParams(float offset_, float scale_, const v3f &spread_, s32 seed_,
  113. u16 octaves_, float persist_, float lacunarity_,
  114. u32 flags_=NOISE_FLAG_DEFAULTS)
  115. {
  116. offset = offset_;
  117. scale = scale_;
  118. spread = spread_;
  119. seed = seed_;
  120. octaves = octaves_;
  121. persist = persist_;
  122. lacunarity = lacunarity_;
  123. flags = flags_;
  124. }
  125. };
  126. class Noise {
  127. public:
  128. NoiseParams np;
  129. s32 seed;
  130. u32 sx;
  131. u32 sy;
  132. u32 sz;
  133. float *noise_buf = nullptr;
  134. float *gradient_buf = nullptr;
  135. float *persist_buf = nullptr;
  136. float *result = nullptr;
  137. Noise(const NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1);
  138. ~Noise();
  139. void setSize(u32 sx, u32 sy, u32 sz=1);
  140. void setSpreadFactor(v3f spread);
  141. void setOctaves(int octaves);
  142. void gradientMap2D(
  143. float x, float y,
  144. float step_x, float step_y,
  145. s32 seed);
  146. void gradientMap3D(
  147. float x, float y, float z,
  148. float step_x, float step_y, float step_z,
  149. s32 seed);
  150. float *perlinMap2D(float x, float y, float *persistence_map=NULL);
  151. float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
  152. inline float *perlinMap2D_PO(float x, float xoff, float y, float yoff,
  153. float *persistence_map=NULL)
  154. {
  155. return perlinMap2D(
  156. x + xoff * np.spread.X,
  157. y + yoff * np.spread.Y,
  158. persistence_map);
  159. }
  160. inline float *perlinMap3D_PO(float x, float xoff, float y, float yoff,
  161. float z, float zoff, float *persistence_map=NULL)
  162. {
  163. return perlinMap3D(
  164. x + xoff * np.spread.X,
  165. y + yoff * np.spread.Y,
  166. z + zoff * np.spread.Z,
  167. persistence_map);
  168. }
  169. private:
  170. void allocBuffers();
  171. void resizeNoiseBuf(bool is3d);
  172. void updateResults(float g, float *gmap, const float *persistence_map,
  173. size_t bufsize);
  174. };
  175. float NoisePerlin2D(const NoiseParams *np, float x, float y, s32 seed);
  176. float NoisePerlin3D(const NoiseParams *np, float x, float y, float z, s32 seed);
  177. inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
  178. float y, float yoff, s32 seed)
  179. {
  180. return NoisePerlin2D(np,
  181. x + xoff * np->spread.X,
  182. y + yoff * np->spread.Y,
  183. seed);
  184. }
  185. inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
  186. float y, float yoff, float z, float zoff, s32 seed)
  187. {
  188. return NoisePerlin3D(np,
  189. x + xoff * np->spread.X,
  190. y + yoff * np->spread.Y,
  191. z + zoff * np->spread.Z,
  192. seed);
  193. }
  194. // Return value: -1 ... 1
  195. float noise2d(int x, int y, s32 seed);
  196. float noise3d(int x, int y, int z, s32 seed);
  197. float noise2d_gradient(float x, float y, s32 seed, bool eased=true);
  198. float noise3d_gradient(float x, float y, float z, s32 seed, bool eased=false);
  199. float noise2d_perlin(float x, float y, s32 seed,
  200. int octaves, float persistence, bool eased=true);
  201. inline float easeCurve(float t)
  202. {
  203. return t * t * t * (t * (6.f * t - 15.f) + 10.f);
  204. }
  205. float contour(float v);