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.
25 #include "util/numeric.h"
27 #define NOISE_MAGIC_X 1619
28 #define NOISE_MAGIC_Y 31337
29 #define NOISE_MAGIC_Z 52591
30 #define NOISE_MAGIC_SEED 1013
32 float cos_lookup[16] = {
33 1.0, 0.9238, 0.7071, 0.3826, 0, -0.3826, -0.7071, -0.9238,
34 1.0, -0.9238, -0.7071, -0.3826, 0, 0.3826, 0.7071, 0.9238
38 ///////////////////////////////////////////////////////////////////////////////
41 //noise poly: p(n) = 60493n^3 + 19990303n + 137612589
42 float noise2d(int x, int y, int seed) {
43 int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
44 + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
46 n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
47 return 1.f - (float)n / 0x40000000;
51 float noise3d(int x, int y, int z, int seed) {
52 int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
53 + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
55 n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
56 return 1.f - (float)n / 0x40000000;
60 float dotProduct(float vx, float vy, float wx, float wy) {
61 return vx * wx + vy * wy;
65 inline float linearInterpolation(float v0, float v1, float t) {
66 return v0 + (v1 - v0) * t;
70 float biLinearInterpolation(float v00, float v10,
73 float tx = easeCurve(x);
74 float ty = easeCurve(y);
75 float u = linearInterpolation(v00, v10, tx);
76 float v = linearInterpolation(v01, v11, tx);
77 return linearInterpolation(u, v, ty);
81 float biLinearInterpolationNoEase(float x0y0, float x1y0,
82 float x0y1, float x1y1,
84 float u = linearInterpolation(x0y0, x1y0, x);
85 float v = linearInterpolation(x0y1, x1y1, x);
86 return linearInterpolation(u, v, y);
90 float triLinearInterpolation(
91 float v000, float v100, float v010, float v110,
92 float v001, float v101, float v011, float v111,
93 float x, float y, float z) {
94 float u = biLinearInterpolationNoEase(v000, v100, v010, v110, x, y);
95 float v = biLinearInterpolationNoEase(v001, v101, v011, v111, x, y);
96 return linearInterpolation(u, v, z);
101 float triLinearInterpolation(
102 float v000, float v100, float v010, float v110,
103 float v001, float v101, float v011, float v111,
104 float x, float y, float z)
106 /*float tx = easeCurve(x);
107 float ty = easeCurve(y);
108 float tz = easeCurve(z);*/
113 v000 * (1 - tx) * (1 - ty) * (1 - tz) +
114 v100 * tx * (1 - ty) * (1 - tz) +
115 v010 * (1 - tx) * ty * (1 - tz) +
116 v110 * tx * ty * (1 - tz) +
117 v001 * (1 - tx) * (1 - ty) * tz +
118 v101 * tx * (1 - ty) * tz +
119 v011 * (1 - tx) * ty * tz +
127 float noise2d_gradient(float x, float y, int seed)
129 // Calculate the integer coordinates
130 int x0 = (x > 0.0 ? (int)x : (int)x - 1);
131 int y0 = (y > 0.0 ? (int)y : (int)y - 1);
132 // Calculate the remaining part of the coordinates
133 float xl = x - (float)x0;
134 float yl = y - (float)y0;
135 // Calculate random cosine lookup table indices for the integer corners.
136 // They are looked up as unit vector gradients from the lookup table.
137 int n00 = (int)((noise2d(x0, y0, seed)+1)*8);
138 int n10 = (int)((noise2d(x0+1, y0, seed)+1)*8);
139 int n01 = (int)((noise2d(x0, y0+1, seed)+1)*8);
140 int n11 = (int)((noise2d(x0+1, y0+1, seed)+1)*8);
141 // Make a dot product for the gradients and the positions, to get the values
142 float s = dotProduct(cos_lookup[n00], cos_lookup[(n00+12)%16], xl, yl);
143 float u = dotProduct(-cos_lookup[n10], cos_lookup[(n10+12)%16], 1.-xl, yl);
144 float v = dotProduct(cos_lookup[n01], -cos_lookup[(n01+12)%16], xl, 1.-yl);
145 float w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11+12)%16], 1.-xl, 1.-yl);
146 // Interpolate between the values
147 return biLinearInterpolation(s,u,v,w,xl,yl);
152 float noise2d_gradient(float x, float y, int seed)
154 // Calculate the integer coordinates
157 // Calculate the remaining part of the coordinates
158 float xl = x - (float)x0;
159 float yl = y - (float)y0;
160 // Get values for corners of square
161 float v00 = noise2d(x0, y0, seed);
162 float v10 = noise2d(x0+1, y0, seed);
163 float v01 = noise2d(x0, y0+1, seed);
164 float v11 = noise2d(x0+1, y0+1, seed);
166 return biLinearInterpolation(v00,v10,v01,v11,xl,yl);
170 float noise3d_gradient(float x, float y, float z, int seed)
172 // Calculate the integer coordinates
176 // Calculate the remaining part of the coordinates
177 float xl = x - (float)x0;
178 float yl = y - (float)y0;
179 float zl = z - (float)z0;
180 // Get values for corners of cube
181 float v000 = noise3d(x0, y0, z0, seed);
182 float v100 = noise3d(x0 + 1, y0, z0, seed);
183 float v010 = noise3d(x0, y0 + 1, z0, seed);
184 float v110 = noise3d(x0 + 1, y0 + 1, z0, seed);
185 float v001 = noise3d(x0, y0, z0 + 1, seed);
186 float v101 = noise3d(x0 + 1, y0, z0 + 1, seed);
187 float v011 = noise3d(x0, y0 + 1, z0 + 1, seed);
188 float v111 = noise3d(x0 + 1, y0 + 1, z0 + 1, seed);
190 return triLinearInterpolation(v000, v100, v010, v110,
191 v001, v101, v011, v111,
196 float noise2d_perlin(float x, float y, int seed,
197 int octaves, float persistence)
202 for (int i = 0; i < octaves; i++)
204 a += g * noise2d_gradient(x * f, y * f, seed + i);
212 float noise2d_perlin_abs(float x, float y, int seed,
213 int octaves, float persistence)
218 for (int i = 0; i < octaves; i++)
220 a += g * fabs(noise2d_gradient(x * f, y * f, seed + i));
228 float noise3d_perlin(float x, float y, float z, int seed,
229 int octaves, float persistence)
234 for (int i = 0; i < octaves; i++)
236 a += g * noise3d_gradient(x * f, y * f, z * f, seed + i);
244 float noise3d_perlin_abs(float x, float y, float z, int seed,
245 int octaves, float persistence)
250 for (int i = 0; i < octaves; i++)
252 a += g * fabs(noise3d_gradient(x * f, y * f, z * f, seed + i));
261 float contour(float v)
270 ///////////////////////// [ New perlin stuff ] ////////////////////////////
273 Noise::Noise(NoiseParams *np, int seed, int sx, int sy) {
274 init(np, seed, sx, sy, 1);
278 Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz) {
279 init(np, seed, sx, sy, sz);
283 void Noise::init(NoiseParams *np, int seed, int sx, int sy, int sz) {
290 this->noisebuf = NULL;
291 resizeNoiseBuf(sz > 1);
293 this->buf = new float[sx * sy * sz];
294 this->result = new float[sx * sy * sz];
305 void Noise::setSize(int sx, int sy) {
310 void Noise::setSize(int sx, int sy, int sz) {
315 this->noisebuf = NULL;
316 resizeNoiseBuf(sz > 1);
320 this->buf = new float[sx * sy * sz];
321 this->result = new float[sx * sy * sz];
325 void Noise::setSpreadFactor(v3f spread) {
326 this->np->spread = spread;
328 resizeNoiseBuf(sz > 1);
332 void Noise::setOctaves(int octaves) {
333 this->np->octaves = octaves;
335 resizeNoiseBuf(sz > 1);
339 void Noise::resizeNoiseBuf(bool is3d) {
343 //maximum possible spread value factor
344 ofactor = (float)(1 << (np->octaves - 1));
346 //noise lattice point count
347 //(int)(sz * spread * ofactor) is # of lattice points crossed due to length
348 // + 2 for the two initial endpoints
349 // + 1 for potentially crossing a boundary due to offset
350 nlx = (int)(sx * ofactor / np->spread.X) + 3;
351 nly = (int)(sy * ofactor / np->spread.Y) + 3;
352 nlz = is3d ? (int)(sz * ofactor / np->spread.Z) + 3 : 1;
356 noisebuf = new float[nlx * nly * nlz];
361 * NB: This algorithm is not optimal in terms of space complexity. The entire
362 * integer lattice of noise points could be done as 2 lines instead, and for 3D,
363 * 2 lines + 2 planes.
364 * However, this would require the noise calls to be interposed with the
365 * interpolation loops, which may trash the icache, leading to lower overall
367 * Another optimization that could save half as many noise calls is to carry over
368 * values from the previous noise lattice as midpoints in the new lattice for the
371 #define idx(x, y) ((y) * nlx + (x))
372 void Noise::gradientMap2D(float x, float y, float step_x, float step_y, int seed) {
373 float v00, v01, v10, v11, u, v, orig_u;
374 int index, i, j, x0, y0, noisex, noisey;
383 //calculate noise point lattice
384 nlx = (int)(u + sx * step_x) + 2;
385 nly = (int)(v + sy * step_y) + 2;
387 for (j = 0; j != nly; j++)
388 for (i = 0; i != nlx; i++)
389 noisebuf[index++] = noise2d(x0 + i, y0 + j, seed);
391 //calculate interpolations
394 for (j = 0; j != sy; j++) {
395 v00 = noisebuf[idx(0, noisey)];
396 v10 = noisebuf[idx(1, noisey)];
397 v01 = noisebuf[idx(0, noisey + 1)];
398 v11 = noisebuf[idx(1, noisey + 1)];
402 for (i = 0; i != sx; i++) {
403 buf[index++] = biLinearInterpolation(v00, v10, v01, v11, u, v);
410 v10 = noisebuf[idx(noisex + 1, noisey)];
411 v11 = noisebuf[idx(noisex + 1, noisey + 1)];
425 #define idx(x, y, z) ((z) * nly * nlx + (y) * nlx + (x))
426 void Noise::gradientMap3D(float x, float y, float z,
427 float step_x, float step_y, float step_z,
429 float v000, v010, v100, v110;
430 float v001, v011, v101, v111;
431 float u, v, w, orig_u, orig_v;
432 int index, i, j, k, x0, y0, z0, noisex, noisey, noisez;
444 //calculate noise point lattice
445 nlx = (int)(u + sx * step_x) + 2;
446 nly = (int)(v + sy * step_y) + 2;
447 nlz = (int)(w + sz * step_z) + 2;
449 for (k = 0; k != nlz; k++)
450 for (j = 0; j != nly; j++)
451 for (i = 0; i != nlx; i++)
452 noisebuf[index++] = noise3d(x0 + i, y0 + j, z0 + k, seed);
454 //calculate interpolations
458 for (k = 0; k != sz; k++) {
461 for (j = 0; j != sy; j++) {
462 v000 = noisebuf[idx(0, noisey, noisez)];
463 v100 = noisebuf[idx(1, noisey, noisez)];
464 v010 = noisebuf[idx(0, noisey + 1, noisez)];
465 v110 = noisebuf[idx(1, noisey + 1, noisez)];
466 v001 = noisebuf[idx(0, noisey, noisez + 1)];
467 v101 = noisebuf[idx(1, noisey, noisez + 1)];
468 v011 = noisebuf[idx(0, noisey + 1, noisez + 1)];
469 v111 = noisebuf[idx(1, noisey + 1, noisez + 1)];
473 for (i = 0; i != sx; i++) {
474 buf[index++] = triLinearInterpolation(
475 v000, v100, v010, v110,
476 v001, v101, v011, v111,
484 v100 = noisebuf[idx(noisex + 1, noisey, noisez)];
485 v110 = noisebuf[idx(noisex + 1, noisey + 1, noisez)];
488 v101 = noisebuf[idx(noisex + 1, noisey, noisez + 1)];
489 v111 = noisebuf[idx(noisex + 1, noisey + 1, noisez + 1)];
510 float *Noise::perlinMap2D(float x, float y) {
511 float f = 1.0, g = 1.0;
512 int i, j, index, oct;
517 memset(result, 0, sizeof(float) * sx * sy);
519 for (oct = 0; oct < np->octaves; oct++) {
520 gradientMap2D(x * f, y * f,
521 f / np->spread.X, f / np->spread.Y,
522 seed + np->seed + oct);
525 for (j = 0; j != sy; j++) {
526 for (i = 0; i != sx; i++) {
527 result[index] += g * buf[index];
540 float *Noise::perlinMap2DModulated(float x, float y, float *persist_map) {
542 int i, j, index, oct;
547 memset(result, 0, sizeof(float) * sx * sy);
549 float *g = new float[sx * sy];
550 for (index = 0; index != sx * sy; index++)
553 for (oct = 0; oct < np->octaves; oct++) {
554 gradientMap2D(x * f, y * f,
555 f / np->spread.X, f / np->spread.Y,
556 seed + np->seed + oct);
559 for (j = 0; j != sy; j++) {
560 for (i = 0; i != sx; i++) {
561 result[index] += g[index] * buf[index];
562 g[index] *= persist_map[index];
575 float *Noise::perlinMap3D(float x, float y, float z) {
576 float f = 1.0, g = 1.0;
577 int i, j, k, index, oct;
583 memset(result, 0, sizeof(float) * sx * sy * sz);
585 for (oct = 0; oct < np->octaves; oct++) {
586 gradientMap3D(x * f, y * f, z * f,
587 f / np->spread.X, f / np->spread.Y, f / np->spread.Z,
588 seed + np->seed + oct);
591 for (k = 0; k != sz; k++) {
592 for (j = 0; j != sy; j++) {
593 for (i = 0; i != sx; i++) {
594 result[index] += g * buf[index];
608 void Noise::transformNoiseMap() {
610 for (int z = 0; z != sz; z++) {
611 for (int y = 0; y != sy; y++) {
612 for (int x = 0; x != sx; x++) {
613 result[i] = result[i] * np->scale + np->offset;