X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=fractalnoise.c;h=06d00db0eedbc805291b94771b13178d17bbcc42;hb=88527b4ee4fadb34fdb90f5ca99d17f3553506af;hp=93622b14e4530f6f8f8223223bd79dfeda4353b3;hpb=e4b3858e7aca0ead91be1d8f675db084d025abad;p=xonotic%2Fdarkplaces.git diff --git a/fractalnoise.c b/fractalnoise.c index 93622b14..06d00db0 100644 --- a/fractalnoise.c +++ b/fractalnoise.c @@ -1,7 +1,7 @@ #include "quakedef.h" -void fractalnoise(byte *noise, int size, int startgrid) +void fractalnoise(qbyte *noise, int size, int startgrid) { int x, y, g, g2, amplitude, min, max, size1 = size - 1, sizepower, gridpower; int *noisebuf; @@ -17,8 +17,8 @@ void fractalnoise(byte *noise, int size, int startgrid) startgrid = bound(0, startgrid, size); - amplitude = 32767; - noisebuf = qmalloc(size*size*sizeof(int)); + amplitude = 0xFFFF; // this gets halved before use + noisebuf = Mem_Alloc(tempmempool, size*size*sizeof(int)); memset(noisebuf, 0, size*size*sizeof(int)); for (g2 = startgrid;g2;g2 >>= 1) @@ -32,7 +32,7 @@ void fractalnoise(byte *noise, int size, int startgrid) g = g2 >> 1; if (g) { - // subdivide, diamond-square algorythm (really this has little to do with squares) + // subdivide, diamond-square algorithm (really this has little to do with squares) // diamond for (y = 0;y < size;y += g2) for (x = 0;x < size;x += g2) @@ -55,10 +55,59 @@ void fractalnoise(byte *noise, int size, int startgrid) if (n(x,y) > max) max = n(x,y); } max -= min; + max++; // normalize noise and copy to output for (y = 0;y < size;y++) for (x = 0;x < size;x++) - *noise++ = (n(x,y) - min) * 255 / max; - qfree(noisebuf); + *noise++ = (qbyte) (((n(x,y) - min) * 256) / max); + Mem_Free(noisebuf); #undef n } + +// unnormalized, used for explosions mainly, does not allocate/free memory (hence the name quick) +void fractalnoisequick(qbyte *noise, int size, int startgrid) +{ + int x, y, g, g2, amplitude, size1 = size - 1, sizepower, gridpower; +#define n(x,y) noise[((y)&size1)*size+((x)&size1)] + + for (sizepower = 0;(1 << sizepower) < size;sizepower++); + if (size != (1 << sizepower)) + Sys_Error("fractalnoise: size must be power of 2\n"); + + for (gridpower = 0;(1 << gridpower) < startgrid;gridpower++); + if (startgrid != (1 << gridpower)) + Sys_Error("fractalnoise: grid must be power of 2\n"); + + startgrid = bound(0, startgrid, size); + + amplitude = 255; // this gets halved before use + memset(noise, 0, size*size); + + for (g2 = startgrid;g2;g2 >>= 1) + { + // brownian motion (at every smaller level there is random behavior) + amplitude >>= 1; + for (y = 0;y < size;y += g2) + for (x = 0;x < size;x += g2) + n(x,y) += (rand()&litude); + + g = g2 >> 1; + if (g) + { + // subdivide, diamond-square algorithm (really this has little to do with squares) + // diamond + for (y = 0;y < size;y += g2) + for (x = 0;x < size;x += g2) + n(x+g,y+g) = (qbyte) (((int) n(x,y) + (int) n(x+g2,y) + (int) n(x,y+g2) + (int) n(x+g2,y+g2)) >> 2); + // square + for (y = 0;y < size;y += g2) + for (x = 0;x < size;x += g2) + { + n(x+g,y) = (qbyte) (((int) n(x,y) + (int) n(x+g2,y) + (int) n(x+g,y-g) + (int) n(x+g,y+g)) >> 2); + n(x,y+g) = (qbyte) (((int) n(x,y) + (int) n(x,y+g2) + (int) n(x-g,y+g) + (int) n(x+g,y+g)) >> 2); + } + } + } +#undef n +} +