Perlin.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include <TestFramework.h>
  2. #include <Math/Perlin.h>
  3. // not same permutation table as Perlin's reference to avoid copyright issues;
  4. // Perlin's table can be found at http://mrl.nyu.edu/~perlin/noise/
  5. // @OPTIMIZE: should this be unsigned char instead of int for cache?
  6. static unsigned char stb_perlin_randtab[512] =
  7. {
  8. 23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123,
  9. 152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72,
  10. 175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240,
  11. 8, 50, 84, 229, 49, 210, 173, 239, 141, 1, 87, 18, 2, 198, 143, 57,
  12. 225, 160, 58, 217, 168, 206, 245, 204, 199, 6, 73, 60, 20, 230, 211, 233,
  13. 94, 200, 88, 9, 74, 155, 33, 15, 219, 130, 226, 202, 83, 236, 42, 172,
  14. 165, 218, 55, 222, 46, 107, 98, 154, 109, 67, 196, 178, 127, 158, 13, 243,
  15. 65, 79, 166, 248, 25, 224, 115, 80, 68, 51, 184, 128, 232, 208, 151, 122,
  16. 26, 212, 105, 43, 179, 213, 235, 148, 146, 89, 14, 195, 28, 78, 112, 76,
  17. 250, 47, 24, 251, 140, 108, 186, 190, 228, 170, 183, 139, 39, 188, 244, 246,
  18. 132, 48, 119, 144, 180, 138, 134, 193, 82, 182, 120, 121, 86, 220, 209, 3,
  19. 91, 241, 149, 85, 205, 150, 113, 216, 31, 100, 41, 164, 177, 214, 153, 231,
  20. 38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221,
  21. 131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62,
  22. 27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135,
  23. 61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5,
  24. // and a second copy so we don't need an extra mask or static initializer
  25. 23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123,
  26. 152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72,
  27. 175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240,
  28. 8, 50, 84, 229, 49, 210, 173, 239, 141, 1, 87, 18, 2, 198, 143, 57,
  29. 225, 160, 58, 217, 168, 206, 245, 204, 199, 6, 73, 60, 20, 230, 211, 233,
  30. 94, 200, 88, 9, 74, 155, 33, 15, 219, 130, 226, 202, 83, 236, 42, 172,
  31. 165, 218, 55, 222, 46, 107, 98, 154, 109, 67, 196, 178, 127, 158, 13, 243,
  32. 65, 79, 166, 248, 25, 224, 115, 80, 68, 51, 184, 128, 232, 208, 151, 122,
  33. 26, 212, 105, 43, 179, 213, 235, 148, 146, 89, 14, 195, 28, 78, 112, 76,
  34. 250, 47, 24, 251, 140, 108, 186, 190, 228, 170, 183, 139, 39, 188, 244, 246,
  35. 132, 48, 119, 144, 180, 138, 134, 193, 82, 182, 120, 121, 86, 220, 209, 3,
  36. 91, 241, 149, 85, 205, 150, 113, 216, 31, 100, 41, 164, 177, 214, 153, 231,
  37. 38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221,
  38. 131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62,
  39. 27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135,
  40. 61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5,
  41. };
  42. static float stb_perlin_lerp(float a, float b, float t)
  43. {
  44. return a + (b-a) * t;
  45. }
  46. static int stb_perlin_fastfloor(float a)
  47. {
  48. int ai = (int) a;
  49. return (a < ai) ? ai-1 : ai;
  50. }
  51. // different grad function from Perlin's, but easy to modify to match reference
  52. static float stb_perlin_grad(int hash, float x, float y, float z)
  53. {
  54. static float basis[12][4] =
  55. {
  56. { 1, 1, 0 },
  57. { -1, 1, 0 },
  58. { 1,-1, 0 },
  59. { -1,-1, 0 },
  60. { 1, 0, 1 },
  61. { -1, 0, 1 },
  62. { 1, 0,-1 },
  63. { -1, 0,-1 },
  64. { 0, 1, 1 },
  65. { 0,-1, 1 },
  66. { 0, 1,-1 },
  67. { 0,-1,-1 },
  68. };
  69. // perlin's gradient has 12 cases so some get used 1/16th of the time
  70. // and some 2/16ths. We reduce bias by changing those fractions
  71. // to 5/64ths and 6/64ths, and the same 4 cases get the extra weight.
  72. static unsigned char indices[64] =
  73. {
  74. 0,1,2,3,4,5,6,7,8,9,10,11,
  75. 0,9,1,11,
  76. 0,1,2,3,4,5,6,7,8,9,10,11,
  77. 0,1,2,3,4,5,6,7,8,9,10,11,
  78. 0,1,2,3,4,5,6,7,8,9,10,11,
  79. 0,1,2,3,4,5,6,7,8,9,10,11,
  80. };
  81. // if you use reference permutation table, change 63 below to 15 to match reference
  82. // (this is why the ordering of the table above is funky)
  83. float *grad = basis[indices[hash & 63]];
  84. return grad[0]*x + grad[1]*y + grad[2]*z;
  85. }
  86. float PerlinNoise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)
  87. {
  88. float u,v,w;
  89. float n000,n001,n010,n011,n100,n101,n110,n111;
  90. float n00,n01,n10,n11;
  91. float n0,n1;
  92. unsigned int x_mask = (x_wrap-1) & 255;
  93. unsigned int y_mask = (y_wrap-1) & 255;
  94. unsigned int z_mask = (z_wrap-1) & 255;
  95. int px = stb_perlin_fastfloor(x);
  96. int py = stb_perlin_fastfloor(y);
  97. int pz = stb_perlin_fastfloor(z);
  98. int x0 = px & x_mask, x1 = (px+1) & x_mask;
  99. int y0 = py & y_mask, y1 = (py+1) & y_mask;
  100. int z0 = pz & z_mask, z1 = (pz+1) & z_mask;
  101. int r0,r1, r00,r01,r10,r11;
  102. #define stb_perlin_ease(a) (((a*6-15)*a + 10) * a * a * a)
  103. x -= px; u = stb_perlin_ease(x);
  104. y -= py; v = stb_perlin_ease(y);
  105. z -= pz; w = stb_perlin_ease(z);
  106. r0 = stb_perlin_randtab[x0];
  107. r1 = stb_perlin_randtab[x1];
  108. r00 = stb_perlin_randtab[r0+y0];
  109. r01 = stb_perlin_randtab[r0+y1];
  110. r10 = stb_perlin_randtab[r1+y0];
  111. r11 = stb_perlin_randtab[r1+y1];
  112. n000 = stb_perlin_grad(stb_perlin_randtab[r00+z0], x , y , z );
  113. n001 = stb_perlin_grad(stb_perlin_randtab[r00+z1], x , y , z-1 );
  114. n010 = stb_perlin_grad(stb_perlin_randtab[r01+z0], x , y-1, z );
  115. n011 = stb_perlin_grad(stb_perlin_randtab[r01+z1], x , y-1, z-1 );
  116. n100 = stb_perlin_grad(stb_perlin_randtab[r10+z0], x-1, y , z );
  117. n101 = stb_perlin_grad(stb_perlin_randtab[r10+z1], x-1, y , z-1 );
  118. n110 = stb_perlin_grad(stb_perlin_randtab[r11+z0], x-1, y-1, z );
  119. n111 = stb_perlin_grad(stb_perlin_randtab[r11+z1], x-1, y-1, z-1 );
  120. n00 = stb_perlin_lerp(n000,n001,w);
  121. n01 = stb_perlin_lerp(n010,n011,w);
  122. n10 = stb_perlin_lerp(n100,n101,w);
  123. n11 = stb_perlin_lerp(n110,n111,w);
  124. n0 = stb_perlin_lerp(n00,n01,v);
  125. n1 = stb_perlin_lerp(n10,n11,v);
  126. return stb_perlin_lerp(n0,n1,u);
  127. }
  128. float PerlinRidgeNoise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves, int x_wrap, int y_wrap, int z_wrap)
  129. {
  130. int i;
  131. float frequency = 1.0f;
  132. float prev = 1.0f;
  133. float amplitude = 0.5f;
  134. float sum = 0.0f;
  135. for (i = 0; i < octaves; i++) {
  136. float r = (float)(PerlinNoise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap));
  137. r = r<0 ? -r : r; // abs()
  138. r = offset - r;
  139. r = r*r;
  140. sum += r*amplitude*prev;
  141. prev = r;
  142. frequency *= lacunarity;
  143. amplitude *= gain;
  144. }
  145. return sum;
  146. }
  147. float PerlinFBMNoise3(float x, float y, float z, float lacunarity, float gain, int octaves, int x_wrap, int y_wrap, int z_wrap)
  148. {
  149. int i;
  150. float frequency = 1.0f;
  151. float amplitude = 1.0f;
  152. float sum = 0.0f;
  153. for (i = 0; i < octaves; i++) {
  154. sum += PerlinNoise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap)*amplitude;
  155. frequency *= lacunarity;
  156. amplitude *= gain;
  157. }
  158. return sum;
  159. }
  160. float PerlinTurbulenceNoise3(float x, float y, float z, float lacunarity, float gain, int octaves, int x_wrap, int y_wrap, int z_wrap)
  161. {
  162. int i;
  163. float frequency = 1.0f;
  164. float amplitude = 1.0f;
  165. float sum = 0.0f;
  166. for (i = 0; i < octaves; i++) {
  167. float r = PerlinNoise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap)*amplitude;
  168. r = r<0 ? -r : r; // abs()
  169. sum += r;
  170. frequency *= lacunarity;
  171. amplitude *= gain;
  172. }
  173. return sum;
  174. }
  175. /*
  176. ------------------------------------------------------------------------------
  177. This software is available under 2 licenses -- choose whichever you prefer.
  178. ------------------------------------------------------------------------------
  179. ALTERNATIVE A - MIT License
  180. Copyright (c) 2017 Sean Barrett
  181. Permission is hereby granted, free of charge, to any person obtaining a copy of
  182. this software and associated documentation files (the "Software"), to deal in
  183. the Software without restriction, including without limitation the rights to
  184. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  185. of the Software, and to permit persons to whom the Software is furnished to do
  186. so, subject to the following conditions:
  187. The above copyright notice and this permission notice shall be included in all
  188. copies or substantial portions of the Software.
  189. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  190. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  191. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  192. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  193. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  194. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  195. SOFTWARE.
  196. ------------------------------------------------------------------------------
  197. ALTERNATIVE B - Public Domain (www.unlicense.org)
  198. This is free and unencumbered software released into the public domain.
  199. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  200. software, either in source code form or as a compiled binary, for any purpose,
  201. commercial or non-commercial, and by any means.
  202. In jurisdictions that recognize copyright laws, the author or authors of this
  203. software dedicate any and all copyright interest in the software to the public
  204. domain. We make this dedication for the benefit of the public at large and to
  205. the detriment of our heirs and successors. We intend this dedication to be an
  206. overt act of relinquishment in perpetuity of all present and future rights to
  207. this software under copyright law.
  208. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  209. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  210. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  211. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  212. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  213. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  214. ------------------------------------------------------------------------------
  215. */