3d noise stuff
parent
ec3cb2d1d7
commit
48a772b3c6
|
@ -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="<<x<<" y="<<y<<" x0="<<x0<<" y0="<<y0<<" xl="<<xl<<" yl="<<yl<<" n00="<<n00<<" n10="<<n01<<" s="<<s<<std::endl;
|
||||
std::cout<<"cos_lookup[n00]="<<(cos_lookup[n00])<<" cos_lookup[(n00-4)%16]="<<(cos_lookup[(n00-4)%16])<<std::endl;*/
|
||||
// Interpolate between the values
|
||||
return biLinearInterpolation(s,u,v,w,xl,yl);
|
||||
}
|
||||
|
||||
double noise3d_gradient(double x, double y, double z, 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);
|
||||
int z0 = (z > 0.0 ? (int)z : (int)z - 1);
|
||||
// Calculate the remaining part of the coordinates
|
||||
double xl = x - (double)x0;
|
||||
double yl = y - (double)y0;
|
||||
double zl = y - (double)z0;
|
||||
// Get values for corners of cube
|
||||
double v000 = noise3d(x0, y0, z0, seed);
|
||||
double v100 = noise3d(x0+1, y0, z0, seed);
|
||||
double v010 = noise3d(x0, y0+1, z0, seed);
|
||||
double v110 = noise3d(x0+1, y0+1, z0, seed);
|
||||
double v001 = noise3d(x0, y0, z0+1, seed);
|
||||
double v101 = noise3d(x0+1, y0, z0+1, seed);
|
||||
double v011 = noise3d(x0, y0+1, z0+1, seed);
|
||||
double v111 = noise3d(x0+1, y0+1, z0+1, seed);
|
||||
// Interpolate
|
||||
return triLinearInterpolation(v000,v100,v010,v110,v001,v101,v011,v111,xl,yl,zl);
|
||||
}
|
||||
|
||||
double noise2d_perlin(double x, double y, int seed,
|
||||
int octaves, double persistence)
|
||||
{
|
||||
|
@ -98,3 +153,18 @@ double noise2d_perlin(double x, double y, int seed,
|
|||
return a;
|
||||
}
|
||||
|
||||
double noise3d_perlin(double x, double y, double z, int seed,
|
||||
int octaves, double persistence)
|
||||
{
|
||||
double a = 0;
|
||||
double f = 1.0;
|
||||
double g = 1.0;
|
||||
for(int i=0; i<octaves; i++)
|
||||
{
|
||||
a += g * noise3d_gradient(x*f, y*f, z*f, seed+i);
|
||||
f *= 2.0;
|
||||
g *= persistence;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,11 +24,16 @@ double easeCurve(double t);
|
|||
|
||||
// Return value: -1 ... 1
|
||||
double noise2d(int x, int y, int seed);
|
||||
double noise3d(int x, int y, int z, int seed);
|
||||
|
||||
double noise2d_gradient(double x, double y, int seed);
|
||||
double noise3d_gradient(double x, double y, double z, int seed);
|
||||
|
||||
double noise2d_perlin(double x, double y, int seed,
|
||||
int octaves, double persistence);
|
||||
|
||||
double noise3d_perlin(double x, double y, double z, int seed,
|
||||
int octaves, double persistence);
|
||||
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in New Issue