3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
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.
24 #include <string.h> // memset
26 #include "util/numeric.h"
28 #define NOISE_MAGIC_X 1619
29 #define NOISE_MAGIC_Y 31337
30 #define NOISE_MAGIC_Z 52591
31 #define NOISE_MAGIC_SEED 1013
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
39 ///////////////////////////////////////////////////////////////////////////////
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;
47 n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
48 return 1.f - (float)n / 0x40000000;
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;
56 n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
57 return 1.f - (float)n / 0x40000000;
61 float dotProduct(float vx, float vy, float wx, float wy) {
62 return vx * wx + vy * wy;
66 inline float linearInterpolation(float v0, float v1, float t) {
67 return v0 + (v1 - v0) * t;
71 float biLinearInterpolation(float v00, float v10,
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);
82 float biLinearInterpolationNoEase(float x0y0, float x1y0,
83 float x0y1, float x1y1,
85 float u = linearInterpolation(x0y0, x1y0, x);
86 float v = linearInterpolation(x0y1, x1y1, x);
87 return linearInterpolation(u, v, y);
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);
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)
107 /*float tx = easeCurve(x);
108 float ty = easeCurve(y);
109 float tz = easeCurve(z);*/
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 +
128 float noise2d_gradient(float x, float y, int seed)
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);
153 float noise2d_gradient(float x, float y, int seed)
155 // Calculate the integer coordinates
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);
167 return biLinearInterpolation(v00,v10,v01,v11,xl,yl);
171 float noise3d_gradient(float x, float y, float z, int seed)
173 // Calculate the integer coordinates
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);
191 return triLinearInterpolation(v000, v100, v010, v110,
192 v001, v101, v011, v111,
197 float noise2d_perlin(float x, float y, int seed,
198 int octaves, float persistence)
203 for (int i = 0; i < octaves; i++)
205 a += g * noise2d_gradient(x * f, y * f, seed + i);
213 float noise2d_perlin_abs(float x, float y, int seed,
214 int octaves, float persistence)
219 for (int i = 0; i < octaves; i++)
221 a += g * fabs(noise2d_gradient(x * f, y * f, seed + i));
229 float noise3d_perlin(float x, float y, float z, int seed,
230 int octaves, float persistence)
235 for (int i = 0; i < octaves; i++)
237 a += g * noise3d_gradient(x * f, y * f, z * f, seed + i);
245 float noise3d_perlin_abs(float x, float y, float z, int seed,
246 int octaves, float persistence)
251 for (int i = 0; i < octaves; i++)
253 a += g * fabs(noise3d_gradient(x * f, y * f, z * f, seed + i));
262 float contour(float v)
271 ///////////////////////// [ New perlin stuff ] ////////////////////////////
274 Noise::Noise(NoiseParams *np, int seed, int sx, int sy) {
275 init(np, seed, sx, sy, 1);
279 Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz) {
280 init(np, seed, sx, sy, sz);
284 void Noise::init(NoiseParams *np, int seed, int sx, int sy, int sz) {
291 this->noisebuf = NULL;
292 resizeNoiseBuf(sz > 1);
294 this->buf = new float[sx * sy * sz];
295 this->result = new float[sx * sy * sz];
306 void Noise::setSize(int sx, int sy) {
311 void Noise::setSize(int sx, int sy, int sz) {
316 this->noisebuf = NULL;
317 resizeNoiseBuf(sz > 1);
321 this->buf = new float[sx * sy * sz];
322 this->result = new float[sx * sy * sz];
326 void Noise::setSpreadFactor(v3f spread) {
327 this->np->spread = spread;
329 resizeNoiseBuf(sz > 1);
333 void Noise::setOctaves(int octaves) {
334 this->np->octaves = octaves;
336 resizeNoiseBuf(sz > 1);
340 void Noise::resizeNoiseBuf(bool is3d) {
344 //maximum possible spread value factor
345 ofactor = (float)(1 << (np->octaves - 1));
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;
357 noisebuf = new float[nlx * nly * nlz];
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
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
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;
384 //calculate noise point lattice
385 nlx = (int)(u + sx * step_x) + 2;
386 nly = (int)(v + sy * step_y) + 2;
388 for (j = 0; j != nly; j++)
389 for (i = 0; i != nlx; i++)
390 noisebuf[index++] = noise2d(x0 + i, y0 + j, seed);
392 //calculate interpolations
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)];
403 for (i = 0; i != sx; i++) {
404 buf[index++] = biLinearInterpolation(v00, v10, v01, v11, u, v);
411 v10 = noisebuf[idx(noisex + 1, noisey)];
412 v11 = noisebuf[idx(noisex + 1, noisey + 1)];
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,
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;
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;
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);
455 //calculate interpolations
459 for (k = 0; k != sz; k++) {
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)];
474 for (i = 0; i != sx; i++) {
475 buf[index++] = triLinearInterpolation(
476 v000, v100, v010, v110,
477 v001, v101, v011, v111,
485 v100 = noisebuf[idx(noisex + 1, noisey, noisez)];
486 v110 = noisebuf[idx(noisex + 1, noisey + 1, noisez)];
489 v101 = noisebuf[idx(noisex + 1, noisey, noisez + 1)];
490 v111 = noisebuf[idx(noisex + 1, noisey + 1, noisez + 1)];
511 float *Noise::perlinMap2D(float x, float y) {
512 float f = 1.0, g = 1.0;
513 int i, j, index, oct;
518 memset(result, 0, sizeof(float) * sx * sy);
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);
526 for (j = 0; j != sy; j++) {
527 for (i = 0; i != sx; i++) {
528 result[index] += g * buf[index];
541 float *Noise::perlinMap2DModulated(float x, float y, float *persist_map) {
543 int i, j, index, oct;
548 memset(result, 0, sizeof(float) * sx * sy);
550 float *g = new float[sx * sy];
551 for (index = 0; index != sx * sy; index++)
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);
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];
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;
584 memset(result, 0, sizeof(float) * sx * sy * sz);
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);
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];
609 void Noise::transformNoiseMap() {
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;