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