From: Perttu Ahola Date: Sat, 26 Feb 2011 18:16:47 +0000 (+0200) Subject: 3d noise stuff X-Git-Url: http://81.2.79.47:8989/gitweb/?a=commitdiff_plain;h=48a772b3c6388cbe7f9003557f27cc2cf51b8fe8;p=zefram%2Fminetest%2Fminetest_engine.git 3d noise stuff --- diff --git a/src/noise.cpp b/src/noise.cpp index a98dd588..7b651dbb 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define NOISE_MAGIC_X 1619 #define NOISE_MAGIC_Y 31337 +#define NOISE_MAGIC_Z 52591 #define NOISE_MAGIC_SEED 1013 double cos_lookup[16] = { @@ -50,6 +51,29 @@ double biLinearInterpolation(double x0y0, double x1y0, double x0y1, double x1y1, return linearInterpolation(u,v,ty); } +double triLinearInterpolation( + double v000, double v100, double v010, double v110, + double v001, double v101, double v011, double v111, + double x, double y, double z) +{ + /*double tx = easeCurve(x); + double ty = easeCurve(y); + double tz = easeCurve(z);*/ + double tx = x; + double ty = y; + double tz = z; + return( + v000*(1-tx)*(1-ty)*(1-tz) + + v100*tx*(1-ty)*(1-tz) + + v010*(1-tx)*ty*(1-tz) + + v110*tx*ty*(1-tz) + + v001*(1-tx)*(1-ty)*tz + + v101*tx*(1-ty)*tz + + v011*(1-tx)*ty*tz + + v111*tx*ty*tz + ); +} + double noise2d(int x, int y, int seed) { int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y @@ -59,30 +83,61 @@ double noise2d(int x, int y, int seed) return 1.0 - (double)n/1073741824; } +double noise3d(int x, int y, int z, int seed) +{ + int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z + + NOISE_MAGIC_SEED * seed) & 0x7fffffff; + n = (n>>13)^n; + n = (n * (n*n*60493+19990303) + 1376312589) & 0x7fffffff; + return 1.0 - (double)n/1073741824; +} + double noise2d_gradient(double x, double y, int seed) { + // Calculate the integer coordinates int x0 = (x > 0.0 ? (int)x : (int)x - 1); int y0 = (y > 0.0 ? (int)y : (int)y - 1); + // Calculate the remaining part of the coordinates double xl = x - (double)x0; double yl = y - (double)y0; + // Calculate random cosine lookup table indices for the integer corners. + // They are looked up as unit vector gradients from the lookup table. int n00 = (int)((noise2d(x0, y0, seed)+1)*8); int n10 = (int)((noise2d(x0+1, y0, seed)+1)*8); int n01 = (int)((noise2d(x0, y0+1, seed)+1)*8); int n11 = (int)((noise2d(x0+1, y0+1, seed)+1)*8); - /* In this format, these fail to work on MSVC8 if n00 < 4 - double s = dotProduct(cos_lookup[n00], cos_lookup[(n00-4)%16], xl, yl); - double u = dotProduct(-cos_lookup[n10], cos_lookup[(n10-4)%16], 1.-xl, yl); - double v = dotProduct(cos_lookup[n01], -cos_lookup[(n01-4)%16], xl, 1.-yl); - double w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11-4)%16], 1.-xl, 1.-yl);*/ + // Make a dot product for the gradients and the positions, to get the values double s = dotProduct(cos_lookup[n00], cos_lookup[(n00+12)%16], xl, yl); double u = dotProduct(-cos_lookup[n10], cos_lookup[(n10+12)%16], 1.-xl, yl); double v = dotProduct(cos_lookup[n01], -cos_lookup[(n01+12)%16], xl, 1.-yl); double w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11+12)%16], 1.-xl, 1.-yl); - /*std::cout<<"x="<