Team Fortress 2 Source Code as on 22/4/2020
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.

316 lines
13 KiB

  1. // stb_perlin.h - v0.3 - perlin noise
  2. // public domain single-file C implementation by Sean Barrett
  3. //
  4. // LICENSE
  5. //
  6. // See end of file.
  7. //
  8. //
  9. // to create the implementation,
  10. // #define STB_PERLIN_IMPLEMENTATION
  11. // in *one* C/CPP file that includes this file.
  12. //
  13. //
  14. // Documentation:
  15. //
  16. // float stb_perlin_noise3( float x,
  17. // float y,
  18. // float z,
  19. // int x_wrap=0,
  20. // int y_wrap=0,
  21. // int z_wrap=0)
  22. //
  23. // This function computes a random value at the coordinate (x,y,z).
  24. // Adjacent random values are continuous but the noise fluctuates
  25. // its randomness with period 1, i.e. takes on wholly unrelated values
  26. // at integer points. Specifically, this implements Ken Perlin's
  27. // revised noise function from 2002.
  28. //
  29. // The "wrap" parameters can be used to create wraparound noise that
  30. // wraps at powers of two. The numbers MUST be powers of two. Specify
  31. // 0 to mean "don't care". (The noise always wraps every 256 due
  32. // details of the implementation, even if you ask for larger or no
  33. // wrapping.)
  34. //
  35. // Fractal Noise:
  36. //
  37. // Three common fractal noise functions are included, which produce
  38. // a wide variety of nice effects depending on the parameters
  39. // provided. Note that each function will call stb_perlin_noise3
  40. // 'octaves' times, so this parameter will affect runtime.
  41. //
  42. // float stb_perlin_ridge_noise3(float x, float y, float z,
  43. // float lacunarity, float gain, float offset, int octaves,
  44. // int x_wrap, int y_wrap, int z_wrap);
  45. //
  46. // float stb_perlin_fbm_noise3(float x, float y, float z,
  47. // float lacunarity, float gain, int octaves,
  48. // int x_wrap, int y_wrap, int z_wrap);
  49. //
  50. // float stb_perlin_turbulence_noise3(float x, float y, float z,
  51. // float lacunarity, float gain,int octaves,
  52. // int x_wrap, int y_wrap, int z_wrap);
  53. //
  54. // Typical values to start playing with:
  55. // octaves = 6 -- number of "octaves" of noise3() to sum
  56. // lacunarity = ~ 2.0 -- spacing between successive octaves (use exactly 2.0 for wrapping output)
  57. // gain = 0.5 -- relative weighting applied to each successive octave
  58. // offset = 1.0? -- used to invert the ridges, may need to be larger, not sure
  59. //
  60. //
  61. // Contributors:
  62. // Jack Mott - additional noise functions
  63. //
  64. #ifdef __cplusplus
  65. extern "C" {
  66. #endif
  67. extern float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap);
  68. extern float stb_perlin_ridge_noise3(float x, float y, float z,float lacunarity, float gain, float offset, int octaves,int x_wrap, int y_wrap, int z_wrap);
  69. extern float stb_perlin_fbm_noise3(float x, float y, float z,float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap);
  70. extern float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap);
  71. #ifdef __cplusplus
  72. }
  73. #endif
  74. #ifdef STB_PERLIN_IMPLEMENTATION
  75. // not same permutation table as Perlin's reference to avoid copyright issues;
  76. // Perlin's table can be found at http://mrl.nyu.edu/~perlin/noise/
  77. // @OPTIMIZE: should this be unsigned char instead of int for cache?
  78. static unsigned char stb__perlin_randtab[512] =
  79. {
  80. 23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123,
  81. 152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72,
  82. 175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240,
  83. 8, 50, 84, 229, 49, 210, 173, 239, 141, 1, 87, 18, 2, 198, 143, 57,
  84. 225, 160, 58, 217, 168, 206, 245, 204, 199, 6, 73, 60, 20, 230, 211, 233,
  85. 94, 200, 88, 9, 74, 155, 33, 15, 219, 130, 226, 202, 83, 236, 42, 172,
  86. 165, 218, 55, 222, 46, 107, 98, 154, 109, 67, 196, 178, 127, 158, 13, 243,
  87. 65, 79, 166, 248, 25, 224, 115, 80, 68, 51, 184, 128, 232, 208, 151, 122,
  88. 26, 212, 105, 43, 179, 213, 235, 148, 146, 89, 14, 195, 28, 78, 112, 76,
  89. 250, 47, 24, 251, 140, 108, 186, 190, 228, 170, 183, 139, 39, 188, 244, 246,
  90. 132, 48, 119, 144, 180, 138, 134, 193, 82, 182, 120, 121, 86, 220, 209, 3,
  91. 91, 241, 149, 85, 205, 150, 113, 216, 31, 100, 41, 164, 177, 214, 153, 231,
  92. 38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221,
  93. 131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62,
  94. 27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135,
  95. 61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5,
  96. // and a second copy so we don't need an extra mask or static initializer
  97. 23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123,
  98. 152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72,
  99. 175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240,
  100. 8, 50, 84, 229, 49, 210, 173, 239, 141, 1, 87, 18, 2, 198, 143, 57,
  101. 225, 160, 58, 217, 168, 206, 245, 204, 199, 6, 73, 60, 20, 230, 211, 233,
  102. 94, 200, 88, 9, 74, 155, 33, 15, 219, 130, 226, 202, 83, 236, 42, 172,
  103. 165, 218, 55, 222, 46, 107, 98, 154, 109, 67, 196, 178, 127, 158, 13, 243,
  104. 65, 79, 166, 248, 25, 224, 115, 80, 68, 51, 184, 128, 232, 208, 151, 122,
  105. 26, 212, 105, 43, 179, 213, 235, 148, 146, 89, 14, 195, 28, 78, 112, 76,
  106. 250, 47, 24, 251, 140, 108, 186, 190, 228, 170, 183, 139, 39, 188, 244, 246,
  107. 132, 48, 119, 144, 180, 138, 134, 193, 82, 182, 120, 121, 86, 220, 209, 3,
  108. 91, 241, 149, 85, 205, 150, 113, 216, 31, 100, 41, 164, 177, 214, 153, 231,
  109. 38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221,
  110. 131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62,
  111. 27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135,
  112. 61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5,
  113. };
  114. static float stb__perlin_lerp(float a, float b, float t)
  115. {
  116. return a + (b-a) * t;
  117. }
  118. static int stb__perlin_fastfloor(float a)
  119. {
  120. int ai = (int) a;
  121. return (a < ai) ? ai-1 : ai;
  122. }
  123. // different grad function from Perlin's, but easy to modify to match reference
  124. static float stb__perlin_grad(int hash, float x, float y, float z)
  125. {
  126. static float basis[12][4] =
  127. {
  128. { 1, 1, 0 },
  129. { -1, 1, 0 },
  130. { 1,-1, 0 },
  131. { -1,-1, 0 },
  132. { 1, 0, 1 },
  133. { -1, 0, 1 },
  134. { 1, 0,-1 },
  135. { -1, 0,-1 },
  136. { 0, 1, 1 },
  137. { 0,-1, 1 },
  138. { 0, 1,-1 },
  139. { 0,-1,-1 },
  140. };
  141. // perlin's gradient has 12 cases so some get used 1/16th of the time
  142. // and some 2/16ths. We reduce bias by changing those fractions
  143. // to 5/64ths and 6/64ths, and the same 4 cases get the extra weight.
  144. static unsigned char indices[64] =
  145. {
  146. 0,1,2,3,4,5,6,7,8,9,10,11,
  147. 0,9,1,11,
  148. 0,1,2,3,4,5,6,7,8,9,10,11,
  149. 0,1,2,3,4,5,6,7,8,9,10,11,
  150. 0,1,2,3,4,5,6,7,8,9,10,11,
  151. 0,1,2,3,4,5,6,7,8,9,10,11,
  152. };
  153. // if you use reference permutation table, change 63 below to 15 to match reference
  154. // (this is why the ordering of the table above is funky)
  155. float *grad = basis[indices[hash & 63]];
  156. return grad[0]*x + grad[1]*y + grad[2]*z;
  157. }
  158. float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)
  159. {
  160. float u,v,w;
  161. float n000,n001,n010,n011,n100,n101,n110,n111;
  162. float n00,n01,n10,n11;
  163. float n0,n1;
  164. unsigned int x_mask = (x_wrap-1) & 255;
  165. unsigned int y_mask = (y_wrap-1) & 255;
  166. unsigned int z_mask = (z_wrap-1) & 255;
  167. int px = stb__perlin_fastfloor(x);
  168. int py = stb__perlin_fastfloor(y);
  169. int pz = stb__perlin_fastfloor(z);
  170. int x0 = px & x_mask, x1 = (px+1) & x_mask;
  171. int y0 = py & y_mask, y1 = (py+1) & y_mask;
  172. int z0 = pz & z_mask, z1 = (pz+1) & z_mask;
  173. int r0,r1, r00,r01,r10,r11;
  174. #define stb__perlin_ease(a) (((a*6-15)*a + 10) * a * a * a)
  175. x -= px; u = stb__perlin_ease(x);
  176. y -= py; v = stb__perlin_ease(y);
  177. z -= pz; w = stb__perlin_ease(z);
  178. r0 = stb__perlin_randtab[x0];
  179. r1 = stb__perlin_randtab[x1];
  180. r00 = stb__perlin_randtab[r0+y0];
  181. r01 = stb__perlin_randtab[r0+y1];
  182. r10 = stb__perlin_randtab[r1+y0];
  183. r11 = stb__perlin_randtab[r1+y1];
  184. n000 = stb__perlin_grad(stb__perlin_randtab[r00+z0], x , y , z );
  185. n001 = stb__perlin_grad(stb__perlin_randtab[r00+z1], x , y , z-1 );
  186. n010 = stb__perlin_grad(stb__perlin_randtab[r01+z0], x , y-1, z );
  187. n011 = stb__perlin_grad(stb__perlin_randtab[r01+z1], x , y-1, z-1 );
  188. n100 = stb__perlin_grad(stb__perlin_randtab[r10+z0], x-1, y , z );
  189. n101 = stb__perlin_grad(stb__perlin_randtab[r10+z1], x-1, y , z-1 );
  190. n110 = stb__perlin_grad(stb__perlin_randtab[r11+z0], x-1, y-1, z );
  191. n111 = stb__perlin_grad(stb__perlin_randtab[r11+z1], x-1, y-1, z-1 );
  192. n00 = stb__perlin_lerp(n000,n001,w);
  193. n01 = stb__perlin_lerp(n010,n011,w);
  194. n10 = stb__perlin_lerp(n100,n101,w);
  195. n11 = stb__perlin_lerp(n110,n111,w);
  196. n0 = stb__perlin_lerp(n00,n01,v);
  197. n1 = stb__perlin_lerp(n10,n11,v);
  198. return stb__perlin_lerp(n0,n1,u);
  199. }
  200. float stb_perlin_ridge_noise3(float x, float y, float z,float lacunarity, float gain, float offset, int octaves,int x_wrap, int y_wrap, int z_wrap)
  201. {
  202. int i;
  203. float frequency = 1.0f;
  204. float prev = 1.0f;
  205. float amplitude = 0.5f;
  206. float sum = 0.0f;
  207. for (i = 0; i < octaves; i++) {
  208. float r = (float)(stb_perlin_noise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap));
  209. r = r<0 ? -r : r; // fabs()
  210. r = offset - r;
  211. r = r*r;
  212. sum += r*amplitude*prev;
  213. prev = r;
  214. frequency *= lacunarity;
  215. amplitude *= gain;
  216. }
  217. return sum;
  218. }
  219. float stb_perlin_fbm_noise3(float x, float y, float z,float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap)
  220. {
  221. int i;
  222. float frequency = 1.0f;
  223. float amplitude = 1.0f;
  224. float sum = 0.0f;
  225. for (i = 0; i < octaves; i++) {
  226. sum += stb_perlin_noise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap)*amplitude;
  227. frequency *= lacunarity;
  228. amplitude *= gain;
  229. }
  230. return sum;
  231. }
  232. float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap)
  233. {
  234. int i;
  235. float frequency = 1.0f;
  236. float amplitude = 1.0f;
  237. float sum = 0.0f;
  238. for (i = 0; i < octaves; i++) {
  239. float r = stb_perlin_noise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap)*amplitude;
  240. r = r<0 ? -r : r; // fabs()
  241. sum += r;
  242. frequency *= lacunarity;
  243. amplitude *= gain;
  244. }
  245. return sum;
  246. }
  247. #endif // STB_PERLIN_IMPLEMENTATION
  248. /*
  249. ------------------------------------------------------------------------------
  250. This software is available under 2 licenses -- choose whichever you prefer.
  251. ------------------------------------------------------------------------------
  252. ALTERNATIVE A - MIT License
  253. Copyright (c) 2017 Sean Barrett
  254. Permission is hereby granted, free of charge, to any person obtaining a copy of
  255. this software and associated documentation files (the "Software"), to deal in
  256. the Software without restriction, including without limitation the rights to
  257. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  258. of the Software, and to permit persons to whom the Software is furnished to do
  259. so, subject to the following conditions:
  260. The above copyright notice and this permission notice shall be included in all
  261. copies or substantial portions of the Software.
  262. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  263. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  264. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  265. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  266. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  267. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  268. SOFTWARE.
  269. ------------------------------------------------------------------------------
  270. ALTERNATIVE B - Public Domain (www.unlicense.org)
  271. This is free and unencumbered software released into the public domain.
  272. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  273. software, either in source code form or as a compiled binary, for any purpose,
  274. commercial or non-commercial, and by any means.
  275. In jurisdictions that recognize copyright laws, the author or authors of this
  276. software dedicate any and all copyright interest in the software to the public
  277. domain. We make this dedication for the benefit of the public at large and to
  278. the detriment of our heirs and successors. We intend this dedication to be an
  279. overt act of relinquishment in perpetuity of all present and future rights to
  280. this software under copyright law.
  281. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  282. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  283. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  284. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  285. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  286. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  287. ------------------------------------------------------------------------------
  288. */