3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
24 #include "util/numeric.h"
26 #define NOISE_MAGIC_X 1619
27 #define NOISE_MAGIC_Y 31337
28 #define NOISE_MAGIC_Z 52591
29 #define NOISE_MAGIC_SEED 1013
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
37 ///////////////////////////////////////////////////////////////////////////////
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;
45 n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
46 return 1.f - (float)n / 0x40000000;
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;
54 n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
55 return 1.f - (float)n / 0x40000000;
59 float dotProduct(float vx, float vy, float wx, float wy) {
60 return vx * wx + vy * wy;
64 inline float linearInterpolation(float v0, float v1, float t) {
65 return v0 + (v1 - v0) * t;
69 float biLinearInterpolation(float v00, float v10,
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);
80 float biLinearInterpolationNoEase(float x0y0, float x1y0,
81 float x0y1, float x1y1,
83 float u = linearInterpolation(x0y0, x1y0, x);
84 float v = linearInterpolation(x0y1, x1y1, x);
85 return linearInterpolation(u, v, y);
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);
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)
105 /*float tx = easeCurve(x);
106 float ty = easeCurve(y);
107 float tz = easeCurve(z);*/
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 +
126 float noise2d_gradient(float x, float y, int seed)
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);
151 float noise2d_gradient(float x, float y, int seed)
153 // Calculate the integer coordinates
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);
165 return biLinearInterpolation(v00,v10,v01,v11,xl,yl);
169 float noise3d_gradient(float x, float y, float z, int seed)
171 // Calculate the integer coordinates
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);
189 return triLinearInterpolation(v000, v100, v010, v110,
190 v001, v101, v011, v111,
195 float noise2d_perlin(float x, float y, int seed,
196 int octaves, float persistence)
201 for (int i = 0; i < octaves; i++)
203 a += g * noise2d_gradient(x * f, y * f, seed + i);
211 float noise2d_perlin_abs(float x, float y, int seed,
212 int octaves, float persistence)
217 for (int i = 0; i < octaves; i++)
219 a += g * fabs(noise2d_gradient(x * f, y * f, seed + i));
227 float noise3d_perlin(float x, float y, float z, int seed,
228 int octaves, float persistence)
233 for (int i = 0; i < octaves; i++)
235 a += g * noise3d_gradient(x * f, y * f, z * f, seed + i);
243 float noise3d_perlin_abs(float x, float y, float z, int seed,
244 int octaves, float persistence)
249 for (int i = 0; i < octaves; i++)
251 a += g * fabs(noise3d_gradient(x * f, y * f, z * f, seed + i));
260 float contour(float v)
269 ///////////////////////// [ New perlin stuff ] ////////////////////////////
272 Noise::Noise(NoiseParams *np, int seed, int sx, int sy) {
276 //maximum possible spread value factor
277 ofactor = (float)(1 << (np->octaves - 1));
279 //noise lattice point count
280 //(int)(sz * spread * ofactor) is # of lattice points crossed due to length
281 // + 2 for the two initial endpoints
282 // + 1 for potentially crossing a boundary due to offset
283 nlx = (int)(sx * ofactor / np->spread.X) + 3;
284 nly = (int)(sy * ofactor / np->spread.Y) + 3;
291 this->noisebuf = new float[nlx * nly];
292 this->buf = new float[sx * sy];
293 this->result = new float[sx * sy];
297 Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz) {
301 ofactor = (float)(1 << (np->octaves - 1));
302 nlx = (int)(sx * ofactor / np->spread.X) + 3;
303 nly = (int)(sy * ofactor / np->spread.Y) + 3;
304 nlz = (int)(sz * ofactor / np->spread.Z) + 3;
311 this->noisebuf = new float[nlx * nly * nlz];
312 this->buf = new float[sx * sy * sz];
313 this->result = new float[sx * sy * sz];
325 * NB: This algorithm is not optimal in terms of space complexity. The entire
326 * integer lattice of noise points could be done as 2 lines instead, and for 3D,
327 * 2 lines + 2 planes.
328 * However, this would require the noise calls to be interposed with the
329 * interpolation loops, which may trash the icache, leading to lower overall
331 * Another optimization that could save half as many noise calls is to carry over
332 * values from the previous noise lattice as midpoints in the new lattice for the
335 void Noise::gradientMap2D(float x, float y, float step_x, float step_y, int seed) {
336 float v00, v01, v10, v11, u, v, orig_u;
337 int index, i, j, x0, y0, noisex, noisey;
346 //calculate noise point lattice
348 nlx = (int)(u + sx * step_x) + 2;
349 nly = (int)(v + sy * step_y) + 2;
351 for (j = 0; j != nly; j++)
352 for (i = 0; i != nlx; i++)
353 noisebuf[index++] = noise2d(x0 + i, y0 + j, seed);
355 //calculate interpolations
357 for (j = 0; j != sy; j++) {
358 v00 = noisebuf[noisey * nlx];
359 v10 = noisebuf[noisey * nlx + 1];
360 v01 = noisebuf[(noisey + 1) * nlx];
361 v11 = noisebuf[(noisey + 1) * nlx + 1];
365 for (i = 0; i != sx; i++) {
366 buf[j * sx + i] = biLinearInterpolation(v00, v10, v01, v11, u, v);
373 v10 = noisebuf[noisey * nlx + noisex + 1];
374 v11 = noisebuf[(noisey + 1) * nlx + noisex + 1];
387 void Noise::gradientMap3D(float x, float y, float z,
388 float step_x, float step_y, float step_z,
390 float v000, v010, v100, v110;
391 float v001, v011, v101, v111;
392 float u, v, w, orig_u, orig_w;
393 int index, i, j, k, x0, y0, z0, noisex, noisey, noisez;
405 //calculate noise point lattice
406 nlx = (int)(u + sx * step_x) + 2;
407 nly = (int)(v + sy * step_y) + 2;
408 nlz = (int)(v + sy * step_z) + 2;
410 for (k = 0; k != nlz; k++)
411 for (j = 0; j != nly; j++)
412 for (i = 0; i != nlx; i++)
413 noisebuf[index++] = noise3d(x0 + i, y0 + j, z0 + k, seed);
415 #define index(x, y, z) ((z) * nly * nlx + (y) * nlx + (x))
417 //calculate interpolations
420 for (k = 0; k != sz; k++) {
421 v000 = noisebuf[index(0, noisey, noisez)];
422 v100 = noisebuf[index(1, noisey, noisez)];
423 v010 = noisebuf[index(0, noisey + 1, noisez)];
424 v110 = noisebuf[index(1, noisey + 1, noisez)];
425 v001 = noisebuf[index(0, noisey, noisez + 1)];
426 v101 = noisebuf[index(1, noisey, noisez + 1)];
427 v011 = noisebuf[index(0, noisey + 1, noisez + 1)];
428 v111 = noisebuf[index(1, noisey + 1, noisez + 1)];
432 for (j = 0; j != sy; j++) {
433 v000 = noisebuf[index(0, noisey, noisez)];
434 v100 = noisebuf[index(1, noisey, noisez)];
435 v010 = noisebuf[index(0, noisey + 1, noisez)];
436 v110 = noisebuf[index(1, noisey + 1, noisez)];
437 v001 = noisebuf[index(0, noisey, noisez + 1)];
438 v101 = noisebuf[index(1, noisey, noisez + 1)];
439 v011 = noisebuf[index(0, noisey + 1, noisez + 1)];
440 v111 = noisebuf[index(1, noisey + 1, noisez + 1)];
444 for (i = 0; i != sx; i++) {
445 buf[j * sx + i] = triLinearInterpolation(
446 v000, v100, v010, v110,
447 v001, v101, v011, v111,
455 v100 = noisebuf[index(noisex + 1, noisey, noisez)];
456 v110 = noisebuf[index(noisex + 1, noisey + 1, noisez)];
459 v101 = noisebuf[index(noisex + 1, noisey, noisez + 1)];
460 v111 = noisebuf[index(noisex + 1, noisey + 1, noisez + 1)];
480 float *Noise::perlinMap2D(float x, float y) {
481 float a = 0.0, f = 1.0, g = 1.0;
482 int i, j, index, oct;
487 memset(result, 0, sizeof(float) * sx * sy);
489 for (oct = 0; oct < np->octaves; oct++) {
490 gradientMap2D(x * f, y * f,
491 f / np->spread.X, f / np->spread.Y,
492 seed + np->seed + oct);
495 for (j = 0; j != sy; j++) {
496 for (i = 0; i != sx; i++) {
497 result[index] += g * buf[index];
510 float *Noise::perlinMap3D(float x, float y, float z) {
511 float a = 0.0, f = 1.0, g = 1.0;
512 int i, j, k, index, oct;
518 memset(result, 0, sizeof(float) * sx * sy * sz);
520 for (oct = 0; oct < np->octaves; oct++) {
521 gradientMap3D(x * f, y * f, z * f,
522 f / np->spread.X, f / np->spread.Y, f / np->spread.Z,
523 seed + np->seed + oct);
526 for (k = 0; k != sz; k++) {
527 for (j = 0; j != sy; j++) {
528 for (i = 0; i != sx; i++) {
529 result[index] += g * buf[index];