llnoise.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * @file llnoise.h
  3. * @brief Perlin noise routines for procedural textures, etc
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLNOISE_H
  33. #define LL_LLNOISE_H
  34. #include "llmath.h"
  35. F32 turbulence2(F32* v, F32 freq);
  36. F32 turbulence3(float* v, float freq);
  37. F32 clouds3(float* v, float freq);
  38. F32 noise2(float* vec);
  39. F32 noise3(float* vec);
  40. extern F32 LOGHALFFACTOR;
  41. LL_INLINE F32 bias(F32 a, F32 b)
  42. {
  43. return powf(a, logf(b) * LOGHALFFACTOR);
  44. }
  45. LL_INLINE F32 gain(F32 a, F32 b)
  46. {
  47. F32 p = logf(1.f - b) * LOGHALFFACTOR;
  48. if (a < .001f)
  49. {
  50. return 0.f;
  51. }
  52. else if (a > .999f)
  53. {
  54. return 1.f;
  55. }
  56. if (a < 0.5f)
  57. {
  58. return powf(2.f * a, p) * 0.5f;
  59. }
  60. else
  61. {
  62. return 1.f - powf(2.f * (1.f - a), p) * 0.5f;
  63. }
  64. }
  65. LL_INLINE F32 turbulence2(F32* v, F32 freq)
  66. {
  67. F32 t, vec[2];
  68. for (t = 0.f; freq >= 1.f; freq *= 0.5f)
  69. {
  70. vec[0] = freq * v[0];
  71. vec[1] = freq * v[1];
  72. t += noise2(vec) / freq;
  73. }
  74. return t;
  75. }
  76. LL_INLINE F32 turbulence3(F32* v, F32 freq)
  77. {
  78. F32 t, vec[3];
  79. for (t = 0.f; freq >= 1.f; freq *= 0.5f)
  80. {
  81. vec[0] = freq * v[0];
  82. vec[1] = freq * v[1];
  83. vec[2] = freq * v[2];
  84. t += noise3(vec) / freq;
  85. #if 0
  86. // Like snow, bubbly at low frequencies
  87. t += fabsf(noise3(vec)) / freq;
  88. // Better at low freq
  89. t += sqrtf(fabsf(noise3(vec))) / freq;
  90. t += (noise3(vec) * noise3(vec)) / freq;
  91. #endif
  92. }
  93. return t;
  94. }
  95. LL_INLINE F32 clouds3(F32* v, F32 freq)
  96. {
  97. F32 t, vec[3];
  98. for (t = 0.f; freq >= 1.f; freq *= 0.5f)
  99. {
  100. vec[0] = freq * v[0];
  101. vec[1] = freq * v[1];
  102. vec[2] = freq * v[2];
  103. #if 0
  104. t += noise3(vec) / freq;
  105. // Like snow - bubbly at low frequencies
  106. t += fabsf(noise3(vec)) / freq;
  107. // Better at low freq
  108. t += sqrtf(fabsf(noise3(vec))) / freq;
  109. #endif
  110. t += (noise3(vec) * noise3(vec)) / freq;
  111. }
  112. return t;
  113. }
  114. /* noise functions over 1, 2, and 3 dimensions */
  115. #define B 0x100
  116. #define BM 0xff
  117. #define N 0x1000
  118. #define NF32 (4096.f)
  119. #define NP 12 /* 2^N */
  120. #define NM 0xfff
  121. extern S32 p[B + B + 2];
  122. extern F32 g3[B + B + 2][3];
  123. extern F32 g2[B + B + 2][2];
  124. extern F32 g1[B + B + 2];
  125. extern S32 gNoiseStart;
  126. static void init();
  127. #define s_curve(t) ( t * t * (3.f - 2.f * t) )
  128. #define lerp_m(t, a, b) ( a + t * (b - a) )
  129. #define setup_noise(i,b0,b1,r0,r1)\
  130. F32 t = vec[i] + N;\
  131. b0 = (llfloor(t)) & BM;\
  132. b1 = (b0+1) & BM;\
  133. r0 = t - llfloor(t);\
  134. r1 = r0 - 1.f;
  135. LL_INLINE void fast_setup(F32 vec, U8& b0, U8& b1, F32& r0, F32& r1)
  136. {
  137. r1 = vec + NF32;
  138. S32 t_S32 = llfloor(r1);
  139. b0 = (U8)t_S32;
  140. b1 = b0 + 1;
  141. r0 = r1 - t_S32;
  142. r1 = r0 - 1.f;
  143. }
  144. LL_INLINE F32 noise1(F32 arg)
  145. {
  146. F32 vec[1];
  147. vec[0] = arg;
  148. if (gNoiseStart)
  149. {
  150. gNoiseStart = 0;
  151. init();
  152. }
  153. S32 bx0, bx1;
  154. F32 rx0, rx1;
  155. setup_noise(0, bx0, bx1, rx0, rx1);
  156. F32 sx = s_curve(rx0);
  157. F32 u = rx0 * g1[p[bx0]];
  158. F32 v = rx1 * g1[p[bx1]];
  159. return lerp_m(sx, u, v);
  160. }
  161. LL_INLINE F32 fast_at2(F32 rx, F32 ry, F32* q)
  162. {
  163. return rx * (*q) + ry * (*(q + 1));
  164. }
  165. LL_INLINE F32 fast_at3(F32 rx, F32 ry, F32 rz, F32* q)
  166. {
  167. return rx * (*q) + ry * (*(q + 1)) + rz * (*(q + 2));
  168. }
  169. LL_INLINE F32 noise3(F32* vec)
  170. {
  171. if (gNoiseStart)
  172. {
  173. gNoiseStart = 0;
  174. init();
  175. }
  176. U8 bx0, bx1, by0, by1, bz0, bz1;
  177. F32 rx0, rx1, ry0, ry1, rz0, rz1;
  178. fast_setup(*vec, bx0, bx1, rx0, rx1);
  179. fast_setup(*(vec + 1), by0, by1, ry0, ry1);
  180. fast_setup(*(vec + 2), bz0, bz1, rz0, rz1);
  181. S32 i = p[bx0];
  182. S32 j = p[bx1];
  183. S32 b00 = p[i + by0];
  184. S32 b10 = p[j + by0];
  185. S32 b01 = p[i + by1];
  186. S32 b11 = p[j + by1];
  187. F32 t = s_curve(rx0);
  188. F32 sy = s_curve(ry0);
  189. F32 sz = s_curve(rz0);
  190. F32* q = g3[b00 + bz0];
  191. F32 u = fast_at3(rx0, ry0, rz0, q);
  192. q = g3[b10 + bz0];
  193. F32 v = fast_at3(rx1, ry0, rz0, q);
  194. F32 a = lerp_m(t, u, v);
  195. q = g3[b01 + bz0];
  196. u = fast_at3(rx0, ry1, rz0, q);
  197. q = g3[b11 + bz0];
  198. v = fast_at3(rx1, ry1, rz0, q);
  199. F32 b = lerp_m(t, u, v);
  200. F32 c = lerp_m(sy, a, b);
  201. q = g3[b00 + bz1];
  202. u = fast_at3(rx0, ry0, rz1, q);
  203. q = g3[b10 + bz1];
  204. v = fast_at3(rx1, ry0, rz1, q);
  205. a = lerp_m(t, u, v);
  206. q = g3[b01 + bz1];
  207. u = fast_at3(rx0, ry1, rz1, q);
  208. q = g3[b11 + bz1];
  209. v = fast_at3(rx1, ry1, rz1, q);
  210. b = lerp_m(t, u, v);
  211. F32 d = lerp_m(sy, a, b);
  212. return lerp_m(sz, c, d);
  213. }
  214. static void normalize2(F32 v[2])
  215. {
  216. F32 s = 1.f / sqrtf(v[0] * v[0] + v[1] * v[1]);
  217. v[0] = v[0] * s;
  218. v[1] = v[1] * s;
  219. }
  220. static void normalize3(F32 v[3])
  221. {
  222. F32 s = 1.f / sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  223. v[0] = v[0] * s;
  224. v[1] = v[1] * s;
  225. v[2] = v[2] * s;
  226. }
  227. static void init()
  228. {
  229. // We want repeatable noise (e.g. for stable terrain texturing), so seed
  230. // with known value
  231. srand(42);
  232. S32 i, j, k;
  233. for (i = 0; i < B; ++i)
  234. {
  235. p[i] = i;
  236. g1[i] = (F32)((rand() % (B + B)) - B) / B;
  237. for (j = 0; j < 2; ++j)
  238. {
  239. g2[i][j] = (F32)((rand() % (B + B)) - B) / B;
  240. }
  241. normalize2(g2[i]);
  242. for (j = 0; j < 3; ++j)
  243. {
  244. g3[i][j] = (F32)((rand() % (B + B)) - B) / B;
  245. }
  246. normalize3(g3[i]);
  247. }
  248. while (--i)
  249. {
  250. k = p[i];
  251. p[i] = p[j = rand() % B];
  252. p[j] = k;
  253. }
  254. for (i = 0; i < B + 2; ++i)
  255. {
  256. p[B + i] = p[i];
  257. g1[B + i] = g1[i];
  258. for (j = 0; j < 2; ++j)
  259. {
  260. g2[B + i][j] = g2[i][j];
  261. }
  262. for (j = 0; j < 3; ++j)
  263. {
  264. g3[B + i][j] = g3[i][j];
  265. }
  266. }
  267. // Reintroduce entropy
  268. srand(time(NULL));
  269. }
  270. #undef B
  271. #undef BM
  272. #undef N
  273. #undef NF32
  274. #undef NP
  275. #undef NM
  276. #endif // LL_LLNOISE_H