noise.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. #include <cmath>
  26. #include "noise.h"
  27. #include <iostream>
  28. #include <cstring> // memset
  29. #include "debug.h"
  30. #include "util/numeric.h"
  31. #include "util/string.h"
  32. #include "exceptions.h"
  33. #define NOISE_MAGIC_X 1619
  34. #define NOISE_MAGIC_Y 31337
  35. #define NOISE_MAGIC_Z 52591
  36. #define NOISE_MAGIC_SEED 1013
  37. typedef float (*Interp2dFxn)(
  38. float v00, float v10, float v01, float v11,
  39. float x, float y);
  40. typedef float (*Interp3dFxn)(
  41. float v000, float v100, float v010, float v110,
  42. float v001, float v101, float v011, float v111,
  43. float x, float y, float z);
  44. FlagDesc flagdesc_noiseparams[] = {
  45. {"defaults", NOISE_FLAG_DEFAULTS},
  46. {"eased", NOISE_FLAG_EASED},
  47. {"absvalue", NOISE_FLAG_ABSVALUE},
  48. {"pointbuffer", NOISE_FLAG_POINTBUFFER},
  49. {"simplex", NOISE_FLAG_SIMPLEX},
  50. {NULL, 0}
  51. };
  52. ///////////////////////////////////////////////////////////////////////////////
  53. PcgRandom::PcgRandom(u64 state, u64 seq)
  54. {
  55. seed(state, seq);
  56. }
  57. void PcgRandom::seed(u64 state, u64 seq)
  58. {
  59. m_state = 0U;
  60. m_inc = (seq << 1u) | 1u;
  61. next();
  62. m_state += state;
  63. next();
  64. }
  65. u32 PcgRandom::next()
  66. {
  67. u64 oldstate = m_state;
  68. m_state = oldstate * 6364136223846793005ULL + m_inc;
  69. u32 xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
  70. u32 rot = oldstate >> 59u;
  71. return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
  72. }
  73. u32 PcgRandom::range(u32 bound)
  74. {
  75. // If the bound is 0, we cover the whole RNG's range
  76. if (bound == 0)
  77. return next();
  78. /*
  79. This is an optimization of the expression:
  80. 0x100000000ull % bound
  81. since 64-bit modulo operations typically much slower than 32.
  82. */
  83. u32 threshold = -bound % bound;
  84. u32 r;
  85. /*
  86. If the bound is not a multiple of the RNG's range, it may cause bias,
  87. e.g. a RNG has a range from 0 to 3 and we take want a number 0 to 2.
  88. Using rand() % 3, the number 0 would be twice as likely to appear.
  89. With a very large RNG range, the effect becomes less prevalent but
  90. still present.
  91. This can be solved by modifying the range of the RNG to become a
  92. multiple of bound by dropping values above the a threshold.
  93. In our example, threshold == 4 % 3 == 1, so reject values < 1
  94. (that is, 0), thus making the range == 3 with no bias.
  95. This loop may look dangerous, but will always terminate due to the
  96. RNG's property of uniformity.
  97. */
  98. while ((r = next()) < threshold)
  99. ;
  100. return r % bound;
  101. }
  102. s32 PcgRandom::range(s32 min, s32 max)
  103. {
  104. if (max < min)
  105. throw PrngException("Invalid range (max < min)");
  106. // We have to cast to s64 because otherwise this could overflow,
  107. // and signed overflow is undefined behavior.
  108. u32 bound = (s64)max - (s64)min + 1;
  109. return range(bound) + min;
  110. }
  111. void PcgRandom::bytes(void *out, size_t len)
  112. {
  113. u8 *outb = (u8 *)out;
  114. int bytes_left = 0;
  115. u32 r;
  116. while (len--) {
  117. if (bytes_left == 0) {
  118. bytes_left = sizeof(u32);
  119. r = next();
  120. }
  121. *outb = r & 0xFF;
  122. outb++;
  123. bytes_left--;
  124. r >>= CHAR_BIT;
  125. }
  126. }
  127. s32 PcgRandom::randNormalDist(s32 min, s32 max, int num_trials)
  128. {
  129. s32 accum = 0;
  130. for (int i = 0; i != num_trials; i++)
  131. accum += range(min, max);
  132. return myround((float)accum / num_trials);
  133. }
  134. ///////////////////////////////////////////////////////////////////////////////
  135. float noise2d(int x, int y, s32 seed)
  136. {
  137. unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
  138. + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
  139. n = (n >> 13) ^ n;
  140. n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
  141. return 1.f - (float)(int)n / 0x40000000;
  142. }
  143. float noise3d(int x, int y, int z, s32 seed)
  144. {
  145. unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
  146. + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
  147. n = (n >> 13) ^ n;
  148. n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
  149. return 1.f - (float)(int)n / 0x40000000;
  150. }
  151. inline float dotProduct(float vx, float vy, float wx, float wy)
  152. {
  153. return vx * wx + vy * wy;
  154. }
  155. inline float linearInterpolation(float v0, float v1, float t)
  156. {
  157. return v0 + (v1 - v0) * t;
  158. }
  159. inline float biLinearInterpolation(
  160. float v00, float v10,
  161. float v01, float v11,
  162. float x, float y)
  163. {
  164. float tx = easeCurve(x);
  165. float ty = easeCurve(y);
  166. float u = linearInterpolation(v00, v10, tx);
  167. float v = linearInterpolation(v01, v11, tx);
  168. return linearInterpolation(u, v, ty);
  169. }
  170. inline float biLinearInterpolationNoEase(
  171. float v00, float v10,
  172. float v01, float v11,
  173. float x, float y)
  174. {
  175. float u = linearInterpolation(v00, v10, x);
  176. float v = linearInterpolation(v01, v11, x);
  177. return linearInterpolation(u, v, y);
  178. }
  179. float triLinearInterpolation(
  180. float v000, float v100, float v010, float v110,
  181. float v001, float v101, float v011, float v111,
  182. float x, float y, float z)
  183. {
  184. float tx = easeCurve(x);
  185. float ty = easeCurve(y);
  186. float tz = easeCurve(z);
  187. float u = biLinearInterpolationNoEase(v000, v100, v010, v110, tx, ty);
  188. float v = biLinearInterpolationNoEase(v001, v101, v011, v111, tx, ty);
  189. return linearInterpolation(u, v, tz);
  190. }
  191. float triLinearInterpolationNoEase(
  192. float v000, float v100, float v010, float v110,
  193. float v001, float v101, float v011, float v111,
  194. float x, float y, float z)
  195. {
  196. float u = biLinearInterpolationNoEase(v000, v100, v010, v110, x, y);
  197. float v = biLinearInterpolationNoEase(v001, v101, v011, v111, x, y);
  198. return linearInterpolation(u, v, z);
  199. }
  200. float noise2d_gradient(float x, float y, s32 seed, bool eased)
  201. {
  202. // Calculate the integer coordinates
  203. int x0 = myfloor(x);
  204. int y0 = myfloor(y);
  205. // Calculate the remaining part of the coordinates
  206. float xl = x - (float)x0;
  207. float yl = y - (float)y0;
  208. // Get values for corners of square
  209. float v00 = noise2d(x0, y0, seed);
  210. float v10 = noise2d(x0+1, y0, seed);
  211. float v01 = noise2d(x0, y0+1, seed);
  212. float v11 = noise2d(x0+1, y0+1, seed);
  213. // Interpolate
  214. if (eased)
  215. return biLinearInterpolation(v00, v10, v01, v11, xl, yl);
  216. return biLinearInterpolationNoEase(v00, v10, v01, v11, xl, yl);
  217. }
  218. float noise3d_gradient(float x, float y, float z, s32 seed, bool eased)
  219. {
  220. // Calculate the integer coordinates
  221. int x0 = myfloor(x);
  222. int y0 = myfloor(y);
  223. int z0 = myfloor(z);
  224. // Calculate the remaining part of the coordinates
  225. float xl = x - (float)x0;
  226. float yl = y - (float)y0;
  227. float zl = z - (float)z0;
  228. // Get values for corners of cube
  229. float v000 = noise3d(x0, y0, z0, seed);
  230. float v100 = noise3d(x0 + 1, y0, z0, seed);
  231. float v010 = noise3d(x0, y0 + 1, z0, seed);
  232. float v110 = noise3d(x0 + 1, y0 + 1, z0, seed);
  233. float v001 = noise3d(x0, y0, z0 + 1, seed);
  234. float v101 = noise3d(x0 + 1, y0, z0 + 1, seed);
  235. float v011 = noise3d(x0, y0 + 1, z0 + 1, seed);
  236. float v111 = noise3d(x0 + 1, y0 + 1, z0 + 1, seed);
  237. // Interpolate
  238. if (eased) {
  239. return triLinearInterpolation(
  240. v000, v100, v010, v110,
  241. v001, v101, v011, v111,
  242. xl, yl, zl);
  243. }
  244. return triLinearInterpolationNoEase(
  245. v000, v100, v010, v110,
  246. v001, v101, v011, v111,
  247. xl, yl, zl);
  248. }
  249. float noise2d_perlin(float x, float y, s32 seed,
  250. int octaves, float persistence, bool eased)
  251. {
  252. float a = 0;
  253. float f = 1.0;
  254. float g = 1.0;
  255. for (int i = 0; i < octaves; i++)
  256. {
  257. a += g * noise2d_gradient(x * f, y * f, seed + i, eased);
  258. f *= 2.0;
  259. g *= persistence;
  260. }
  261. return a;
  262. }
  263. float contour(float v)
  264. {
  265. v = std::fabs(v);
  266. if (v >= 1.0)
  267. return 0.0;
  268. return (1.0 - v);
  269. }
  270. ///////////////////////// [ New noise ] ////////////////////////////
  271. float NoisePerlin2D(const NoiseParams *np, float x, float y, s32 seed)
  272. {
  273. float a = 0;
  274. float f = 1.0;
  275. float g = 1.0;
  276. x /= np->spread.X;
  277. y /= np->spread.Y;
  278. seed += np->seed;
  279. for (size_t i = 0; i < np->octaves; i++) {
  280. float noiseval = noise2d_gradient(x * f, y * f, seed + i,
  281. np->flags & (NOISE_FLAG_DEFAULTS | NOISE_FLAG_EASED));
  282. if (np->flags & NOISE_FLAG_ABSVALUE)
  283. noiseval = std::fabs(noiseval);
  284. a += g * noiseval;
  285. f *= np->lacunarity;
  286. g *= np->persist;
  287. }
  288. return np->offset + a * np->scale;
  289. }
  290. float NoisePerlin3D(const NoiseParams *np, float x, float y, float z, s32 seed)
  291. {
  292. float a = 0;
  293. float f = 1.0;
  294. float g = 1.0;
  295. x /= np->spread.X;
  296. y /= np->spread.Y;
  297. z /= np->spread.Z;
  298. seed += np->seed;
  299. for (size_t i = 0; i < np->octaves; i++) {
  300. float noiseval = noise3d_gradient(x * f, y * f, z * f, seed + i,
  301. np->flags & NOISE_FLAG_EASED);
  302. if (np->flags & NOISE_FLAG_ABSVALUE)
  303. noiseval = std::fabs(noiseval);
  304. a += g * noiseval;
  305. f *= np->lacunarity;
  306. g *= np->persist;
  307. }
  308. return np->offset + a * np->scale;
  309. }
  310. Noise::Noise(const NoiseParams *np_, s32 seed, u32 sx, u32 sy, u32 sz)
  311. {
  312. np = *np_;
  313. this->seed = seed;
  314. this->sx = sx;
  315. this->sy = sy;
  316. this->sz = sz;
  317. allocBuffers();
  318. }
  319. Noise::~Noise()
  320. {
  321. delete[] gradient_buf;
  322. delete[] persist_buf;
  323. delete[] noise_buf;
  324. delete[] result;
  325. }
  326. void Noise::allocBuffers()
  327. {
  328. if (sx < 1)
  329. sx = 1;
  330. if (sy < 1)
  331. sy = 1;
  332. if (sz < 1)
  333. sz = 1;
  334. this->noise_buf = NULL;
  335. resizeNoiseBuf(sz > 1);
  336. delete[] gradient_buf;
  337. delete[] persist_buf;
  338. delete[] result;
  339. try {
  340. size_t bufsize = sx * sy * sz;
  341. this->persist_buf = NULL;
  342. this->gradient_buf = new float[bufsize];
  343. this->result = new float[bufsize];
  344. } catch (std::bad_alloc &e) {
  345. throw InvalidNoiseParamsException();
  346. }
  347. }
  348. void Noise::setSize(u32 sx, u32 sy, u32 sz)
  349. {
  350. this->sx = sx;
  351. this->sy = sy;
  352. this->sz = sz;
  353. allocBuffers();
  354. }
  355. void Noise::setSpreadFactor(v3f spread)
  356. {
  357. this->np.spread = spread;
  358. resizeNoiseBuf(sz > 1);
  359. }
  360. void Noise::setOctaves(int octaves)
  361. {
  362. this->np.octaves = octaves;
  363. resizeNoiseBuf(sz > 1);
  364. }
  365. void Noise::resizeNoiseBuf(bool is3d)
  366. {
  367. // Maximum possible spread value factor
  368. float ofactor = (np.lacunarity > 1.0) ?
  369. pow(np.lacunarity, np.octaves - 1) :
  370. np.lacunarity;
  371. // Noise lattice point count
  372. // (int)(sz * spread * ofactor) is # of lattice points crossed due to length
  373. float num_noise_points_x = sx * ofactor / np.spread.X;
  374. float num_noise_points_y = sy * ofactor / np.spread.Y;
  375. float num_noise_points_z = sz * ofactor / np.spread.Z;
  376. // Protect against obviously invalid parameters
  377. if (num_noise_points_x > 1000000000.f ||
  378. num_noise_points_y > 1000000000.f ||
  379. num_noise_points_z > 1000000000.f)
  380. throw InvalidNoiseParamsException();
  381. // Protect against an octave having a spread < 1, causing broken noise values
  382. if (np.spread.X / ofactor < 1.0f ||
  383. np.spread.Y / ofactor < 1.0f ||
  384. np.spread.Z / ofactor < 1.0f) {
  385. errorstream << "A noise parameter has too many octaves: "
  386. << np.octaves << " octaves" << std::endl;
  387. throw InvalidNoiseParamsException("A noise parameter has too many octaves");
  388. }
  389. // + 2 for the two initial endpoints
  390. // + 1 for potentially crossing a boundary due to offset
  391. size_t nlx = (size_t)std::ceil(num_noise_points_x) + 3;
  392. size_t nly = (size_t)std::ceil(num_noise_points_y) + 3;
  393. size_t nlz = is3d ? (size_t)std::ceil(num_noise_points_z) + 3 : 1;
  394. delete[] noise_buf;
  395. try {
  396. noise_buf = new float[nlx * nly * nlz];
  397. } catch (std::bad_alloc &e) {
  398. throw InvalidNoiseParamsException();
  399. }
  400. }
  401. /*
  402. * NB: This algorithm is not optimal in terms of space complexity. The entire
  403. * integer lattice of noise points could be done as 2 lines instead, and for 3D,
  404. * 2 lines + 2 planes.
  405. * However, this would require the noise calls to be interposed with the
  406. * interpolation loops, which may trash the icache, leading to lower overall
  407. * performance.
  408. * Another optimization that could save half as many noise calls is to carry over
  409. * values from the previous noise lattice as midpoints in the new lattice for the
  410. * next octave.
  411. */
  412. #define idx(x, y) ((y) * nlx + (x))
  413. void Noise::gradientMap2D(
  414. float x, float y,
  415. float step_x, float step_y,
  416. s32 seed)
  417. {
  418. float v00, v01, v10, v11, u, v, orig_u;
  419. u32 index, i, j, noisex, noisey;
  420. u32 nlx, nly;
  421. s32 x0, y0;
  422. bool eased = np.flags & (NOISE_FLAG_DEFAULTS | NOISE_FLAG_EASED);
  423. Interp2dFxn interpolate = eased ?
  424. biLinearInterpolation : biLinearInterpolationNoEase;
  425. x0 = std::floor(x);
  426. y0 = std::floor(y);
  427. u = x - (float)x0;
  428. v = y - (float)y0;
  429. orig_u = u;
  430. //calculate noise point lattice
  431. nlx = (u32)(u + sx * step_x) + 2;
  432. nly = (u32)(v + sy * step_y) + 2;
  433. index = 0;
  434. for (j = 0; j != nly; j++)
  435. for (i = 0; i != nlx; i++)
  436. noise_buf[index++] = noise2d(x0 + i, y0 + j, seed);
  437. //calculate interpolations
  438. index = 0;
  439. noisey = 0;
  440. for (j = 0; j != sy; j++) {
  441. v00 = noise_buf[idx(0, noisey)];
  442. v10 = noise_buf[idx(1, noisey)];
  443. v01 = noise_buf[idx(0, noisey + 1)];
  444. v11 = noise_buf[idx(1, noisey + 1)];
  445. u = orig_u;
  446. noisex = 0;
  447. for (i = 0; i != sx; i++) {
  448. gradient_buf[index++] = interpolate(v00, v10, v01, v11, u, v);
  449. u += step_x;
  450. if (u >= 1.0) {
  451. u -= 1.0;
  452. noisex++;
  453. v00 = v10;
  454. v01 = v11;
  455. v10 = noise_buf[idx(noisex + 1, noisey)];
  456. v11 = noise_buf[idx(noisex + 1, noisey + 1)];
  457. }
  458. }
  459. v += step_y;
  460. if (v >= 1.0) {
  461. v -= 1.0;
  462. noisey++;
  463. }
  464. }
  465. }
  466. #undef idx
  467. #define idx(x, y, z) ((z) * nly * nlx + (y) * nlx + (x))
  468. void Noise::gradientMap3D(
  469. float x, float y, float z,
  470. float step_x, float step_y, float step_z,
  471. s32 seed)
  472. {
  473. float v000, v010, v100, v110;
  474. float v001, v011, v101, v111;
  475. float u, v, w, orig_u, orig_v;
  476. u32 index, i, j, k, noisex, noisey, noisez;
  477. u32 nlx, nly, nlz;
  478. s32 x0, y0, z0;
  479. Interp3dFxn interpolate = (np.flags & NOISE_FLAG_EASED) ?
  480. triLinearInterpolation : triLinearInterpolationNoEase;
  481. x0 = std::floor(x);
  482. y0 = std::floor(y);
  483. z0 = std::floor(z);
  484. u = x - (float)x0;
  485. v = y - (float)y0;
  486. w = z - (float)z0;
  487. orig_u = u;
  488. orig_v = v;
  489. //calculate noise point lattice
  490. nlx = (u32)(u + sx * step_x) + 2;
  491. nly = (u32)(v + sy * step_y) + 2;
  492. nlz = (u32)(w + sz * step_z) + 2;
  493. index = 0;
  494. for (k = 0; k != nlz; k++)
  495. for (j = 0; j != nly; j++)
  496. for (i = 0; i != nlx; i++)
  497. noise_buf[index++] = noise3d(x0 + i, y0 + j, z0 + k, seed);
  498. //calculate interpolations
  499. index = 0;
  500. noisey = 0;
  501. noisez = 0;
  502. for (k = 0; k != sz; k++) {
  503. v = orig_v;
  504. noisey = 0;
  505. for (j = 0; j != sy; j++) {
  506. v000 = noise_buf[idx(0, noisey, noisez)];
  507. v100 = noise_buf[idx(1, noisey, noisez)];
  508. v010 = noise_buf[idx(0, noisey + 1, noisez)];
  509. v110 = noise_buf[idx(1, noisey + 1, noisez)];
  510. v001 = noise_buf[idx(0, noisey, noisez + 1)];
  511. v101 = noise_buf[idx(1, noisey, noisez + 1)];
  512. v011 = noise_buf[idx(0, noisey + 1, noisez + 1)];
  513. v111 = noise_buf[idx(1, noisey + 1, noisez + 1)];
  514. u = orig_u;
  515. noisex = 0;
  516. for (i = 0; i != sx; i++) {
  517. gradient_buf[index++] = interpolate(
  518. v000, v100, v010, v110,
  519. v001, v101, v011, v111,
  520. u, v, w);
  521. u += step_x;
  522. if (u >= 1.0) {
  523. u -= 1.0;
  524. noisex++;
  525. v000 = v100;
  526. v010 = v110;
  527. v100 = noise_buf[idx(noisex + 1, noisey, noisez)];
  528. v110 = noise_buf[idx(noisex + 1, noisey + 1, noisez)];
  529. v001 = v101;
  530. v011 = v111;
  531. v101 = noise_buf[idx(noisex + 1, noisey, noisez + 1)];
  532. v111 = noise_buf[idx(noisex + 1, noisey + 1, noisez + 1)];
  533. }
  534. }
  535. v += step_y;
  536. if (v >= 1.0) {
  537. v -= 1.0;
  538. noisey++;
  539. }
  540. }
  541. w += step_z;
  542. if (w >= 1.0) {
  543. w -= 1.0;
  544. noisez++;
  545. }
  546. }
  547. }
  548. #undef idx
  549. float *Noise::perlinMap2D(float x, float y, float *persistence_map)
  550. {
  551. float f = 1.0, g = 1.0;
  552. size_t bufsize = sx * sy;
  553. x /= np.spread.X;
  554. y /= np.spread.Y;
  555. memset(result, 0, sizeof(float) * bufsize);
  556. if (persistence_map) {
  557. if (!persist_buf)
  558. persist_buf = new float[bufsize];
  559. for (size_t i = 0; i != bufsize; i++)
  560. persist_buf[i] = 1.0;
  561. }
  562. for (size_t oct = 0; oct < np.octaves; oct++) {
  563. gradientMap2D(x * f, y * f,
  564. f / np.spread.X, f / np.spread.Y,
  565. seed + np.seed + oct);
  566. updateResults(g, persist_buf, persistence_map, bufsize);
  567. f *= np.lacunarity;
  568. g *= np.persist;
  569. }
  570. if (std::fabs(np.offset - 0.f) > 0.00001 || std::fabs(np.scale - 1.f) > 0.00001) {
  571. for (size_t i = 0; i != bufsize; i++)
  572. result[i] = result[i] * np.scale + np.offset;
  573. }
  574. return result;
  575. }
  576. float *Noise::perlinMap3D(float x, float y, float z, float *persistence_map)
  577. {
  578. float f = 1.0, g = 1.0;
  579. size_t bufsize = sx * sy * sz;
  580. x /= np.spread.X;
  581. y /= np.spread.Y;
  582. z /= np.spread.Z;
  583. memset(result, 0, sizeof(float) * bufsize);
  584. if (persistence_map) {
  585. if (!persist_buf)
  586. persist_buf = new float[bufsize];
  587. for (size_t i = 0; i != bufsize; i++)
  588. persist_buf[i] = 1.0;
  589. }
  590. for (size_t oct = 0; oct < np.octaves; oct++) {
  591. gradientMap3D(x * f, y * f, z * f,
  592. f / np.spread.X, f / np.spread.Y, f / np.spread.Z,
  593. seed + np.seed + oct);
  594. updateResults(g, persist_buf, persistence_map, bufsize);
  595. f *= np.lacunarity;
  596. g *= np.persist;
  597. }
  598. if (std::fabs(np.offset - 0.f) > 0.00001 || std::fabs(np.scale - 1.f) > 0.00001) {
  599. for (size_t i = 0; i != bufsize; i++)
  600. result[i] = result[i] * np.scale + np.offset;
  601. }
  602. return result;
  603. }
  604. void Noise::updateResults(float g, float *gmap,
  605. const float *persistence_map, size_t bufsize)
  606. {
  607. // This looks very ugly, but it is 50-70% faster than having
  608. // conditional statements inside the loop
  609. if (np.flags & NOISE_FLAG_ABSVALUE) {
  610. if (persistence_map) {
  611. for (size_t i = 0; i != bufsize; i++) {
  612. result[i] += gmap[i] * std::fabs(gradient_buf[i]);
  613. gmap[i] *= persistence_map[i];
  614. }
  615. } else {
  616. for (size_t i = 0; i != bufsize; i++)
  617. result[i] += g * std::fabs(gradient_buf[i]);
  618. }
  619. } else {
  620. if (persistence_map) {
  621. for (size_t i = 0; i != bufsize; i++) {
  622. result[i] += gmap[i] * gradient_buf[i];
  623. gmap[i] *= persistence_map[i];
  624. }
  625. } else {
  626. for (size_t i = 0; i != bufsize; i++)
  627. result[i] += g * gradient_buf[i];
  628. }
  629. }
  630. }