summaryrefslogtreecommitdiff
path: root/src/noise.cpp
diff options
context:
space:
mode:
authorkwolekr <mirrorisim@gmail.com>2012-12-18 16:45:50 -0500
committerPerttu Ahola <celeron55@gmail.com>2013-01-21 21:41:37 +0200
commitbddd5f2b98a56fc735d5687ecd0b43767aec8066 (patch)
treee772cf144f6ebfcff1d2a3f5551b0cdabad5419e /src/noise.cpp
parent96898c179458174f858bab6363636ef231b49865 (diff)
downloadminetest-bddd5f2b98a56fc735d5687ecd0b43767aec8066.tar.gz
minetest-bddd5f2b98a56fc735d5687ecd0b43767aec8066.tar.bz2
minetest-bddd5f2b98a56fc735d5687ecd0b43767aec8066.zip
Cleaned & enhanced noise object management
Diffstat (limited to 'src/noise.cpp')
-rw-r--r--src/noise.cpp89
1 files changed, 60 insertions, 29 deletions
diff --git a/src/noise.cpp b/src/noise.cpp
index 3874848ad..c038384db 100644
--- a/src/noise.cpp
+++ b/src/noise.cpp
@@ -270,45 +270,25 @@ float contour(float v)
Noise::Noise(NoiseParams *np, int seed, int sx, int sy) {
- int nlx, nly;
- float ofactor;
-
- //maximum possible spread value factor
- ofactor = (float)(1 << (np->octaves - 1));
-
- //noise lattice point count
- //(int)(sz * spread * ofactor) is # of lattice points crossed due to length
- // + 2 for the two initial endpoints
- // + 1 for potentially crossing a boundary due to offset
- nlx = (int)(sx * ofactor / np->spread.X) + 3;
- nly = (int)(sy * ofactor / np->spread.Y) + 3;
-
- this->np = np;
- this->seed = seed;
- this->sx = sx;
- this->sy = sy;
- this->sz = 1;
- this->noisebuf = new float[nlx * nly];
- this->buf = new float[sx * sy];
- this->result = new float[sx * sy];
+ init(np, seed, sx, sy, 1);
}
Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz) {
- int nlx, nly, nlz;
- float ofactor;
+ init(np, seed, sx, sy, sz);
+}
- ofactor = (float)(1 << (np->octaves - 1));
- nlx = (int)(sx * ofactor / np->spread.X) + 3;
- nly = (int)(sy * ofactor / np->spread.Y) + 3;
- nlz = (int)(sz * ofactor / np->spread.Z) + 3;
+void Noise::init(NoiseParams *np, int seed, int sx, int sy, int sz) {
this->np = np;
this->seed = seed;
this->sx = sx;
this->sy = sy;
this->sz = sz;
- this->noisebuf = new float[nlx * nly * nlz];
+
+ this->noisebuf = NULL;
+ resizeNoiseBuf(sz > 1);
+
this->buf = new float[sx * sy * sz];
this->result = new float[sx * sy * sz];
}
@@ -321,6 +301,58 @@ Noise::~Noise() {
}
+void Noise::setSize(int sx, int sy) {
+ setSize(sx, sy, 1);
+}
+
+
+void Noise::setSize(int sx, int sy, int sz) {
+ this->sx = sx;
+ this->sy = sy;
+ this->sz = sz;
+
+ resizeNoiseBuf(sz > 1);
+
+ delete[] buf;
+ delete[] result;
+}
+
+
+void Noise::setSpreadFactor(v3f spread) {
+ this->np->spread = spread;
+
+ resizeNoiseBuf(sz > 1);
+}
+
+
+void Noise::setOctaves(int octaves) {
+ this->np->octaves = octaves;
+
+ resizeNoiseBuf(sz > 1);
+}
+
+
+void Noise::resizeNoiseBuf(bool is3d) {
+ int nlx, nly, nlz;
+ float ofactor;
+
+ //maximum possible spread value factor
+ ofactor = (float)(1 << (np->octaves - 1));
+
+ //noise lattice point count
+ //(int)(sz * spread * ofactor) is # of lattice points crossed due to length
+ // + 2 for the two initial endpoints
+ // + 1 for potentially crossing a boundary due to offset
+ nlx = (int)(sx * ofactor / np->spread.X) + 3;
+ nly = (int)(sy * ofactor / np->spread.Y) + 3;
+ nlz = is3d ? (int)(sz * ofactor / np->spread.Z) + 3 : 1;
+
+ if (noisebuf)
+ delete[] noisebuf;
+ noisebuf = new float[nlx * nly * nlz];
+}
+
+
/*
* NB: This algorithm is not optimal in terms of space complexity. The entire
* integer lattice of noise points could be done as 2 lines instead, and for 3D,
@@ -344,7 +376,6 @@ void Noise::gradientMap2D(float x, float y, float step_x, float step_y, int seed
orig_u = u;
//calculate noise point lattice
-
nlx = (int)(u + sx * step_x) + 2;
nly = (int)(v + sy * step_y) + 2;
index = 0;