From: divverent Date: Sat, 26 May 2007 11:42:33 +0000 (+0000) Subject: change noise4f to a function that does not crash X-Git-Tag: xonotic-v0.1.0preview~3089 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=5f950f0e9c2943048fa8950737c0c47528a1fec9;p=xonotic%2Fdarkplaces.git change noise4f to a function that does not crash git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7371 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/fractalnoise.c b/fractalnoise.c index 40beb8af..1bb1c55a 100644 --- a/fractalnoise.c +++ b/fractalnoise.c @@ -154,12 +154,13 @@ float noise4f(float x, float y, float z, float w) noisetable[i] = (rand() / (double)RAND_MAX) * 2 - 1; // r is a remapping table to make each dimension of the index have different indexing behavior for (i = 0;i < NOISE_SIZE;i++) - r[i] = (int)(rand() * (double)NOISE_MASK / (double)RAND_MAX); + r[i] = (int)(rand() * (double)NOISE_SIZE / ((double)RAND_MAX + 1)) & NOISE_MASK; + // that & is only needed if RAND_MAX is > the range of double, which isn't the case on most platforms } - frac[1][0] = x - floor(x);index[0][0] = (int)floor(x); - frac[1][1] = y - floor(y);index[1][0] = (int)floor(y); - frac[1][2] = z - floor(z);index[2][0] = (int)floor(z); - frac[1][3] = w - floor(w);index[3][0] = (int)floor(w); + frac[1][0] = x - floor(x);index[0][0] = ((int)floor(x)) & NOISE_MASK; + frac[1][1] = y - floor(y);index[1][0] = ((int)floor(y)) & NOISE_MASK; + frac[1][2] = z - floor(z);index[2][0] = ((int)floor(z)) & NOISE_MASK; + frac[1][3] = w - floor(w);index[3][0] = ((int)floor(w)) & NOISE_MASK; for (i = 0;i < 4;i++) frac[i][0] = 1 - frac[i][1]; for (i = 0;i < 4;i++)