noise.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. Minetest-c55
  3. Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <math.h>
  17. #include "noise.h"
  18. #include <iostream>
  19. #include "debug.h"
  20. #define NOISE_MAGIC_X 1619
  21. #define NOISE_MAGIC_Y 31337
  22. #define NOISE_MAGIC_Z 52591
  23. #define NOISE_MAGIC_SEED 1013
  24. double cos_lookup[16] = {
  25. 1.0,0.9238,0.7071,0.3826,0,-0.3826,-0.7071,-0.9238,
  26. 1.0,-0.9238,-0.7071,-0.3826,0,0.3826,0.7071,0.9238
  27. };
  28. double dotProduct(double vx, double vy, double wx, double wy){
  29. return vx*wx+vy*wy;
  30. }
  31. double easeCurve(double t){
  32. return 6*pow(t,5)-15*pow(t,4)+10*pow(t,3);
  33. }
  34. double linearInterpolation(double x0, double x1, double t){
  35. return x0+(x1-x0)*t;
  36. }
  37. double biLinearInterpolation(double x0y0, double x1y0, double x0y1, double x1y1, double x, double y){
  38. double tx = easeCurve(x);
  39. double ty = easeCurve(y);
  40. /*double tx = x;
  41. double ty = y;*/
  42. double u = linearInterpolation(x0y0,x1y0,tx);
  43. double v = linearInterpolation(x0y1,x1y1,tx);
  44. return linearInterpolation(u,v,ty);
  45. }
  46. double triLinearInterpolation(
  47. double v000, double v100, double v010, double v110,
  48. double v001, double v101, double v011, double v111,
  49. double x, double y, double z)
  50. {
  51. /*double tx = easeCurve(x);
  52. double ty = easeCurve(y);
  53. double tz = easeCurve(z);*/
  54. double tx = x;
  55. double ty = y;
  56. double tz = z;
  57. return(
  58. v000*(1-tx)*(1-ty)*(1-tz) +
  59. v100*tx*(1-ty)*(1-tz) +
  60. v010*(1-tx)*ty*(1-tz) +
  61. v110*tx*ty*(1-tz) +
  62. v001*(1-tx)*(1-ty)*tz +
  63. v101*tx*(1-ty)*tz +
  64. v011*(1-tx)*ty*tz +
  65. v111*tx*ty*tz
  66. );
  67. }
  68. double noise2d(int x, int y, int seed)
  69. {
  70. int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
  71. + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
  72. n = (n>>13)^n;
  73. n = (n * (n*n*60493+19990303) + 1376312589) & 0x7fffffff;
  74. return 1.0 - (double)n/1073741824;
  75. }
  76. double noise3d(int x, int y, int z, int seed)
  77. {
  78. int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
  79. + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
  80. n = (n>>13)^n;
  81. n = (n * (n*n*60493+19990303) + 1376312589) & 0x7fffffff;
  82. return 1.0 - (double)n/1073741824;
  83. }
  84. #if 0
  85. double noise2d_gradient(double x, double y, int seed)
  86. {
  87. // Calculate the integer coordinates
  88. int x0 = (x > 0.0 ? (int)x : (int)x - 1);
  89. int y0 = (y > 0.0 ? (int)y : (int)y - 1);
  90. // Calculate the remaining part of the coordinates
  91. double xl = x - (double)x0;
  92. double yl = y - (double)y0;
  93. // Calculate random cosine lookup table indices for the integer corners.
  94. // They are looked up as unit vector gradients from the lookup table.
  95. int n00 = (int)((noise2d(x0, y0, seed)+1)*8);
  96. int n10 = (int)((noise2d(x0+1, y0, seed)+1)*8);
  97. int n01 = (int)((noise2d(x0, y0+1, seed)+1)*8);
  98. int n11 = (int)((noise2d(x0+1, y0+1, seed)+1)*8);
  99. // Make a dot product for the gradients and the positions, to get the values
  100. double s = dotProduct(cos_lookup[n00], cos_lookup[(n00+12)%16], xl, yl);
  101. double u = dotProduct(-cos_lookup[n10], cos_lookup[(n10+12)%16], 1.-xl, yl);
  102. double v = dotProduct(cos_lookup[n01], -cos_lookup[(n01+12)%16], xl, 1.-yl);
  103. double w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11+12)%16], 1.-xl, 1.-yl);
  104. // Interpolate between the values
  105. return biLinearInterpolation(s,u,v,w,xl,yl);
  106. }
  107. #endif
  108. #if 1
  109. double noise2d_gradient(double x, double y, int seed)
  110. {
  111. // Calculate the integer coordinates
  112. int x0 = (x > 0.0 ? (int)x : (int)x - 1);
  113. int y0 = (y > 0.0 ? (int)y : (int)y - 1);
  114. // Calculate the remaining part of the coordinates
  115. double xl = x - (double)x0;
  116. double yl = y - (double)y0;
  117. // Get values for corners of cube
  118. double v00 = noise2d(x0, y0, seed);
  119. double v10 = noise2d(x0+1, y0, seed);
  120. double v01 = noise2d(x0, y0+1, seed);
  121. double v11 = noise2d(x0+1, y0+1, seed);
  122. // Interpolate
  123. return biLinearInterpolation(v00,v10,v01,v11,xl,yl);
  124. }
  125. #endif
  126. double noise3d_gradient(double x, double y, double z, int seed)
  127. {
  128. // Calculate the integer coordinates
  129. int x0 = (x > 0.0 ? (int)x : (int)x - 1);
  130. int y0 = (y > 0.0 ? (int)y : (int)y - 1);
  131. int z0 = (z > 0.0 ? (int)z : (int)z - 1);
  132. // Calculate the remaining part of the coordinates
  133. double xl = x - (double)x0;
  134. double yl = y - (double)y0;
  135. double zl = z - (double)z0;
  136. // Get values for corners of cube
  137. double v000 = noise3d(x0, y0, z0, seed);
  138. double v100 = noise3d(x0+1, y0, z0, seed);
  139. double v010 = noise3d(x0, y0+1, z0, seed);
  140. double v110 = noise3d(x0+1, y0+1, z0, seed);
  141. double v001 = noise3d(x0, y0, z0+1, seed);
  142. double v101 = noise3d(x0+1, y0, z0+1, seed);
  143. double v011 = noise3d(x0, y0+1, z0+1, seed);
  144. double v111 = noise3d(x0+1, y0+1, z0+1, seed);
  145. // Interpolate
  146. return triLinearInterpolation(v000,v100,v010,v110,v001,v101,v011,v111,xl,yl,zl);
  147. }
  148. double noise2d_perlin(double x, double y, int seed,
  149. int octaves, double persistence)
  150. {
  151. double a = 0;
  152. double f = 1.0;
  153. double g = 1.0;
  154. for(int i=0; i<octaves; i++)
  155. {
  156. a += g * noise2d_gradient(x*f, y*f, seed+i);
  157. f *= 2.0;
  158. g *= persistence;
  159. }
  160. return a;
  161. }
  162. double noise2d_perlin_abs(double x, double y, int seed,
  163. int octaves, double persistence)
  164. {
  165. double a = 0;
  166. double f = 1.0;
  167. double g = 1.0;
  168. for(int i=0; i<octaves; i++)
  169. {
  170. a += g * fabs(noise2d_gradient(x*f, y*f, seed+i));
  171. f *= 2.0;
  172. g *= persistence;
  173. }
  174. return a;
  175. }
  176. double noise3d_perlin(double x, double y, double z, int seed,
  177. int octaves, double persistence)
  178. {
  179. double a = 0;
  180. double f = 1.0;
  181. double g = 1.0;
  182. for(int i=0; i<octaves; i++)
  183. {
  184. a += g * noise3d_gradient(x*f, y*f, z*f, seed+i);
  185. f *= 2.0;
  186. g *= persistence;
  187. }
  188. return a;
  189. }
  190. double noise3d_perlin_abs(double x, double y, double z, int seed,
  191. int octaves, double persistence)
  192. {
  193. double a = 0;
  194. double f = 1.0;
  195. double g = 1.0;
  196. for(int i=0; i<octaves; i++)
  197. {
  198. a += g * fabs(noise3d_gradient(x*f, y*f, z*f, seed+i));
  199. f *= 2.0;
  200. g *= persistence;
  201. }
  202. return a;
  203. }
  204. // -1->0, 0->1, 1->0
  205. double contour(double v)
  206. {
  207. v = fabs(v);
  208. if(v >= 1.0)
  209. return 0.0;
  210. return (1.0-v);
  211. }
  212. double noise3d_param(const NoiseParams &param, double x, double y, double z)
  213. {
  214. double s = param.pos_scale;
  215. x /= s;
  216. y /= s;
  217. z /= s;
  218. if(param.type == NOISE_PERLIN)
  219. {
  220. return param.noise_scale*noise3d_perlin(x,y,z, param.seed,
  221. param.octaves,
  222. param.persistence);
  223. }
  224. else if(param.type == NOISE_PERLIN_ABS)
  225. {
  226. return param.noise_scale*noise3d_perlin_abs(x,y,z, param.seed,
  227. param.octaves,
  228. param.persistence);
  229. }
  230. else if(param.type == NOISE_PERLIN_CONTOUR)
  231. {
  232. return contour(param.noise_scale*noise3d_perlin(x,y,z,
  233. param.seed, param.octaves,
  234. param.persistence));
  235. }
  236. else if(param.type == NOISE_PERLIN_CONTOUR_FLIP_YZ)
  237. {
  238. return contour(param.noise_scale*noise3d_perlin(x,z,y,
  239. param.seed, param.octaves,
  240. param.persistence));
  241. }
  242. else assert(0);
  243. }
  244. /*
  245. NoiseBuffer
  246. */
  247. NoiseBuffer::NoiseBuffer():
  248. m_data(NULL)
  249. {
  250. }
  251. NoiseBuffer::~NoiseBuffer()
  252. {
  253. clear();
  254. }
  255. void NoiseBuffer::clear()
  256. {
  257. if(m_data)
  258. delete[] m_data;
  259. m_data = NULL;
  260. m_size_x = 0;
  261. m_size_y = 0;
  262. m_size_z = 0;
  263. }
  264. void NoiseBuffer::create(const NoiseParams &param,
  265. double first_x, double first_y, double first_z,
  266. double last_x, double last_y, double last_z,
  267. double samplelength_x, double samplelength_y, double samplelength_z)
  268. {
  269. clear();
  270. m_start_x = first_x - samplelength_x;
  271. m_start_y = first_y - samplelength_y;
  272. m_start_z = first_z - samplelength_z;
  273. m_samplelength_x = samplelength_x;
  274. m_samplelength_y = samplelength_y;
  275. m_samplelength_z = samplelength_z;
  276. m_size_x = (last_x - m_start_x)/samplelength_x + 2;
  277. m_size_y = (last_y - m_start_y)/samplelength_y + 2;
  278. m_size_z = (last_z - m_start_z)/samplelength_z + 2;
  279. m_data = new double[m_size_x*m_size_y*m_size_z];
  280. for(int x=0; x<m_size_x; x++)
  281. for(int y=0; y<m_size_y; y++)
  282. for(int z=0; z<m_size_z; z++)
  283. {
  284. double xd = (m_start_x + (double)x*m_samplelength_x);
  285. double yd = (m_start_y + (double)y*m_samplelength_y);
  286. double zd = (m_start_z + (double)z*m_samplelength_z);
  287. double a = noise3d_param(param, xd,yd,zd);
  288. intSet(x,y,z, a);
  289. }
  290. }
  291. void NoiseBuffer::multiply(const NoiseParams &param)
  292. {
  293. assert(m_data != NULL);
  294. for(int x=0; x<m_size_x; x++)
  295. for(int y=0; y<m_size_y; y++)
  296. for(int z=0; z<m_size_z; z++)
  297. {
  298. double xd = (m_start_x + (double)x*m_samplelength_x);
  299. double yd = (m_start_y + (double)y*m_samplelength_y);
  300. double zd = (m_start_z + (double)z*m_samplelength_z);
  301. double a = noise3d_param(param, xd,yd,zd);
  302. intMultiply(x,y,z, a);
  303. }
  304. }
  305. // Deprecated
  306. void NoiseBuffer::create(int seed, int octaves, double persistence,
  307. bool abs,
  308. double first_x, double first_y, double first_z,
  309. double last_x, double last_y, double last_z,
  310. double samplelength_x, double samplelength_y, double samplelength_z)
  311. {
  312. NoiseParams param;
  313. param.type = abs ? NOISE_PERLIN_ABS : NOISE_PERLIN;
  314. param.seed = seed;
  315. param.octaves = octaves;
  316. param.persistence = persistence;
  317. create(param, first_x, first_y, first_z,
  318. last_x, last_y, last_z,
  319. samplelength_x, samplelength_y, samplelength_z);
  320. }
  321. void NoiseBuffer::intSet(int x, int y, int z, double d)
  322. {
  323. int i = m_size_x*m_size_y*z + m_size_x*y + x;
  324. assert(i >= 0);
  325. assert(i < m_size_x*m_size_y*m_size_z);
  326. m_data[i] = d;
  327. }
  328. void NoiseBuffer::intMultiply(int x, int y, int z, double d)
  329. {
  330. int i = m_size_x*m_size_y*z + m_size_x*y + x;
  331. assert(i >= 0);
  332. assert(i < m_size_x*m_size_y*m_size_z);
  333. m_data[i] = m_data[i] * d;
  334. }
  335. double NoiseBuffer::intGet(int x, int y, int z)
  336. {
  337. int i = m_size_x*m_size_y*z + m_size_x*y + x;
  338. assert(i >= 0);
  339. assert(i < m_size_x*m_size_y*m_size_z);
  340. return m_data[i];
  341. }
  342. double NoiseBuffer::get(double x, double y, double z)
  343. {
  344. x -= m_start_x;
  345. y -= m_start_y;
  346. z -= m_start_z;
  347. x /= m_samplelength_x;
  348. y /= m_samplelength_y;
  349. z /= m_samplelength_z;
  350. // Calculate the integer coordinates
  351. int x0 = (x > 0.0 ? (int)x : (int)x - 1);
  352. int y0 = (y > 0.0 ? (int)y : (int)y - 1);
  353. int z0 = (z > 0.0 ? (int)z : (int)z - 1);
  354. // Calculate the remaining part of the coordinates
  355. double xl = x - (double)x0;
  356. double yl = y - (double)y0;
  357. double zl = z - (double)z0;
  358. // Get values for corners of cube
  359. double v000 = intGet(x0, y0, z0);
  360. double v100 = intGet(x0+1, y0, z0);
  361. double v010 = intGet(x0, y0+1, z0);
  362. double v110 = intGet(x0+1, y0+1, z0);
  363. double v001 = intGet(x0, y0, z0+1);
  364. double v101 = intGet(x0+1, y0, z0+1);
  365. double v011 = intGet(x0, y0+1, z0+1);
  366. double v111 = intGet(x0+1, y0+1, z0+1);
  367. // Interpolate
  368. return triLinearInterpolation(v000,v100,v010,v110,v001,v101,v011,v111,xl,yl,zl);
  369. }