noise.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. extern FlagDesc flagdesc_noiseparams[];
  30. // Note: this class is not polymorphic so that its high level of
  31. // optimizability may be preserved in the common use case
  32. class PseudoRandom {
  33. public:
  34. const static u32 RANDOM_RANGE = 32767;
  35. inline PseudoRandom(int seed=0):
  36. m_next(seed)
  37. {
  38. }
  39. inline void seed(int seed)
  40. {
  41. m_next = seed;
  42. }
  43. inline int next()
  44. {
  45. m_next = m_next * 1103515245 + 12345;
  46. return (unsigned)(m_next / 65536) % (RANDOM_RANGE + 1);
  47. }
  48. inline int range(int min, int max)
  49. {
  50. if (max < min)
  51. throw PrngException("Invalid range (max < min)");
  52. /*
  53. Here, we ensure the range is not too large relative to RANDOM_MAX,
  54. as otherwise the effects of bias would become noticable. Unlike
  55. PcgRandom, we cannot modify this RNG's range as it would change the
  56. output of this RNG for reverse compatibility.
  57. */
  58. if ((u32)(max - min) > (RANDOM_RANGE + 1) / 10)
  59. throw PrngException("Range too large");
  60. return (next() % (max - min + 1)) + min;
  61. }
  62. private:
  63. int m_next;
  64. };
  65. class PcgRandom {
  66. public:
  67. const static s32 RANDOM_MIN = -0x7fffffff - 1;
  68. const static s32 RANDOM_MAX = 0x7fffffff;
  69. const static u32 RANDOM_RANGE = 0xffffffff;
  70. PcgRandom(u64 state=0x853c49e6748fea9bULL, u64 seq=0xda3e39cb94b95bdbULL);
  71. void seed(u64 state, u64 seq=0xda3e39cb94b95bdbULL);
  72. u32 next();
  73. u32 range(u32 bound);
  74. s32 range(s32 min, s32 max);
  75. void bytes(void *out, size_t len);
  76. s32 randNormalDist(s32 min, s32 max, int num_trials=6);
  77. private:
  78. u64 m_state;
  79. u64 m_inc;
  80. };
  81. #define NOISE_FLAG_DEFAULTS 0x01
  82. #define NOISE_FLAG_EASED 0x02
  83. #define NOISE_FLAG_ABSVALUE 0x04
  84. //// TODO(hmmmm): implement these!
  85. #define NOISE_FLAG_POINTBUFFER 0x08
  86. #define NOISE_FLAG_SIMPLEX 0x10
  87. struct NoiseParams {
  88. float offset = 0.0f;
  89. float scale = 1.0f;
  90. v3f spread = v3f(250, 250, 250);
  91. s32 seed = 12345;
  92. u16 octaves = 3;
  93. float persist = 0.6f;
  94. float lacunarity = 2.0f;
  95. u32 flags = NOISE_FLAG_DEFAULTS;
  96. NoiseParams() = default;
  97. NoiseParams(float offset_, float scale_, const v3f &spread_, s32 seed_,
  98. u16 octaves_, float persist_, float lacunarity_,
  99. u32 flags_=NOISE_FLAG_DEFAULTS)
  100. {
  101. offset = offset_;
  102. scale = scale_;
  103. spread = spread_;
  104. seed = seed_;
  105. octaves = octaves_;
  106. persist = persist_;
  107. lacunarity = lacunarity_;
  108. flags = flags_;
  109. }
  110. };
  111. class Noise {
  112. public:
  113. NoiseParams np;
  114. s32 seed;
  115. u32 sx;
  116. u32 sy;
  117. u32 sz;
  118. float *noise_buf = nullptr;
  119. float *gradient_buf = nullptr;
  120. float *persist_buf = nullptr;
  121. float *result = nullptr;
  122. Noise(NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1);
  123. ~Noise();
  124. void setSize(u32 sx, u32 sy, u32 sz=1);
  125. void setSpreadFactor(v3f spread);
  126. void setOctaves(int octaves);
  127. void gradientMap2D(
  128. float x, float y,
  129. float step_x, float step_y,
  130. s32 seed);
  131. void gradientMap3D(
  132. float x, float y, float z,
  133. float step_x, float step_y, float step_z,
  134. s32 seed);
  135. float *perlinMap2D(float x, float y, float *persistence_map=NULL);
  136. float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
  137. inline float *perlinMap2D_PO(float x, float xoff, float y, float yoff,
  138. float *persistence_map=NULL)
  139. {
  140. return perlinMap2D(
  141. x + xoff * np.spread.X,
  142. y + yoff * np.spread.Y,
  143. persistence_map);
  144. }
  145. inline float *perlinMap3D_PO(float x, float xoff, float y, float yoff,
  146. float z, float zoff, float *persistence_map=NULL)
  147. {
  148. return perlinMap3D(
  149. x + xoff * np.spread.X,
  150. y + yoff * np.spread.Y,
  151. z + zoff * np.spread.Z,
  152. persistence_map);
  153. }
  154. private:
  155. void allocBuffers();
  156. void resizeNoiseBuf(bool is3d);
  157. void updateResults(float g, float *gmap, const float *persistence_map,
  158. size_t bufsize);
  159. };
  160. float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed);
  161. float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed);
  162. inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
  163. float y, float yoff, s32 seed)
  164. {
  165. return NoisePerlin2D(np,
  166. x + xoff * np->spread.X,
  167. y + yoff * np->spread.Y,
  168. seed);
  169. }
  170. inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
  171. float y, float yoff, float z, float zoff, s32 seed)
  172. {
  173. return NoisePerlin3D(np,
  174. x + xoff * np->spread.X,
  175. y + yoff * np->spread.Y,
  176. z + zoff * np->spread.Z,
  177. seed);
  178. }
  179. // Return value: -1 ... 1
  180. float noise2d(int x, int y, s32 seed);
  181. float noise3d(int x, int y, int z, s32 seed);
  182. float noise2d_gradient(float x, float y, s32 seed, bool eased=true);
  183. float noise3d_gradient(float x, float y, float z, s32 seed, bool eased=false);
  184. float noise2d_perlin(float x, float y, s32 seed,
  185. int octaves, float persistence, bool eased=true);
  186. float noise2d_perlin_abs(float x, float y, s32 seed,
  187. int octaves, float persistence, bool eased=true);
  188. float noise3d_perlin(float x, float y, float z, s32 seed,
  189. int octaves, float persistence, bool eased=false);
  190. float noise3d_perlin_abs(float x, float y, float z, s32 seed,
  191. int octaves, float persistence, bool eased=false);
  192. inline float easeCurve(float t)
  193. {
  194. return t * t * t * (t * (6.f * t - 15.f) + 10.f);
  195. }
  196. float contour(float v);