]> git.lizzy.rs Git - minetest.git/blob - src/noise.cpp
Cleaned & enhanced noise object management
[minetest.git] / src / noise.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <math.h>
21 #include "noise.h"
22 #include <iostream>
23 #include "debug.h"
24 #include "util/numeric.h"
25
26 #define NOISE_MAGIC_X    1619
27 #define NOISE_MAGIC_Y    31337
28 #define NOISE_MAGIC_Z    52591
29 #define NOISE_MAGIC_SEED 1013
30
31 float cos_lookup[16] = {
32         1.0,  0.9238,  0.7071,  0.3826, 0, -0.3826, -0.7071, -0.9238,
33         1.0, -0.9238, -0.7071, -0.3826, 0,  0.3826,  0.7071,  0.9238
34 };
35
36
37 ///////////////////////////////////////////////////////////////////////////////
38
39
40 //noise poly:  p(n) = 60493n^3 + 19990303n + 137612589
41 float noise2d(int x, int y, int seed) {
42         int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
43                         + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
44         n = (n >> 13) ^ n;
45         n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
46         return 1.f - (float)n / 0x40000000;
47 }
48
49
50 float noise3d(int x, int y, int z, int seed) {
51         int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
52                         + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
53         n = (n >> 13) ^ n;
54         n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
55         return 1.f - (float)n / 0x40000000;
56 }
57
58
59 float dotProduct(float vx, float vy, float wx, float wy) {
60         return vx * wx + vy * wy;
61 }
62
63
64 inline float linearInterpolation(float v0, float v1, float t) {
65     return v0 + (v1 - v0) * t;
66 }
67
68
69 float biLinearInterpolation(float v00, float v10,
70                                                         float v01, float v11,
71                                                         float x, float y) {
72     float tx = easeCurve(x);
73     float ty = easeCurve(y);
74     float u = linearInterpolation(v00, v10, tx);
75     float v = linearInterpolation(v01, v11, tx);
76     return linearInterpolation(u, v, ty);
77 }
78
79
80 float biLinearInterpolationNoEase(float x0y0, float x1y0,
81                                                                   float x0y1, float x1y1,
82                                                                   float x, float y) {
83     float u = linearInterpolation(x0y0, x1y0, x);
84     float v = linearInterpolation(x0y1, x1y1, x);
85     return linearInterpolation(u, v, y);
86 }
87
88
89 float triLinearInterpolation(
90                 float v000, float v100, float v010, float v110,
91                 float v001, float v101, float v011, float v111,
92                 float x, float y, float z) {
93         float u = biLinearInterpolationNoEase(v000, v100, v010, v110, x, y);
94         float v = biLinearInterpolationNoEase(v001, v101, v011, v111, x, y);
95         return linearInterpolation(u, v, z);
96 }
97
98
99 #if 0
100 float triLinearInterpolation(
101                 float v000, float v100, float v010, float v110,
102                 float v001, float v101, float v011, float v111,
103                 float x, float y, float z)
104 {
105         /*float tx = easeCurve(x);
106         float ty = easeCurve(y);
107         float tz = easeCurve(z);*/
108         float tx = x;
109         float ty = y;
110         float tz = z;
111         return(
112                 v000 * (1 - tx) * (1 - ty) * (1 - tz) +
113                 v100 * tx * (1 - ty) * (1 - tz) +
114                 v010 * (1 - tx) * ty * (1 - tz) +
115                 v110 * tx * ty * (1 - tz) +
116                 v001 * (1 - tx) * (1 - ty) * tz +
117                 v101 * tx * (1 - ty) * tz +
118                 v011 * (1 - tx) * ty * tz +
119                 v111 * tx * ty * tz
120         );
121 }
122 #endif
123
124
125 #if 0
126 float noise2d_gradient(float x, float y, 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         // Calculate the remaining part of the coordinates
132         float xl = x - (float)x0;
133         float yl = y - (float)y0;
134         // Calculate random cosine lookup table indices for the integer corners.
135         // They are looked up as unit vector gradients from the lookup table.
136         int n00 = (int)((noise2d(x0, y0, seed)+1)*8);
137         int n10 = (int)((noise2d(x0+1, y0, seed)+1)*8);
138         int n01 = (int)((noise2d(x0, y0+1, seed)+1)*8);
139         int n11 = (int)((noise2d(x0+1, y0+1, seed)+1)*8);
140         // Make a dot product for the gradients and the positions, to get the values
141         float s = dotProduct(cos_lookup[n00], cos_lookup[(n00+12)%16], xl, yl);
142         float u = dotProduct(-cos_lookup[n10], cos_lookup[(n10+12)%16], 1.-xl, yl);
143         float v = dotProduct(cos_lookup[n01], -cos_lookup[(n01+12)%16], xl, 1.-yl);
144         float w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11+12)%16], 1.-xl, 1.-yl);
145         // Interpolate between the values
146         return biLinearInterpolation(s,u,v,w,xl,yl);
147 }
148 #endif
149
150
151 float noise2d_gradient(float x, float y, int seed)
152 {
153         // Calculate the integer coordinates
154         int x0 = myfloor(x);
155         int y0 = myfloor(y);
156         // Calculate the remaining part of the coordinates
157         float xl = x - (float)x0;
158         float yl = y - (float)y0;
159         // Get values for corners of square
160         float v00 = noise2d(x0, y0, seed);
161         float v10 = noise2d(x0+1, y0, seed);
162         float v01 = noise2d(x0, y0+1, seed);
163         float v11 = noise2d(x0+1, y0+1, seed);
164         // Interpolate
165         return biLinearInterpolation(v00,v10,v01,v11,xl,yl);
166 }
167
168
169 float noise3d_gradient(float x, float y, float z, int seed)
170 {
171         // Calculate the integer coordinates
172         int x0 = myfloor(x);
173         int y0 = myfloor(y);
174         int z0 = myfloor(z);
175         // Calculate the remaining part of the coordinates
176         float xl = x - (float)x0;
177         float yl = y - (float)y0;
178         float zl = z - (float)z0;
179         // Get values for corners of cube
180         float v000 = noise3d(x0,     y0,     z0,     seed);
181         float v100 = noise3d(x0 + 1, y0,     z0,     seed);
182         float v010 = noise3d(x0,     y0 + 1, z0,     seed);
183         float v110 = noise3d(x0 + 1, y0 + 1, z0,     seed);
184         float v001 = noise3d(x0,     y0,     z0 + 1, seed);
185         float v101 = noise3d(x0 + 1, y0,     z0 + 1, seed);
186         float v011 = noise3d(x0,     y0 + 1, z0 + 1, seed);
187         float v111 = noise3d(x0 + 1, y0 + 1, z0 + 1, seed);
188         // Interpolate
189         return triLinearInterpolation(v000, v100, v010, v110,
190                                                                   v001, v101, v011, v111,
191                                                                   xl, yl, zl);
192 }
193
194
195 float noise2d_perlin(float x, float y, int seed,
196                 int octaves, float persistence)
197 {
198         float a = 0;
199         float f = 1.0;
200         float g = 1.0;
201         for (int i = 0; i < octaves; i++)
202         {
203                 a += g * noise2d_gradient(x * f, y * f, seed + i);
204                 f *= 2.0;
205                 g *= persistence;
206         }
207         return a;
208 }
209
210
211 float noise2d_perlin_abs(float x, float y, int seed,
212                 int octaves, float persistence)
213 {
214         float a = 0;
215         float f = 1.0;
216         float g = 1.0;
217         for (int i = 0; i < octaves; i++)
218         {
219                 a += g * fabs(noise2d_gradient(x * f, y * f, seed + i));
220                 f *= 2.0;
221                 g *= persistence;
222         }
223         return a;
224 }
225
226
227 float noise3d_perlin(float x, float y, float z, int seed,
228                 int octaves, float persistence)
229 {
230         float a = 0;
231         float f = 1.0;
232         float g = 1.0;
233         for (int i = 0; i < octaves; i++)
234         {
235                 a += g * noise3d_gradient(x * f, y * f, z * f, seed + i);
236                 f *= 2.0;
237                 g *= persistence;
238         }
239         return a;
240 }
241
242
243 float noise3d_perlin_abs(float x, float y, float z, int seed,
244                 int octaves, float persistence)
245 {
246         float a = 0;
247         float f = 1.0;
248         float g = 1.0;
249         for (int i = 0; i < octaves; i++)
250         {
251                 a += g * fabs(noise3d_gradient(x * f, y * f, z * f, seed + i));
252                 f *= 2.0;
253                 g *= persistence;
254         }
255         return a;
256 }
257
258
259 // -1->0, 0->1, 1->0
260 float contour(float v)
261 {
262         v = fabs(v);
263         if(v >= 1.0)
264                 return 0.0;
265         return (1.0-v);
266 }
267
268
269 ///////////////////////// [ New perlin stuff ] ////////////////////////////
270
271
272 Noise::Noise(NoiseParams *np, int seed, int sx, int sy) {
273         init(np, seed, sx, sy, 1);
274 }
275
276
277 Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz) {
278         init(np, seed, sx, sy, sz);
279 }
280
281
282 void Noise::init(NoiseParams *np, int seed, int sx, int sy, int sz) {
283         this->np   = np;
284         this->seed = seed;
285         this->sx   = sx;
286         this->sy   = sy;
287         this->sz   = sz;
288
289         this->noisebuf = NULL;
290         resizeNoiseBuf(sz > 1);
291
292         this->buf      = new float[sx * sy * sz];
293         this->result   = new float[sx * sy * sz];
294 }
295
296
297 Noise::~Noise() {
298         delete[] buf;
299         delete[] result;
300         delete[] noisebuf;
301 }
302
303
304 void Noise::setSize(int sx, int sy) {
305         setSize(sx, sy, 1);
306 }
307
308
309 void Noise::setSize(int sx, int sy, int sz) {
310         this->sx = sx;
311         this->sy = sy;
312         this->sz = sz;
313
314         resizeNoiseBuf(sz > 1);
315
316         delete[] buf;
317         delete[] result;
318 }
319
320
321 void Noise::setSpreadFactor(v3f spread) {
322         this->np->spread = spread;
323
324         resizeNoiseBuf(sz > 1);
325 }
326
327
328 void Noise::setOctaves(int octaves) {
329         this->np->octaves = octaves;
330
331         resizeNoiseBuf(sz > 1);
332 }
333
334
335 void Noise::resizeNoiseBuf(bool is3d) {
336         int nlx, nly, nlz;
337         float ofactor;
338
339         //maximum possible spread value factor
340         ofactor = (float)(1 << (np->octaves - 1));
341
342         //noise lattice point count
343         //(int)(sz * spread * ofactor) is # of lattice points crossed due to length
344         // + 2 for the two initial endpoints
345         // + 1 for potentially crossing a boundary due to offset
346         nlx = (int)(sx * ofactor / np->spread.X) + 3;
347         nly = (int)(sy * ofactor / np->spread.Y) + 3;
348         nlz = is3d ? (int)(sz * ofactor / np->spread.Z) + 3 : 1;
349
350         if (noisebuf)
351                 delete[] noisebuf;
352         noisebuf = new float[nlx * nly * nlz];
353 }
354
355
356 /*
357  * NB:  This algorithm is not optimal in terms of space complexity.  The entire
358  * integer lattice of noise points could be done as 2 lines instead, and for 3D,
359  * 2 lines + 2 planes.
360  * However, this would require the noise calls to be interposed with the
361  * interpolation loops, which may trash the icache, leading to lower overall
362  * performance.
363  * Another optimization that could save half as many noise calls is to carry over
364  * values from the previous noise lattice as midpoints in the new lattice for the
365  * next octave.
366  */
367 void Noise::gradientMap2D(float x, float y, float step_x, float step_y, int seed) {
368         float v00, v01, v10, v11, u, v, orig_u;
369         int index, i, j, x0, y0, noisex, noisey;
370         int nlx, nly;
371
372         x0 = floor(x);
373         y0 = floor(y);
374         u = x - (float)x0;
375         v = y - (float)y0;
376         orig_u = u;
377
378         //calculate noise point lattice
379         nlx = (int)(u + sx * step_x) + 2;
380         nly = (int)(v + sy * step_y) + 2;
381         index = 0;
382         for (j = 0; j != nly; j++)
383                 for (i = 0; i != nlx; i++)
384                         noisebuf[index++] = noise2d(x0 + i, y0 + j, seed);
385
386         //calculate interpolations
387         noisey = 0;
388         for (j = 0; j != sy; j++) {
389                 v00 = noisebuf[noisey * nlx];
390                 v10 = noisebuf[noisey * nlx + 1];
391                 v01 = noisebuf[(noisey + 1) * nlx];
392                 v11 = noisebuf[(noisey + 1) * nlx + 1];
393
394                 u = orig_u;
395                 noisex = 0;
396                 for (i = 0; i != sx; i++) {
397                         buf[j * sx + i] = biLinearInterpolation(v00, v10, v01, v11, u, v);
398                         u += step_x;
399                         if (u >= 1.0) {
400                                 u -= 1.0;
401                                 noisex++;
402                                 v00 = v10;
403                                 v01 = v11;
404                                 v10 = noisebuf[noisey * nlx + noisex + 1];
405                                 v11 = noisebuf[(noisey + 1) * nlx + noisex + 1];
406                         }
407                 }
408
409                 v += step_y;
410                 if (v >= 1.0) {
411                         v -= 1.0;
412                         noisey++;
413                 }
414         }
415 }
416
417
418 void Noise::gradientMap3D(float x, float y, float z,
419                                                   float step_x, float step_y, float step_z,
420                                                   int seed) {
421         float v000, v010, v100, v110;
422         float v001, v011, v101, v111;
423         float u, v, w, orig_u, orig_w;
424         int index, i, j, k, x0, y0, z0, noisex, noisey, noisez;
425         int nlx, nly, nlz;
426
427         x0 = floor(x);
428         y0 = floor(y);
429         z0 = floor(z);
430         u = x - (float)x0;
431         v = y - (float)y0;
432         w = z - (float)z0;
433         orig_u = u;
434         orig_w = w;
435
436         //calculate noise point lattice
437         nlx = (int)(u + sx * step_x) + 2;
438         nly = (int)(v + sy * step_y) + 2;
439         nlz = (int)(v + sy * step_z) + 2;
440         index = 0;
441         for (k = 0; k != nlz; k++)
442                 for (j = 0; j != nly; j++)
443                         for (i = 0; i != nlx; i++)
444                                 noisebuf[index++] = noise3d(x0 + i, y0 + j, z0 + k, seed);
445
446 #define index(x, y, z) ((z) * nly * nlx + (y) * nlx + (x))
447
448         //calculate interpolations
449         noisey = 0;
450         noisez = 0;
451         for (k = 0; k != sz; k++) {
452                 v000 = noisebuf[index(0, noisey,     noisez)];
453                 v100 = noisebuf[index(1, noisey,     noisez)];
454                 v010 = noisebuf[index(0, noisey + 1, noisez)];
455                 v110 = noisebuf[index(1, noisey + 1, noisez)];
456                 v001 = noisebuf[index(0, noisey,     noisez + 1)];
457                 v101 = noisebuf[index(1, noisey,     noisez + 1)];
458                 v011 = noisebuf[index(0, noisey + 1, noisez + 1)];
459                 v111 = noisebuf[index(1, noisey + 1, noisez + 1)];
460
461                 w = orig_w;
462                 noisey = 0;
463                 for (j = 0; j != sy; j++) {
464                         v000 = noisebuf[index(0, noisey,     noisez)];
465                         v100 = noisebuf[index(1, noisey,     noisez)];
466                         v010 = noisebuf[index(0, noisey + 1, noisez)];
467                         v110 = noisebuf[index(1, noisey + 1, noisez)];
468                         v001 = noisebuf[index(0, noisey,     noisez + 1)];
469                         v101 = noisebuf[index(1, noisey,     noisez + 1)];
470                         v011 = noisebuf[index(0, noisey + 1, noisez + 1)];
471                         v111 = noisebuf[index(1, noisey + 1, noisez + 1)];
472
473                         u = orig_u;
474                         noisex = 0;
475                         for (i = 0; i != sx; i++) {
476                                 buf[j * sx + i] = triLinearInterpolation(
477                                                                         v000, v100, v010, v110,
478                                                                         v001, v101, v011, v111,
479                                                                         u, v, w);
480                                 u += step_x;
481                                 if (u >= 1.0) {
482                                         u -= 1.0;
483                                         noisex++;
484                                         v000 = v100;
485                                         v010 = v110;
486                                         v100 = noisebuf[index(noisex + 1, noisey,     noisez)];
487                                         v110 = noisebuf[index(noisex + 1, noisey + 1, noisez)];
488                                         v001 = v101;
489                                         v011 = v111;
490                                         v101 = noisebuf[index(noisex + 1, noisey,     noisez + 1)];
491                                         v111 = noisebuf[index(noisex + 1, noisey + 1, noisez + 1)];
492                                 }
493                         }
494
495                         v += step_y;
496                         if (v >= 1.0) {
497                                 v -= 1.0;
498                                 noisey++;
499                         }
500                 }
501
502                 w += step_z;
503                 if (w >= 1.0) {
504                         w -= 1.0;
505                         noisez++;
506                 }
507         }
508 }
509
510
511 float *Noise::perlinMap2D(float x, float y) {
512         float a = 0.0, f = 1.0, g = 1.0;
513         int i, j, index, oct;
514
515         x /= np->spread.X;
516         y /= np->spread.Y;
517
518         memset(result, 0, sizeof(float) * sx * sy);
519
520         for (oct = 0; oct < np->octaves; oct++) {
521                 gradientMap2D(x * f, y * f,
522                         f / np->spread.X, f / np->spread.Y,
523                         seed + np->seed + oct);
524
525                 index = 0;
526                 for (j = 0; j != sy; j++) {
527                         for (i = 0; i != sx; i++) {
528                                 result[index] += g * buf[index];
529                                 index++;
530                         }
531                 }
532
533                 f *= 2.0;
534                 g *= np->persist;
535         }
536
537         return result;
538 }
539
540
541 float *Noise::perlinMap3D(float x, float y, float z) {
542         float a = 0.0, f = 1.0, g = 1.0;
543         int i, j, k, index, oct;
544
545         x /= np->spread.X;
546         y /= np->spread.Y;
547         z /= np->spread.Z;
548
549         memset(result, 0, sizeof(float) * sx * sy * sz);
550
551         for (oct = 0; oct < np->octaves; oct++) {
552                 gradientMap3D(x * f, y * f, z * f,
553                         f / np->spread.X, f / np->spread.Y, f / np->spread.Z,
554                         seed + np->seed + oct);
555
556                 index = 0;
557                 for (k = 0; k != sz; k++) {
558                         for (j = 0; j != sy; j++) {
559                                 for (i = 0; i != sx; i++) {
560                                         result[index] += g * buf[index];
561                                         index++;
562                                 }
563                         }
564                 }
565
566                 f *= 2.0;
567                 g *= np->persist;
568         }
569
570         return result;
571 }
572
573
574 void Noise::transformNoiseMap() {
575         int i = 0;
576         for (int z = 0; z != sz; z++) {
577                 for (int y = 0; y != sy; y++) {
578                         for (int x = 0; x != sx; x++) {
579                                 result[i] = result[i] * np->scale + np->offset;
580                                 i++;
581                         }
582                 }
583         }
584 }