+
+#define idx(x, y, z) ((z) * nly * nlx + (y) * nlx + (x))
+void Noise::gradientMap3D(
+ float x, float y, float z,
+ float step_x, float step_y, float step_z,
+ s32 seed)
+{
+ float v000, v010, v100, v110;
+ float v001, v011, v101, v111;
+ float u, v, w, orig_u, orig_v;
+ u32 index, i, j, k, noisex, noisey, noisez;
+ u32 nlx, nly, nlz;
+ s32 x0, y0, z0;
+
+ Interp3dFxn interpolate = (np.flags & NOISE_FLAG_EASED) ?
+ triLinearInterpolation : triLinearInterpolationNoEase;
+
+ x0 = floor(x);
+ y0 = floor(y);
+ z0 = floor(z);
+ u = x - (float)x0;
+ v = y - (float)y0;
+ w = z - (float)z0;
+ orig_u = u;
+ orig_v = v;
+
+ //calculate noise point lattice
+ nlx = (u32)(u + sx * step_x) + 2;
+ nly = (u32)(v + sy * step_y) + 2;
+ nlz = (u32)(w + sz * step_z) + 2;
+ index = 0;
+ for (k = 0; k != nlz; k++)
+ for (j = 0; j != nly; j++)
+ for (i = 0; i != nlx; i++)
+ noise_buf[index++] = noise3d(x0 + i, y0 + j, z0 + k, seed);
+
+ //calculate interpolations
+ index = 0;
+ noisey = 0;
+ noisez = 0;
+ for (k = 0; k != sz; k++) {
+ v = orig_v;
+ noisey = 0;
+ for (j = 0; j != sy; j++) {
+ v000 = noise_buf[idx(0, noisey, noisez)];
+ v100 = noise_buf[idx(1, noisey, noisez)];
+ v010 = noise_buf[idx(0, noisey + 1, noisez)];
+ v110 = noise_buf[idx(1, noisey + 1, noisez)];
+ v001 = noise_buf[idx(0, noisey, noisez + 1)];
+ v101 = noise_buf[idx(1, noisey, noisez + 1)];
+ v011 = noise_buf[idx(0, noisey + 1, noisez + 1)];
+ v111 = noise_buf[idx(1, noisey + 1, noisez + 1)];
+
+ u = orig_u;
+ noisex = 0;
+ for (i = 0; i != sx; i++) {
+ gradient_buf[index++] = interpolate(
+ v000, v100, v010, v110,
+ v001, v101, v011, v111,
+ u, v, w);
+
+ u += step_x;
+ if (u >= 1.0) {
+ u -= 1.0;
+ noisex++;
+ v000 = v100;
+ v010 = v110;
+ v100 = noise_buf[idx(noisex + 1, noisey, noisez)];
+ v110 = noise_buf[idx(noisex + 1, noisey + 1, noisez)];
+ v001 = v101;
+ v011 = v111;
+ v101 = noise_buf[idx(noisex + 1, noisey, noisez + 1)];
+ v111 = noise_buf[idx(noisex + 1, noisey + 1, noisez + 1)];
+ }
+ }
+
+ v += step_y;
+ if (v >= 1.0) {
+ v -= 1.0;
+ noisey++;
+ }
+ }
+
+ w += step_z;
+ if (w >= 1.0) {
+ w -= 1.0;
+ noisez++;
+ }
+ }
+}
+#undef idx
+
+
+float *Noise::perlinMap2D(float x, float y, float *persistence_map)
+{
+ float f = 1.0, g = 1.0;
+ size_t bufsize = sx * sy;
+
+ x /= np.spread.X;
+ y /= np.spread.Y;
+
+ memset(result, 0, sizeof(float) * bufsize);
+
+ if (persistence_map) {
+ if (!persist_buf)
+ persist_buf = new float[bufsize];
+ for (size_t i = 0; i != bufsize; i++)
+ persist_buf[i] = 1.0;
+ }
+
+ for (size_t oct = 0; oct < np.octaves; oct++) {
+ gradientMap2D(x * f, y * f,
+ f / np.spread.X, f / np.spread.Y,
+ seed + np.seed + oct);
+
+ updateResults(g, persist_buf, persistence_map, bufsize);
+
+ f *= np.lacunarity;
+ 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;
+}
+
+
+float *Noise::perlinMap3D(float x, float y, float z, float *persistence_map)
+{
+ float f = 1.0, g = 1.0;
+ size_t bufsize = sx * sy * sz;
+
+ x /= np.spread.X;
+ y /= np.spread.Y;
+ z /= np.spread.Z;
+
+ memset(result, 0, sizeof(float) * bufsize);
+
+ if (persistence_map) {
+ if (!persist_buf)
+ persist_buf = new float[bufsize];
+ for (size_t i = 0; i != bufsize; i++)
+ persist_buf[i] = 1.0;
+ }
+
+ for (size_t oct = 0; oct < np.octaves; oct++) {
+ gradientMap3D(x * f, y * f, z * f,
+ f / np.spread.X, f / np.spread.Y, f / np.spread.Z,
+ seed + np.seed + oct);
+
+ updateResults(g, persist_buf, persistence_map, bufsize);
+
+ f *= np.lacunarity;
+ 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;
+}
+
+
+void Noise::updateResults(float g, float *gmap,
+ const float *persistence_map, size_t bufsize)
+{
+ // This looks very ugly, but it is 50-70% faster than having
+ // conditional statements inside the loop
+ if (np.flags & NOISE_FLAG_ABSVALUE) {
+ if (persistence_map) {
+ for (size_t i = 0; i != bufsize; i++) {
+ result[i] += gmap[i] * fabs(gradient_buf[i]);
+ gmap[i] *= persistence_map[i];
+ }
+ } else {
+ for (size_t i = 0; i != bufsize; i++)
+ result[i] += g * fabs(gradient_buf[i]);
+ }
+ } else {
+ if (persistence_map) {
+ for (size_t i = 0; i != bufsize; i++) {
+ result[i] += gmap[i] * gradient_buf[i];
+ gmap[i] *= persistence_map[i];
+ }
+ } else {
+ for (size_t i = 0; i != bufsize; i++)
+ result[i] += g * gradient_buf[i];
+ }
+ }
+}