Skip to content

Commit

Permalink
Clean up Noise macros
Browse files Browse the repository at this point in the history
  • Loading branch information
kwolekr committed Dec 11, 2014
1 parent c07f15e commit fb80a7c
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/mapgen_v5.cpp
Expand Up @@ -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;
Expand Down
42 changes: 11 additions & 31 deletions src/mapgen_v6.cpp
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion src/mapgen_v7.cpp
Expand Up @@ -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)
Expand Down
55 changes: 54 additions & 1 deletion src/noise.cpp
Expand Up @@ -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)
Expand Down
74 changes: 41 additions & 33 deletions src/noise.h
Expand Up @@ -151,13 +151,54 @@ class Noise {
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);
void updateResults(float g, float *gmap, float *persistence_map, size_t bufsize);

};

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);
Expand All @@ -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

0 comments on commit fb80a7c

Please sign in to comment.