aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkwolekr <kwolekr@minetest.net>2014-12-11 02:53:10 -0500
committerkwolekr <kwolekr@minetest.net>2014-12-11 02:53:10 -0500
commitfb80a7c111c138d335f4c7e68fc098f8aa8483d3 (patch)
tree16006b2845b33e84293209047f375de927cac90f
parentc07f15e910ccce52b62b45af1b1a9210c2130817 (diff)
downloadminetest-fb80a7c111c138d335f4c7e68fc098f8aa8483d3.tar.gz
minetest-fb80a7c111c138d335f4c7e68fc098f8aa8483d3.tar.bz2
minetest-fb80a7c111c138d335f4c7e68fc098f8aa8483d3.zip
Clean up Noise macros
-rw-r--r--src/mapgen_v5.cpp2
-rw-r--r--src/mapgen_v6.cpp42
-rw-r--r--src/mapgen_v7.cpp2
-rw-r--r--src/noise.cpp55
-rw-r--r--src/noise.h74
5 files changed, 108 insertions, 67 deletions
diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp
index 0ec19ebda..b741ebfba 100644
--- a/src/mapgen_v5.cpp
+++ b/src/mapgen_v5.cpp
@@ -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;
diff --git a/src/mapgen_v6.cpp b/src/mapgen_v6.cpp
index 004aef8c7..148b4497c 100644
--- a/src/mapgen_v6.cpp
+++ b/src/mapgen_v6.cpp
@@ -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);
}
diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp
index 097e70934..efaf60a6f 100644
--- a/src/mapgen_v7.cpp
+++ b/src/mapgen_v7.cpp
@@ -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)
diff --git a/src/noise.cpp b/src/noise.cpp
index 069c60d44..93f9651ed 100644
--- a/src/noise.cpp
+++ b/src/noise.cpp
@@ -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)
diff --git a/src/noise.h b/src/noise.h
index 2cdf4203d..f2df0ed5a 100644
--- a/src/noise.h
+++ b/src/noise.h
@@ -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