Skip to content

Commit

Permalink
Noise: Automatically transform noise maps if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
kwolekr committed Dec 11, 2014
1 parent dcc4897 commit 16baed0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 46 deletions.
4 changes: 0 additions & 4 deletions src/mapgen_v5.cpp
Expand Up @@ -299,14 +299,10 @@ void MapgenV5::calculateNoise() {
noise_filler_depth->perlinMap2D(x, z);
noise_factor->perlinMap2D(x, z);
noise_height->perlinMap2D(x, z);
noise_height->transformNoiseMap();

noise_cave1->perlinMap3D(x, y, z);
noise_cave1->transformNoiseMap();
noise_cave2->perlinMap3D(x, y, z);
noise_cave2->transformNoiseMap();
noise_ground->perlinMap3D(x, y, z);
noise_ground->transformNoiseMap();

if (spflags & MGV5_BLOBS) {
noise_crumble->perlinMap3D(x, y, z);
Expand Down
6 changes: 1 addition & 5 deletions src/mapgen_v6.cpp
Expand Up @@ -125,7 +125,7 @@ MapgenV6Params::MapgenV6Params() {
np_terrain_base = NoiseParams(-4, 20.0, v3f(250.0, 250.0, 250.0), 82341, 5, 0.6, 2.0);
np_terrain_higher = NoiseParams(20, 16.0, v3f(500.0, 500.0, 500.0), 85039, 5, 0.6, 2.0);
np_steepness = NoiseParams(0.85,0.5, v3f(125.0, 125.0, 125.0), -932, 5, 0.7, 2.0);
np_height_select = NoiseParams(0.5, 1.0, v3f(250.0, 250.0, 250.0), 4213, 5, 0.69, 2.0);
np_height_select = NoiseParams(0, 1.0, v3f(250.0, 250.0, 250.0), 4213, 5, 0.69, 2.0);
np_mud = NoiseParams(4, 2.0, v3f(200.0, 200.0, 200.0), 91013, 3, 0.55, 2.0);
np_beach = NoiseParams(0, 1.0, v3f(250.0, 250.0, 250.0), 59420, 3, 0.50, 2.0);
np_biome = NoiseParams(0, 1.0, v3f(250.0, 250.0, 250.0), 9130, 3, 0.50, 2.0);
Expand Down Expand Up @@ -552,17 +552,14 @@ void MapgenV6::calculateNoise() {
noise_terrain_base->perlinMap2D(
x + 0.5 * noise_terrain_base->np.spread.X,
z + 0.5 * noise_terrain_base->np.spread.Z);
noise_terrain_base->transformNoiseMap();

noise_terrain_higher->perlinMap2D(
x + 0.5 * noise_terrain_higher->np.spread.X,
z + 0.5 * noise_terrain_higher->np.spread.Z);
noise_terrain_higher->transformNoiseMap();

noise_steepness->perlinMap2D(
x + 0.5 * noise_steepness->np.spread.X,
z + 0.5 * noise_steepness->np.spread.Z);
noise_steepness->transformNoiseMap();

noise_height_select->perlinMap2D(
x + 0.5 * noise_height_select->np.spread.X,
Expand All @@ -571,7 +568,6 @@ void MapgenV6::calculateNoise() {
noise_mud->perlinMap2D(
x + 0.5 * noise_mud->np.spread.X,
z + 0.5 * noise_mud->np.spread.Z);
noise_mud->transformNoiseMap();
}

noise_beach->perlinMap2D(
Expand Down
8 changes: 0 additions & 8 deletions src/mapgen_v7.cpp
Expand Up @@ -270,26 +270,18 @@ void MapgenV7::calculateNoise() {
int z = node_min.Z;

noise_height_select->perlinMap2D(x, z);
noise_height_select->transformNoiseMap();

noise_terrain_persist->perlinMap2D(x, z);
noise_terrain_persist->transformNoiseMap();
float *persistmap = noise_terrain_persist->result;
for (int i = 0; i != csize.X * csize.Z; i++)
persistmap[i] = rangelim(persistmap[i], 0.4, 0.9);

noise_terrain_base->perlinMap2D(x, z, persistmap);
noise_terrain_base->transformNoiseMap();

noise_terrain_alt->perlinMap2D(x, z, persistmap);
noise_terrain_alt->transformNoiseMap();

noise_filler_depth->perlinMap2D(x, z);

if (spflags & MGV7_MOUNTAINS) {
noise_mountain->perlinMap3D(x, y, z);
noise_mount_height->perlinMap2D(x, z);
noise_mount_height->transformNoiseMap();
}

if (spflags & MGV7_RIDGES) {
Expand Down
23 changes: 10 additions & 13 deletions src/noise.cpp
Expand Up @@ -611,6 +611,11 @@ float *Noise::perlinMap2D(float x, float y, float *persistence_map)
g *= np.persist;
}

if (fabs(np.offset - 0.f) > 0.00001 || fabs(np.scale - 1.f) > 0.00001) {
for (size_t i = 0; i != bufsize; i++)
result[i] = result[i] * np.scale + np.offset;
}

return result;
}

Expand Down Expand Up @@ -644,6 +649,11 @@ float *Noise::perlinMap3D(float x, float y, float z, float *persistence_map)
g *= np.persist;
}

if (fabs(np.offset - 0.f) > 0.00001 || fabs(np.scale - 1.f) > 0.00001) {
for (size_t i = 0; i != bufsize; i++)
result[i] = result[i] * np.scale + np.offset;
}

return result;
}

Expand Down Expand Up @@ -675,16 +685,3 @@ void Noise::updateResults(float g, float *gmap,
}
}
}


void Noise::transformNoiseMap()
{
// Because sx, sy, and sz are object members whose values may conceivably be
// modified in other threads. gcc (at least) will consider the buffer size
// computation as invalidated between loop comparisons, resulting in a ~2x
// slowdown even with -O2. To prevent this, store the value in a local.
size_t bufsize = sx * sy * sz;
for (size_t i = 0; i != bufsize; i++)
result[i] = result[i] * np.scale + np.offset;
}

2 changes: 0 additions & 2 deletions src/noise.h
Expand Up @@ -151,8 +151,6 @@ class Noise {
float *perlinMap2D(float x, float y, float *persistence_map=NULL);
float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);

void transformNoiseMap();

private:
void allocBuffers();
void resizeNoiseBuf(bool is3d);
Expand Down
24 changes: 10 additions & 14 deletions src/script/lua_api/l_noise.cpp
Expand Up @@ -149,7 +149,7 @@ int LuaPerlinNoiseMap::gc_object(lua_State *L)
int LuaPerlinNoiseMap::l_get2dMap(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
int i = 0;
size_t i = 0;

LuaPerlinNoiseMap *o = checkobject(L, 1);
v2f p = read_v2f(L, 2);
Expand All @@ -161,8 +161,7 @@ int LuaPerlinNoiseMap::l_get2dMap(lua_State *L)
for (int y = 0; y != n->sy; y++) {
lua_newtable(L);
for (int x = 0; x != n->sx; x++) {
float noiseval = n->np.offset + n->np.scale * n->result[i++];
lua_pushnumber(L, noiseval);
lua_pushnumber(L, n->result[i++]);
lua_rawseti(L, -2, x + 1);
}
lua_rawseti(L, -2, y + 1);
Expand All @@ -181,12 +180,11 @@ int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L)
Noise *n = o->noise;
n->perlinMap2D(p.X, p.Y);

int maplen = n->sx * n->sy;
size_t maplen = n->sx * n->sy;

lua_newtable(L);
for (int i = 0; i != maplen; i++) {
float noiseval = n->np.offset + n->np.scale * n->result[i];
lua_pushnumber(L, noiseval);
for (size_t i = 0; i != maplen; i++) {
lua_pushnumber(L, n->result[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
Expand All @@ -196,7 +194,7 @@ int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L)
int LuaPerlinNoiseMap::l_get3dMap(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
int i = 0;
size_t i = 0;

LuaPerlinNoiseMap *o = checkobject(L, 1);
v3f p = read_v3f(L, 2);
Expand All @@ -210,7 +208,7 @@ int LuaPerlinNoiseMap::l_get3dMap(lua_State *L)
for (int y = 0; y != n->sy; y++) {
lua_newtable(L);
for (int x = 0; x != n->sx; x++) {
lua_pushnumber(L, n->np.offset + n->np.scale * n->result[i++]);
lua_pushnumber(L, n->result[i++]);
lua_rawseti(L, -2, x + 1);
}
lua_rawseti(L, -2, y + 1);
Expand All @@ -231,13 +229,11 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
Noise *n = o->noise;
n->perlinMap3D(p.X, p.Y, p.Z);


int maplen = n->sx * n->sy * n->sz;
size_t maplen = n->sx * n->sy * n->sz;

lua_newtable(L);
for (int i = 0; i != maplen; i++) {
float noiseval = n->np.offset + n->np.scale * n->result[i];
lua_pushnumber(L, noiseval);
for (size_t i = 0; i != maplen; i++) {
lua_pushnumber(L, n->result[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
Expand Down

0 comments on commit 16baed0

Please sign in to comment.