--- deliantra/server/common/noise.C 2011/04/26 03:18:06 1.8
+++ deliantra/server/common/noise.C 2016/11/16 23:41:59 1.21
@@ -1,22 +1,22 @@
/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
- *
- * Copyright (©) 2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
- *
+ *
+ * Copyright (©) 2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
+ *
* Deliantra is free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the Affero GNU General Public License
* and the GNU General Public License along with this program. If not, see
* .
- *
+ *
* The authors can be reached via e-mail to
*/
@@ -228,9 +228,8 @@
/////////////////////////////////////////////////////////////////////////////
template
-template
void
-permutation::seed (random_generator &rng)
+permutation::seed (seedable_rand_gen &rng)
{
for (int i = 0; i < N; ++i)
pmap[i] = i;
@@ -244,146 +243,175 @@
/////////////////////////////////////////////////////////////////////////////
-static vec2d
-floor (vec2d v)
-{
- return vec2d (std::floor (v[0]), std::floor (v[1]));
-}
-
-static vec3d
-floor (vec3d v)
+template
+void
+noise_gen_base::seed (seedable_rand_gen &rng)
{
- return vec3d (std::floor (v[0]), std::floor (v[1]), std::floor (v[2]));
+ for (int i = 0; i < array_length (rvmap); ++i)
+ rvmap[i].seed (rng);
}
-noise_gen::noise_gen (uint32_t seed)
+template
+void
+noise_gen_base::seed (seed_t seed)
{
seedable_rand_gen rng (seed);
- rvmap[0].seed (rng);
- rvmap[1].seed (rng);
+ this->seed (rng);
}
+template<>
vec2d::T_numtype
-noise_gen::operator() (vec2d P)
+noise_gen_base::operator() (vec2d P, uint32_t seed)
{
vec2d I = floor (P);
vec2d F = P - I;
- float v = 0;
+ value_t v = 0;
+
+ seed *= 3039177861;
+ uint8_t i1 = uint8_t (I[1]) + (seed >> 16);
+ uint8_t i0 = uint8_t (I[0]) + (seed >> 8);
+
+ uint8_t h2 = rvmap[2](seed);
for (int j = -1; j <= 2; ++j)
- for (int i = -1; i <= 2; ++i)
- {
- vec2d A = F - vec2d (i, j);
-
- float d = dot (A, A);
-
- if (d < 4)
- {
- float t1 = 1 - d / 4;
- float t2 = t1 * t1;
- float t4 = t2 * t2;
-
- float p = (4 * t1 - 3) * t4;
-
- int h = rvmap[0](I[0] + i) ^ rvmap[1](I[1] + j);
- vec2d G = charges2[h & 0xff];
-
- v += dot (A, G) * p;
- }
- }
+ {
+ uint8_t h1 = rvmap[1](i1 + j) ^ h2;
+
+ for (int i = -1; i <= 2; ++i)
+ {
+ vec2d A = F - vec2d (i, j);
+ value_t d = dot (A, A);
+
+ if (d < 4)
+ {
+ value_t t1 = 1 - d / 4;
+ value_t t2 = t1 * t1;
+ value_t t4 = t2 * t2;
+
+ value_t p = (4 * t1 - 3) * t4;
+
+ uint8_t h = rvmap[0](i0 + i) ^ h1;
+ vec2d G = charges2[h & 0xff];
+
+ v += dot (A, G) * p;
+ }
+ }
+ }
- return clamp (v, -.9999999, .9999999);
+ return v;
}
/////////////////////////////////////////////////////////////////////////////
-noise_gen::noise_gen (uint32_t seed)
-{
- seedable_rand_gen rng (seed);
-
- rvmap [0].seed (rng);
- rvmap [1].seed (rng);
- rvmap [2].seed (rng);
-}
-
+template<>
vec3d::T_numtype
-noise_gen::operator() (vec3d P)
+noise_gen_base::operator() (vec3d P, uint32_t seed)
{
vec3d I = floor (P);
vec3d F = P - I;
- float v = 0;
+ seed *= 3039177861;
+ uint8_t i2 = uint8_t (I[2]) + (seed >> 24);
+ uint8_t i1 = uint8_t (I[1]) + (seed >> 16);
+ uint8_t i0 = uint8_t (I[0]) + (seed >> 8);
- for (int j = -1; j <= 2; ++j)
- for (int i = -1; i <= 2; ++i)
- for (int k = -1; k <= 2; ++k)
+ uint8_t h3 = rvmap[3](seed);
+
+ value_t v = 0;
+
+ for (int k = -1; k <= 2; ++k)
+ {
+ uint8_t h2 = rvmap[2](i2 + k) ^ h3;
+
+ for (int j = -1; j <= 2; ++j)
{
- vec3d A = F - vec3d (i, j, k);
- float d = dot (A, A);
+ uint8_t h1 = rvmap[1](i1 + j) ^ h2;
- if (d < 4)
+ for (int i = -1; i <= 2; ++i)
{
- float t1 = 1 - d / 4;
- float t2 = t1 * t1;
- float t4 = t2 * t2;
+ vec3d A = F - vec3d (i, j, k);
+ value_t d = dot (A, A);
- // (4t⁵ - 3t⁴)
- float p = (4 * t1 - 3) * t4;
+ if (d < 4)
+ {
+ value_t t1 = 1 - d / 4;
+ value_t t2 = t1 * t1;
+ value_t t4 = t2 * t2;
- int h = rvmap[0](I[0] + i) ^ rvmap[1](I[1] + j) ^ rvmap[2](I[2] + k);
- const float *G = charges3[h & 0xff];
+ // (4t⁵ - 3t⁴)
+ value_t p = (4 * t1 - 3) * t4;
- v += dot (A, vec3d (G[0], G[1], G[2])) * p;
+ uint8_t h = rvmap[0](i0 + i) ^ h1;
+ const value_t *G = charges3[h & 0xff];
+
+ v += dot (A, vec3d (G[0], G[1], G[2])) * p;
+ }
}
}
+ }
- return clamp (v * 2, -.9999999, .9999999);
+ return v * 2;
}
vec3d::T_numtype
-noise_gen::operator() (vec3d P, vec3d N)
+noise_gen::operator() (vec3d P, vec3d N, uint32_t seed)
{
vec3d I = floor (P);
vec3d F = P - I;
- float v = 0;
+ seed *= 3039177861;
+ uint8_t i2 = uint8_t (I[2]) + (seed >> 24);
+ uint8_t i1 = uint8_t (I[1]) + (seed >> 16);
+ uint8_t i0 = uint8_t (I[0]) + (seed >> 8);
- for (int j = -1; j <= 2; ++j)
- for (int i = -1; i <= 2; ++i)
- for (int k = -1; k <= 2; ++k)
+ uint8_t h3 = rvmap[3](seed);
+
+ value_t v = 0;
+
+ for (int k = -1; k <= 2; ++k)
+ {
+ uint8_t h2 = rvmap[2](i2 + k) ^ h3;
+
+ for (int j = -1; j <= 2; ++j)
{
- vec3d D = F - vec3d (i, j, k);
- float e = dot (D, N);
- float o = 1 - abs (e);
+ uint8_t h1 = rvmap[1](i1 + j) ^ h2;
- if (o > 0)
+ for (int i = -1; i <= 2; ++i)
{
- vec3d A = D - e * N;
- float d = dot (A, A);
+ vec3d D = F - vec3d (i, j, k);
+ value_t e = dot (D, N);
+ value_t o = 1 - abs (e);
- if (d < 4)
+ if (o > 0)
{
- float t1 = 1 - d / 4;
- float t2 = t1 * t1;
- float t4 = t2 * t2;
-
- float o2 = o * o;
-
- // (4t⁵ - 3t⁴) * (3o³ - 2o²)
- // alternative? ((o * 6 - 15) * o + 10) * o³
- float p = (4 * t1 - 3) * t4 * (3 * o2 - 2 * o2 * o);
+ vec3d A = D - e * N;
+ value_t d = dot (A, A);
- int h = rvmap[0](I[0] + i) ^ rvmap[1](I[1] + j) ^ rvmap[2](I[2] + k);
- const float *G = charges3[h & 0xff];
+ if (d < 4)
+ {
+ value_t t1 = 1 - d / 4;
+ value_t t2 = t1 * t1;
+ value_t t4 = t2 * t2;
+
+ value_t o2 = o * o;
+
+ // (4t⁵ - 3t⁴) * (3o³ - 2o²)
+ // alternative? ((o * 6 - 15) * o + 10) * o³
+ value_t p = (4 * t1 - 3) * t4 * (3 * o2 - 2 * o2 * o);
- v += dot (A, vec3d (G[0], G[1], G[2])) * p;
+ uint8_t h = rvmap[0](i0 + i) ^ h1;
+ const value_t *G = charges3[h & 0xff];
+
+ v += dot (A, vec3d (G[0], G[1], G[2])) * p;
+ }
}
}
}
+ }
- return clamp (v, -.9999999, .9999999);
+ return v;
}
template class noise_gen;
@@ -391,27 +419,16 @@
/////////////////////////////////////////////////////////////////////////////
-// find some uncorrelated spot - we assume that noise 2 units away
-// is completely uncorrelated - this is not true for other coordinates,
-// but generally works well.
template
-static vec_t
-ith_octave (vec_t P, int i)
+frac_gen::frac_gen (int octaves, value_t lacunarity, value_t hurst_expo, seed_t seed)
+: octaves (octaves), lac (lacunarity), h (hurst_expo)
{
- vec_t r = P;
- r[0] += i;
- r[1] += i * 2;
- return r;
-}
+ this->seed (seed);
-template
-frac_gen::frac_gen (value_t hurst_expo, value_t lacunarity)
-: h (hurst_expo), lac (lacunarity), noise_gen (0)
-{
value_t exsum = 0;
value_t phi = noise (vec_t (value_t (0))) * 0.5 + 1;
- for (int i = 0; i < MAX_OCTAVES; ++i)
+ for (int i = 0; i < octaves; ++i)
{
ex [i] = pow (lac, -h * i);
exsum += ex [i];
@@ -422,39 +439,39 @@
template
typename frac_gen::value_t
-frac_gen::fBm (vec_t P, int octaves)
+frac_gen::fBm (vec_t P)
{
value_t v = 0;
for (int i = 0; i < octaves; ++i)
{
rot [i](P);
- v += noise (ith_octave (P, i)) * ex [i];
+ v += noise (P, i) * ex [i];
P *= lac;
}
- return clamp (v * fbm_mul [octaves - 1] + 0.5, 0, .9999999);
+ return v * fbm_mul [octaves - 1];
}
template
typename frac_gen::value_t
-frac_gen::turbulence (vec_t P, int octaves)
+frac_gen::turbulence (vec_t P)
{
value_t v = 0;
for (int i = 0; i < octaves; ++i)
{
rot [i](P);
- v += abs (noise (ith_octave (P, i))) * ex [i];
+ v += abs (noise (P, i)) * ex [i];
P *= lac;
}
- return clamp (v * fbm_mul [octaves - 1] * (0.5 / noise_gen::abs_avg ()), 0, .9999999);
+ return v * fbm_mul [octaves - 1] * (0.5 / noise_gen::abs_avg ());
}
template
typename frac_gen::value_t
-frac_gen::multifractal (vec_t P, int octaves, value_t offset)
+frac_gen::multifractal (vec_t P, value_t offset)
{
value_t v = 1;
@@ -465,12 +482,12 @@
P *= lac;
}
- return clamp (v * 0.5, 0, .9999999);
+ return v * value_t (0.5);
}
template
typename frac_gen::value_t
-frac_gen::heterofractal (vec_t P, int octaves, value_t offset)
+frac_gen::heterofractal (vec_t P, value_t offset)
{
value_t v = noise (P) + offset;
@@ -486,7 +503,7 @@
template
typename frac_gen::value_t
-frac_gen::hybridfractal (vec_t P, int octaves, value_t offset, value_t gain)
+frac_gen::hybridfractal (vec_t P, value_t offset, value_t gain)
{
value_t v = (noise (P) + offset) * ex [0];
value_t weight = v;
@@ -503,12 +520,12 @@
weight *= gain * sig;
}
- return clamp (v * 0.5 + 0.5, 0, .9999999);
+ return v * value_t (0.5) + value_t (0.5);
}
template
typename frac_gen::value_t
-frac_gen::ridgedmultifractal (vec_t P, int octaves, value_t offset, value_t gain)
+frac_gen::ridgedmultifractal (vec_t P, value_t offset, value_t gain)
{
value_t sig = offset - abs (noise (P));
sig *= sig;
@@ -519,6 +536,7 @@
rot [i](P);
P *= lac;
+ // weight higher octaves by previous signal
value_t w = clamp (sig * gain, 0, 1);
sig = offset - abs (noise (P));
@@ -528,12 +546,12 @@
v += sig * ex [i];
}
- return clamp (v * 0.25, 0, .9999999);
+ return v * value_t (0.25);
}
template
typename frac_gen::value_t
-frac_gen::billowfractal (vec_t P, int octaves, value_t offset, value_t gain)
+frac_gen::billowfractal (vec_t P, value_t offset, value_t gain)
{
value_t v = 0;
@@ -542,17 +560,17 @@
for (int i = 0; i < octaves; ++i)
{
rot [i](P);
- v += (abs (noise (ith_octave (P, i))) * gain - offset) * ex [i];
+ v += (abs (noise (P, i)) * gain - offset) * ex [i];
P *= lac;
}
- return clamp (v, -.9999999, .9999999);
+ return v;
}
// http://www.gamasutra.com/view/feature/3098/a_realtime_procedural_universe_.php?page=2
template
typename frac_gen::value_t
-frac_gen::terrain (vec_t P, int octaves)
+frac_gen::terrain (vec_t P)
{
value_t v = 0;
@@ -570,11 +588,11 @@
template
typename frac_gen::value_t
-frac_gen::terrain2 (vec_t P, int octaves)
+frac_gen::terrain2 (vec_t P)
{
- value_t a = fBm (P, octaves);
- value_t b = ridgedmultifractal (P, octaves, 1, 8);
- value_t fade = fBm (P + vec_t(10.3), octaves);
+ value_t a = fBm (P);
+ value_t b = ridgedmultifractal (P, 1, 8);
+ value_t fade = fBm (P + vec_t(10.3));
const value_t width = 0.05;
@@ -594,81 +612,3 @@
template class frac_gen;
template class frac_gen;
-/////////////////////////////////////////////////////////////////////////////
-
-void noise_test ();
-void noise_test ()
-{
- frac_gen gen (0.5, 1.9);
- frac_gen gen3;
-
-#if 1
- int N = 1024;
-
- printf ("P5 %d %d 255\n", N, N);
- // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm
- for (int y = 0; y < N; ++y)
- {
- if (!(y&63))fprintf (stderr, "y %d\n", y);//D
- for (int x = 0; x < N; ++x)
- {
- //putc (127 * gen.noise (vec2d (x * 0.01, y * 0.01)) + 128, stdout);
- //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout);
- //putc (256 * gen.fBm (vec2d(x * 0.01, y * 0.01), 16), stdout);
- putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout);
- //putc (256 * gen.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout);
-
- // mountais or somesuch(?)
- //putc (256 * gen.hybridfractal (vec2d (x * 0.01, y * 0.01), 8, -.4, -4), stdout);
-
- // temperature
- //putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout);
- // rain
-
- //cells
- //putc (127.49 * gen.billowfractal (vec2d (x * 0.01, y * 0.01), 9) + 128, stdout);
- }
- }
-#else
- int N = 512;
-
- //printf ("P6 %d %d 255\n", N, N);
- // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm
- for (int z = 0; z < N; ++z)
- {
- if (!(z&7))fprintf (stderr, "z %d\n", z);//D
- for (int y = 0; y < N; ++y)
- for (int x = 0; x < N; ++x)
- {
- float v = gen3.ridgedmultifractal (vec3d (x * 0.01 + 0.2, y * 0.01 + 0.2, z * 0.01 + 0.2), 3, 1.03, 2) * 2;
-
-#if 0
- if (z < 64)
- v = v * (z * z) / (64 * 64);
-#endif
-
- if (v <= 0.9999)
- continue;
-
- float r[4];
- int i[4];
-
- r[0] = x;
- r[1] = y;
- r[2] = z;
- r[3] = v;
-
- memcpy (i, r, 16);
-
- i[0] = htonl (i[0]);
- i[1] = htonl (i[1]);
- i[2] = htonl (i[2]);
- i[3] = htonl (i[3]);
-
- fwrite (i, 4*4, 1, stdout);
- }
- }
-#endif
-
- exit (0);
-}