Clean up Noise macros
authorkwolekr <kwolekr@minetest.net>
Thu, 11 Dec 2014 07:53:10 +0000 (02:53 -0500)
committerkwolekr <kwolekr@minetest.net>
Thu, 11 Dec 2014 07:53:10 +0000 (02:53 -0500)
src/mapgen_v5.cpp
src/mapgen_v6.cpp
src/mapgen_v7.cpp
src/noise.cpp
src/noise.h

index 0ec19ebda42cf621b6f2b356de7f80a03e09f6ef..b741ebfba75ead0f4bc39dd32d6167f68ab4d59e 100644 (file)
@@ -202,7 +202,7 @@ int MapgenV5::getGroundLevelAtPoint(v2s16 p) {
 
        s16 level = -31000;
        for (s16 y = search_top; y >= search_base; y--) {
-               float n_ground = NoisePerlin3DEased(&noise_ground->np, p.X, y, p.Y, seed);
+               float n_ground = NoisePerlin3D(&noise_ground->np, p.X, y, p.Y, seed);
                if(n_ground * f > y - h) {
                        if(y >= search_top - 7)
                                break;
index 004aef8c7f4d48a1052fd18af8a41eb5435d62e7..148b4497cde0a4c55b99f852a0825c49a5dbaeea 100644 (file)
@@ -246,13 +246,13 @@ float MapgenV6::baseTerrainLevelFromNoise(v2s16 p) {
        if (flags & MG_FLAT)
                return water_level;
 
-       float terrain_base   = NoisePerlin2DPosOffset(&noise_terrain_base->np,
+       float terrain_base   = NoisePerlin2D_PO(&noise_terrain_base->np,
                                                        p.X, 0.5, p.Y, 0.5, seed);
-       float terrain_higher = NoisePerlin2DPosOffset(&noise_terrain_higher->np,
+       float terrain_higher = NoisePerlin2D_PO(&noise_terrain_higher->np,
                                                        p.X, 0.5, p.Y, 0.5, seed);
-       float steepness      = NoisePerlin2DPosOffset(&noise_steepness->np,
+       float steepness      = NoisePerlin2D_PO(&noise_steepness->np,
                                                        p.X, 0.5, p.Y, 0.5, seed);
-       float height_select  = NoisePerlin2DNoTxfmPosOffset(&noise_height_select->np,
+       float height_select  = NoisePerlin2D_PO(&noise_height_select->np,
                                                        p.X, 0.5, p.Y, 0.5, seed);
 
        return baseTerrainLevel(terrain_base, terrain_higher,
@@ -547,36 +547,16 @@ void MapgenV6::calculateNoise() {
        int x = node_min.X;
        int z = node_min.Z;
 
-       // Need to adjust for the original implementation's +.5 offset...
        if (!(flags & MG_FLAT)) {
-               noise_terrain_base->perlinMap2D(
-                       x + 0.5 * noise_terrain_base->np.spread.X,
-                       z + 0.5 * noise_terrain_base->np.spread.Z);
-
-               noise_terrain_higher->perlinMap2D(
-                       x + 0.5 * noise_terrain_higher->np.spread.X,
-                       z + 0.5 * noise_terrain_higher->np.spread.Z);
-
-               noise_steepness->perlinMap2D(
-                       x + 0.5 * noise_steepness->np.spread.X,
-                       z + 0.5 * noise_steepness->np.spread.Z);
-
-               noise_height_select->perlinMap2D(
-                       x + 0.5 * noise_height_select->np.spread.X,
-                       z + 0.5 * noise_height_select->np.spread.Z);
-
-               noise_mud->perlinMap2D(
-                       x + 0.5 * noise_mud->np.spread.X,
-                       z + 0.5 * noise_mud->np.spread.Z);
+               noise_terrain_base->perlinMap2D_PO(x, 0.5, z, 0.5);
+               noise_terrain_higher->perlinMap2D_PO(x, 0.5, z, 0.5);
+               noise_steepness->perlinMap2D_PO(x, 0.5, z, 0.5);
+               noise_height_select->perlinMap2D_PO(x, 0.5, z, 0.5);
+               noise_mud->perlinMap2D_PO(x, 0.5, z, 0.5);
        }
 
-       noise_beach->perlinMap2D(
-               x + 0.2 * noise_beach->np.spread.X,
-               z + 0.7 * noise_beach->np.spread.Z);
-
-       noise_biome->perlinMap2D(
-               x + 0.6 * noise_biome->np.spread.X,
-               z + 0.2 * noise_biome->np.spread.Z);
+       noise_beach->perlinMap2D_PO(x, 0.2, z, 0.7);
+       noise_biome->perlinMap2D_PO(x, 0.6, z, 0.2);
 }
 
 
index 097e709341ef54123a6beb37c79a8bd435797bd5..efaf60a6fa626fc083ffe30ec3c36120509efa80 100644 (file)
@@ -173,7 +173,7 @@ int MapgenV7::getGroundLevelAtPoint(v2s16 p) {
 
        // Ridge/river terrain calculation
        float width = 0.3;
-       float uwatern = NoisePerlin2DNoTxfm(&noise_ridge_uwater->np, p.X, p.Y, seed) * 2;
+       float uwatern = NoisePerlin2D(&noise_ridge_uwater->np, p.X, p.Y, seed) * 2;
        // actually computing the depth of the ridge is much more expensive;
        // if inside a river, simply guess
        if (uwatern >= -width && uwatern <= width)
index 069c60d44ef9695ce69df7432fa6c91d455fc560..93f9651ed8ce4152b0f034a1e2a2974dbe5a56d7 100644 (file)
@@ -314,7 +314,60 @@ float contour(float v)
 }
 
 
-///////////////////////// [ New perlin stuff ] ////////////////////////////
+///////////////////////// [ New noise ] ////////////////////////////
+
+
+float NoisePerlin2D(NoiseParams *np, float x, float y, int seed)
+{
+       float a = 0;
+       float f = 1.0;
+       float g = 1.0;
+
+       x /= np->spread.X;
+       y /= np->spread.Y;
+       seed += np->seed;
+
+       for (size_t i = 0; i < np->octaves; i++) {
+               float noiseval = noise2d_gradient(x * f, y * f, seed + i,
+                       np->flags & (NOISE_FLAG_DEFAULTS | NOISE_FLAG_EASED));
+
+               if (np->flags & NOISE_FLAG_ABSVALUE)
+                       noiseval = fabs(noiseval);
+
+               a += g * noiseval;
+               f *= np->lacunarity;
+               g *= np->persist;
+       }
+
+       return np->offset + a * np->scale;
+}
+
+
+float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed)
+{
+       float a = 0;
+       float f = 1.0;
+       float g = 1.0;
+
+       x /= np->spread.X;
+       y /= np->spread.Y;
+       z /= np->spread.Z;
+       seed += np->seed;
+
+       for (size_t i = 0; i < np->octaves; i++) {
+               float noiseval = noise3d_gradient(x * f, y * f, z * f, seed + i,
+                       np->flags & NOISE_FLAG_EASED);
+
+               if (np->flags & NOISE_FLAG_ABSVALUE)
+                       noiseval = fabs(noiseval);
+
+               a += g * noiseval;
+               f *= np->lacunarity;
+               g *= np->persist;
+       }
+
+       return np->offset + a * np->scale;
+}
 
 
 Noise::Noise(NoiseParams *np_, int seed, int sx, int sy, int sz)
index 2cdf4203d2fcae4bf84a70e75742e40b2a7f76d3..f2df0ed5a59e3b0b5219dac007f2b9b3ccd42704 100644 (file)
@@ -151,6 +151,25 @@ public:
        float *perlinMap2D(float x, float y, float *persistence_map=NULL);
        float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
 
+       inline float *perlinMap2D_PO(float x, float xoff, float y, float yoff,
+               float *persistence_map=NULL)
+       {
+               return perlinMap2D(
+                       x + xoff * np.spread.X,
+                       y + yoff * np.spread.Y,
+                       persistence_map);
+       }
+
+       inline float *perlinMap3D_PO(float x, float xoff, float y, float yoff,
+               float z, float zoff, float *persistence_map=NULL)
+       {
+               return perlinMap3D(
+                       x + xoff * np.spread.X,
+                       y + yoff * np.spread.Y,
+                       z + zoff * np.spread.Z,
+                       persistence_map);
+       }
+
 private:
        void allocBuffers();
        void resizeNoiseBuf(bool is3d);
@@ -158,6 +177,28 @@ private:
 
 };
 
+float NoisePerlin2D(NoiseParams *np, float x, float y, int seed);
+float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed);
+
+inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
+       float y, float yoff, int seed)
+{
+       return NoisePerlin2D(np,
+               x + xoff * np->spread.X,
+               y + yoff * np->spread.Y,
+               seed);
+}
+
+inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
+       float y, float yoff, float z, float zoff, int seed)
+{
+       return NoisePerlin3D(np,
+               x + xoff * np->spread.X,
+               y + yoff * np->spread.Y,
+               z + zoff * np->spread.Z,
+               seed);
+}
+
 // Return value: -1 ... 1
 float noise2d(int x, int y, int seed);
 float noise3d(int x, int y, int z, int seed);
@@ -184,38 +225,5 @@ inline float easeCurve(float t)
 
 float contour(float v);
 
-#define NoisePerlin2D(np, x, y, s) \
-               ((np)->offset + (np)->scale * noise2d_perlin( \
-               (float)(x) / (np)->spread.X, \
-               (float)(y) / (np)->spread.Y, \
-               (s) + (np)->seed, (np)->octaves, (np)->persist))
-
-#define NoisePerlin2DNoTxfm(np, x, y, s) \
-               (noise2d_perlin( \
-               (float)(x) / (np)->spread.X, \
-               (float)(y) / (np)->spread.Y, \
-               (s) + (np)->seed, (np)->octaves, (np)->persist))
-
-#define NoisePerlin2DPosOffset(np, x, xoff, y, yoff, s) \
-               ((np)->offset + (np)->scale * noise2d_perlin( \
-               (float)(xoff) + (float)(x) / (np)->spread.X, \
-               (float)(yoff) + (float)(y) / (np)->spread.Y, \
-               (s) + (np)->seed, (np)->octaves, (np)->persist))
-
-#define NoisePerlin2DNoTxfmPosOffset(np, x, xoff, y, yoff, s) \
-               (noise2d_perlin( \
-               (float)(xoff) + (float)(x) / (np)->spread.X, \
-               (float)(yoff) + (float)(y) / (np)->spread.Y, \
-               (s) + (np)->seed, (np)->octaves, (np)->persist))
-
-#define NoisePerlin3D(np, x, y, z, s) ((np)->offset + (np)->scale * \
-               noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
-               (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, (np)->persist))
-
-#define NoisePerlin3DEased(np, x, y, z, s) ((np)->offset + (np)->scale * \
-               noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
-               (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, \
-               (np)->persist, true))
-
 #endif