PageRenderTime 194ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/llperlin.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 294 lines | 203 code | 59 blank | 32 comment | 13 complexity | 104c013c96edf81f6a0d872a106b618e MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llperlin.cpp
  3. *
  4. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #include "linden_common.h"
  26. #include "llmath.h"
  27. #include "llperlin.h"
  28. #define B 0x100
  29. #define BM 0xff
  30. #define N 0x1000
  31. #define NF32 (4096.f)
  32. #define NP 12 /* 2^N */
  33. #define NM 0xfff
  34. static S32 p[B + B + 2];
  35. static F32 g3[B + B + 2][3];
  36. static F32 g2[B + B + 2][2];
  37. static F32 g1[B + B + 2];
  38. bool LLPerlinNoise::sInitialized = 0;
  39. static void normalize2(F32 v[2])
  40. {
  41. F32 s = 1.f/(F32)sqrt(v[0] * v[0] + v[1] * v[1]);
  42. v[0] = v[0] * s;
  43. v[1] = v[1] * s;
  44. }
  45. static void normalize3(F32 v[3])
  46. {
  47. F32 s = 1.f/(F32)sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  48. v[0] = v[0] * s;
  49. v[1] = v[1] * s;
  50. v[2] = v[2] * s;
  51. }
  52. static void fast_setup(F32 vec, U8 &b0, U8 &b1, F32 &r0, F32 &r1)
  53. {
  54. S32 t_S32;
  55. r1 = vec + NF32;
  56. t_S32 = lltrunc(r1);
  57. b0 = (U8)t_S32;
  58. b1 = b0 + 1;
  59. r0 = r1 - t_S32;
  60. r1 = r0 - 1.f;
  61. }
  62. void LLPerlinNoise::init(void)
  63. {
  64. int i, j, k;
  65. for (i = 0 ; i < B ; i++)
  66. {
  67. p[i] = i;
  68. g1[i] = (F32)((rand() % (B + B)) - B) / B;
  69. for (j = 0 ; j < 2 ; j++)
  70. g2[i][j] = (F32)((rand() % (B + B)) - B) / B;
  71. normalize2(g2[i]);
  72. for (j = 0 ; j < 3 ; j++)
  73. g3[i][j] = (F32)((rand() % (B + B)) - B) / B;
  74. normalize3(g3[i]);
  75. }
  76. while (--i)
  77. {
  78. k = p[i];
  79. p[i] = p[j = rand() % B];
  80. p[j] = k;
  81. }
  82. for (i = 0 ; i < B + 2 ; i++)
  83. {
  84. p[B + i] = p[i];
  85. g1[B + i] = g1[i];
  86. for (j = 0 ; j < 2 ; j++)
  87. g2[B + i][j] = g2[i][j];
  88. for (j = 0 ; j < 3 ; j++)
  89. g3[B + i][j] = g3[i][j];
  90. }
  91. sInitialized = true;
  92. }
  93. //============================================================================
  94. // Noise functions
  95. #define s_curve(t) ( t * t * (3.f - 2.f * t) )
  96. #define lerp_m(t, a, b) ( a + t * (b - a) )
  97. F32 LLPerlinNoise::noise1(F32 x)
  98. {
  99. int bx0, bx1;
  100. F32 rx0, rx1, sx, t, u, v;
  101. if (!sInitialized)
  102. init();
  103. t = x + N;
  104. bx0 = (lltrunc(t)) & BM;
  105. bx1 = (bx0+1) & BM;
  106. rx0 = t - lltrunc(t);
  107. rx1 = rx0 - 1.f;
  108. sx = s_curve(rx0);
  109. u = rx0 * g1[ p[ bx0 ] ];
  110. v = rx1 * g1[ p[ bx1 ] ];
  111. return lerp_m(sx, u, v);
  112. }
  113. static F32 fast_at2(F32 rx, F32 ry, F32 *q)
  114. {
  115. return rx * q[0] + ry * q[1];
  116. }
  117. F32 LLPerlinNoise::noise2(F32 x, F32 y)
  118. {
  119. U8 bx0, bx1, by0, by1;
  120. U32 b00, b10, b01, b11;
  121. F32 rx0, rx1, ry0, ry1, *q, sx, sy, a, b, u, v;
  122. S32 i, j;
  123. if (!sInitialized)
  124. init();
  125. fast_setup(x, bx0, bx1, rx0, rx1);
  126. fast_setup(y, by0, by1, ry0, ry1);
  127. i = *(p + bx0);
  128. j = *(p + bx1);
  129. b00 = *(p + i + by0);
  130. b10 = *(p + j + by0);
  131. b01 = *(p + i + by1);
  132. b11 = *(p + j + by1);
  133. sx = s_curve(rx0);
  134. sy = s_curve(ry0);
  135. q = *(g2 + b00);
  136. u = fast_at2(rx0, ry0, q);
  137. q = *(g2 + b10);
  138. v = fast_at2(rx1, ry0, q);
  139. a = lerp_m(sx, u, v);
  140. q = *(g2 + b01);
  141. u = fast_at2(rx0,ry1,q);
  142. q = *(g2 + b11);
  143. v = fast_at2(rx1,ry1,q);
  144. b = lerp_m(sx, u, v);
  145. return lerp_m(sy, a, b);
  146. }
  147. static F32 fast_at3(F32 rx, F32 ry, F32 rz, F32 *q)
  148. {
  149. return rx * q[0] + ry * q[1] + rz * q[2];
  150. }
  151. F32 LLPerlinNoise::noise3(F32 x, F32 y, F32 z)
  152. {
  153. U8 bx0, bx1, by0, by1, bz0, bz1;
  154. S32 b00, b10, b01, b11;
  155. F32 rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
  156. S32 i, j;
  157. if (!sInitialized)
  158. init();
  159. fast_setup(x, bx0,bx1, rx0,rx1);
  160. fast_setup(y, by0,by1, ry0,ry1);
  161. fast_setup(z, bz0,bz1, rz0,rz1);
  162. i = p[ bx0 ];
  163. j = p[ bx1 ];
  164. b00 = p[ i + by0 ];
  165. b10 = p[ j + by0 ];
  166. b01 = p[ i + by1 ];
  167. b11 = p[ j + by1 ];
  168. t = s_curve(rx0);
  169. sy = s_curve(ry0);
  170. sz = s_curve(rz0);
  171. q = g3[ b00 + bz0 ];
  172. u = fast_at3(rx0,ry0,rz0,q);
  173. q = g3[ b10 + bz0 ];
  174. v = fast_at3(rx1,ry0,rz0,q);
  175. a = lerp_m(t, u, v);
  176. q = g3[ b01 + bz0 ];
  177. u = fast_at3(rx0,ry1,rz0,q);
  178. q = g3[ b11 + bz0 ];
  179. v = fast_at3(rx1,ry1,rz0,q);
  180. b = lerp_m(t, u, v);
  181. c = lerp_m(sy, a, b);
  182. q = g3[ b00 + bz1 ];
  183. u = fast_at3(rx0,ry0,rz1,q);
  184. q = g3[ b10 + bz1 ];
  185. v = fast_at3(rx1,ry0,rz1,q);
  186. a = lerp_m(t, u, v);
  187. q = g3[ b01 + bz1 ];
  188. u = fast_at3(rx0,ry1,rz1,q);
  189. q = g3[ b11 + bz1 ];
  190. v = fast_at3(rx1,ry1,rz1,q);
  191. b = lerp_m(t, u, v);
  192. d = lerp_m(sy, a, b);
  193. return lerp_m(sz, c, d);
  194. }
  195. F32 LLPerlinNoise::turbulence2(F32 x, F32 y, F32 freq)
  196. {
  197. F32 t, lx, ly;
  198. for (t = 0.f ; freq >= 1.f ; freq *= 0.5f)
  199. {
  200. lx = freq * x;
  201. ly = freq * y;
  202. t += noise2(lx, ly)/freq;
  203. }
  204. return t;
  205. }
  206. F32 LLPerlinNoise::turbulence3(F32 x, F32 y, F32 z, F32 freq)
  207. {
  208. F32 t, lx, ly, lz;
  209. for (t = 0.f ; freq >= 1.f ; freq *= 0.5f)
  210. {
  211. lx = freq * x;
  212. ly = freq * y;
  213. lz = freq * z;
  214. t += noise3(lx,ly,lz)/freq;
  215. // t += fabs(noise3(lx,ly,lz)) / freq; // Like snow - bubbly at low frequencies
  216. // t += sqrt(fabs(noise3(lx,ly,lz))) / freq; // Better at low freq
  217. // t += (noise3(lx,ly,lz)*noise3(lx,ly,lz)) / freq;
  218. }
  219. return t;
  220. }
  221. F32 LLPerlinNoise::clouds3(F32 x, F32 y, F32 z, F32 freq)
  222. {
  223. F32 t, lx, ly, lz;
  224. for (t = 0.f ; freq >= 1.f ; freq *= 0.5f)
  225. {
  226. lx = freq * x;
  227. ly = freq * y;
  228. lz = freq * z;
  229. // t += noise3(lx,ly,lz)/freq;
  230. // t += fabs(noise3(lx,ly,lz)) / freq; // Like snow - bubbly at low frequencies
  231. // t += sqrt(fabs(noise3(lx,ly,lz))) / freq; // Better at low freq
  232. t += (noise3(lx,ly,lz)*noise3(lx,ly,lz)) / freq;
  233. }
  234. return t;
  235. }