]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - fractalnoise.c
optimized AngleVectors calls (pass NULL for vectors that should not be generated)
[xonotic/darkplaces.git] / fractalnoise.c
index e9899a47f84771e4bc3e22c2514ad20d514d0575..93622b14e4530f6f8f8223223bd79dfeda4353b3 100644 (file)
@@ -1,48 +1,50 @@
 
 #include "quakedef.h"
 
-void fractalnoise(unsigned char *noise, int size, int startgrid)
+void fractalnoise(byte *noise, int size, int startgrid)
 {
-       int x, y, g, g2, amplitude, min, max, size1 = size - 1;
+       int x, y, g, g2, amplitude, min, max, size1 = size - 1, sizepower, gridpower;
        int *noisebuf;
 #define n(x,y) noisebuf[((y)&size1)*size+((x)&size1)]
-       if (startgrid > size)
-               startgrid = size;
+
+       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 = 32767;
        noisebuf = qmalloc(size*size*sizeof(int));
        memset(noisebuf, 0, size*size*sizeof(int));
 
-       amplitude = 32767;
-       // quick 1x1 case which the rest of the code can't handle
-       if (startgrid < 2)
-       {
-               for (x = 0;x < size*size;x++)
-                       *noise++ = (rand()&255);
-               return;
-       }
-       g2 = startgrid;
-       // clear the starting grid (the rest does not need to be cleared, it will simply be overwritten)
-       for (y = 0;y < size;y += g2)
-               for (x = 0;x < size;x += g2)
-                       n(x,y) = 0;
-       for (;(g = g2 >> 1) >= 1;g2 >>= 1)
+       for (g2 = startgrid;g2;g2 >>= 1)
        {
-               // subdivide, diamond-square algorythm (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) = (n(x,y) + n(x+g2,y) + n(x,y+g2) + n(x+g2,y+g2)) >> 2;
-               // square
+               // 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+g,y) = (n(x,y) + n(x+g2,y) + n(x+g,y-g) + n(x+g,y+g)) >> 2;
-                               n(x,y+g) = (n(x,y) + n(x,y+g2) + n(x-g,y+g) + n(x+g,y+g)) >> 2;
-                       }
-               // brownian motion theory
-               amplitude >>= 1;
-               for (y = 0;y < size;y += g)
-                       for (x = 0;x < size;x += g)
                                n(x,y) += (rand()&amplitude);
+
+               g = g2 >> 1;
+               if (g)
+               {
+                       // subdivide, diamond-square algorythm (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) = (n(x,y) + n(x+g2,y) + n(x,y+g2) + 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) = (n(x,y) + n(x+g2,y) + n(x+g,y-g) + n(x+g,y+g)) >> 2;
+                                       n(x,y+g) = (n(x,y) + n(x,y+g2) + n(x-g,y+g) + n(x+g,y+g)) >> 2;
+                               }
+               }
        }
        // find range of noise values
        min = max = 0;
@@ -59,4 +61,4 @@ void fractalnoise(unsigned char *noise, int size, int startgrid)
                        *noise++ = (n(x,y) - min) * 255 / max;
        qfree(noisebuf);
 #undef n
-}
\ No newline at end of file
+}