4 void fractalnoise(unsigned char *noise, int size, int startgrid)
6 int x, y, g, g2, amplitude, min, max, size1 = size - 1, sizepower, gridpower;
8 #define n(x,y) noisebuf[((y)&size1)*size+((x)&size1)]
10 for (sizepower = 0;(1 << sizepower) < size;sizepower++);
11 if (size != (1 << sizepower))
13 Con_Printf("fractalnoise: size must be power of 2\n");
17 for (gridpower = 0;(1 << gridpower) < startgrid;gridpower++);
18 if (startgrid != (1 << gridpower))
20 Con_Printf("fractalnoise: grid must be power of 2\n");
24 startgrid = bound(0, startgrid, size);
26 amplitude = 0xFFFF; // this gets halved before use
27 noisebuf = (int *)Mem_Alloc(tempmempool, size*size*sizeof(int));
28 memset(noisebuf, 0, size*size*sizeof(int));
30 for (g2 = startgrid;g2;g2 >>= 1)
32 // brownian motion (at every smaller level there is random behavior)
34 for (y = 0;y < size;y += g2)
35 for (x = 0;x < size;x += g2)
36 n(x,y) += (rand()&litude);
41 // subdivide, diamond-square algorithm (really this has little to do with squares)
43 for (y = 0;y < size;y += g2)
44 for (x = 0;x < size;x += g2)
45 n(x+g,y+g) = (n(x,y) + n(x+g2,y) + n(x,y+g2) + n(x+g2,y+g2)) >> 2;
47 for (y = 0;y < size;y += g2)
48 for (x = 0;x < size;x += g2)
50 n(x+g,y) = (n(x,y) + n(x+g2,y) + n(x+g,y-g) + n(x+g,y+g)) >> 2;
51 n(x,y+g) = (n(x,y) + n(x,y+g2) + n(x-g,y+g) + n(x+g,y+g)) >> 2;
55 // find range of noise values
57 for (y = 0;y < size;y++)
58 for (x = 0;x < size;x++)
60 if (n(x,y) < min) min = n(x,y);
61 if (n(x,y) > max) max = n(x,y);
65 // normalize noise and copy to output
66 for (y = 0;y < size;y++)
67 for (x = 0;x < size;x++)
68 *noise++ = (unsigned char) (((n(x,y) - min) * 256) / max);
73 // unnormalized, used for explosions mainly, does not allocate/free memory (hence the name quick)
74 void fractalnoisequick(unsigned char *noise, int size, int startgrid)
76 int x, y, g, g2, amplitude, size1 = size - 1, sizepower, gridpower;
77 #define n(x,y) noise[((y)&size1)*size+((x)&size1)]
79 for (sizepower = 0;(1 << sizepower) < size;sizepower++);
80 if (size != (1 << sizepower))
82 Con_Printf("fractalnoise: size must be power of 2\n");
86 for (gridpower = 0;(1 << gridpower) < startgrid;gridpower++);
87 if (startgrid != (1 << gridpower))
89 Con_Printf("fractalnoise: grid must be power of 2\n");
93 startgrid = bound(0, startgrid, size);
95 amplitude = 255; // this gets halved before use
96 memset(noise, 0, size*size);
98 for (g2 = startgrid;g2;g2 >>= 1)
100 // brownian motion (at every smaller level there is random behavior)
102 for (y = 0;y < size;y += g2)
103 for (x = 0;x < size;x += g2)
104 n(x,y) += (rand()&litude);
109 // subdivide, diamond-square algorithm (really this has little to do with squares)
111 for (y = 0;y < size;y += g2)
112 for (x = 0;x < size;x += g2)
113 n(x+g,y+g) = (unsigned char) (((int) n(x,y) + (int) n(x+g2,y) + (int) n(x,y+g2) + (int) n(x+g2,y+g2)) >> 2);
115 for (y = 0;y < size;y += g2)
116 for (x = 0;x < size;x += g2)
118 n(x+g,y) = (unsigned char) (((int) n(x,y) + (int) n(x+g2,y) + (int) n(x+g,y-g) + (int) n(x+g,y+g)) >> 2);
119 n(x,y+g) = (unsigned char) (((int) n(x,y) + (int) n(x,y+g2) + (int) n(x-g,y+g) + (int) n(x+g,y+g)) >> 2);
126 #define NOISE_SIZE 256
127 #define NOISE_MASK 255
128 float noise4f(float x, float y, float z, float w)
134 static float noisetable[NOISE_SIZE];
135 static int r[NOISE_SIZE];
136 // LordHavoc: this is inspired by code I saw in Quake3, however I think my
137 // version is much cleaner and substantially faster as well
139 // the following changes were made:
140 // 1. for the permutation indexing (r[] array in this code) I substituted
141 // the ^ operator (which never overflows) for the original addition and
142 // masking code, this should not have any effect on quality.
143 // 2. removed the outermost randomization array lookup.
144 // (it really wasn't necessary, it's fine if X indexes the array
145 // directly without permutation indexing)
146 // 3. reimplemented the blending using frac[] arrays rather than a macro.
147 // (the original macro read one parameter twice - not good)
148 // 4. cleaned up the code by using 4 nested loops to make it read nicer
149 // (but then I unrolled it completely for speed, it still looks nicer).
152 // noisetable is a random-ish series of float values in +/- 1 range
153 for (i = 0;i < NOISE_SIZE;i++)
154 noisetable[i] = (rand() / (double)RAND_MAX) * 2 - 1;
155 // r is a remapping table to make each dimension of the index have different indexing behavior
156 for (i = 0;i < NOISE_SIZE;i++)
157 r[i] = (int)(rand() * (double)NOISE_SIZE / ((double)RAND_MAX + 1)) & NOISE_MASK;
158 // that & is only needed if RAND_MAX is > the range of double, which isn't the case on most platforms
160 frac[1][0] = x - floor(x);index[0][0] = ((int)floor(x)) & NOISE_MASK;
161 frac[1][1] = y - floor(y);index[1][0] = ((int)floor(y)) & NOISE_MASK;
162 frac[1][2] = z - floor(z);index[2][0] = ((int)floor(z)) & NOISE_MASK;
163 frac[1][3] = w - floor(w);index[3][0] = ((int)floor(w)) & NOISE_MASK;
164 for (i = 0;i < 4;i++)
165 frac[i][0] = 1 - frac[i][1];
166 for (i = 0;i < 4;i++)
167 index[i][1] = (index[i][0] < NOISE_SIZE - 1) ? (index[i][0] + 1) : 0;
170 v[0] = frac[1][0] * (frac[0][0] * noisetable[r[r[r[index[3][0]] ^ index[2][0]] ^ index[1][0]] ^ index[0][0]] + frac[0][1] * noisetable[r[r[r[index[3][0]] ^ index[2][0]] ^ index[1][0]] ^ index[0][1]]) + frac[1][1] * (frac[0][0] * noisetable[r[r[r[index[3][0]] ^ index[2][0]] ^ index[1][1]] ^ index[0][0]] + frac[0][1] * noisetable[r[r[r[index[3][0]] ^ index[2][0]] ^ index[1][1]] ^ index[0][1]]);
171 v[1] = frac[1][0] * (frac[0][0] * noisetable[r[r[r[index[3][0]] ^ index[2][1]] ^ index[1][0]] ^ index[0][0]] + frac[0][1] * noisetable[r[r[r[index[3][0]] ^ index[2][1]] ^ index[1][0]] ^ index[0][1]]) + frac[1][1] * (frac[0][0] * noisetable[r[r[r[index[3][0]] ^ index[2][1]] ^ index[1][1]] ^ index[0][0]] + frac[0][1] * noisetable[r[r[r[index[3][0]] ^ index[2][1]] ^ index[1][1]] ^ index[0][1]]);
172 v[2] = frac[1][0] * (frac[0][0] * noisetable[r[r[r[index[3][1]] ^ index[2][0]] ^ index[1][0]] ^ index[0][0]] + frac[0][1] * noisetable[r[r[r[index[3][1]] ^ index[2][0]] ^ index[1][0]] ^ index[0][1]]) + frac[1][1] * (frac[0][0] * noisetable[r[r[r[index[3][1]] ^ index[2][0]] ^ index[1][1]] ^ index[0][0]] + frac[0][1] * noisetable[r[r[r[index[3][1]] ^ index[2][0]] ^ index[1][1]] ^ index[0][1]]);
173 v[3] = frac[1][0] * (frac[0][0] * noisetable[r[r[r[index[3][1]] ^ index[2][1]] ^ index[1][0]] ^ index[0][0]] + frac[0][1] * noisetable[r[r[r[index[3][1]] ^ index[2][1]] ^ index[1][0]] ^ index[0][1]]) + frac[1][1] * (frac[0][0] * noisetable[r[r[r[index[3][1]] ^ index[2][1]] ^ index[1][1]] ^ index[0][0]] + frac[0][1] * noisetable[r[r[r[index[3][1]] ^ index[2][1]] ^ index[1][1]] ^ index[0][1]]);
174 return frac[3][0] * (frac[2][0] * v[0] + frac[2][1] * v[1]) + frac[3][1] * (frac[2][0] * v[2] + frac[2][1] * v[3]);
177 v[ 0] = noisetable[r[r[r[index[3][0]] ^ index[2][0]] ^ index[1][0]] ^ index[0][0]];
178 v[ 1] = noisetable[r[r[r[index[3][0]] ^ index[2][0]] ^ index[1][0]] ^ index[0][1]];
179 v[ 2] = noisetable[r[r[r[index[3][0]] ^ index[2][0]] ^ index[1][1]] ^ index[0][0]];
180 v[ 3] = noisetable[r[r[r[index[3][0]] ^ index[2][0]] ^ index[1][1]] ^ index[0][1]];
181 v[ 4] = noisetable[r[r[r[index[3][0]] ^ index[2][1]] ^ index[1][0]] ^ index[0][0]];
182 v[ 5] = noisetable[r[r[r[index[3][0]] ^ index[2][1]] ^ index[1][0]] ^ index[0][1]];
183 v[ 6] = noisetable[r[r[r[index[3][0]] ^ index[2][1]] ^ index[1][1]] ^ index[0][0]];
184 v[ 7] = noisetable[r[r[r[index[3][0]] ^ index[2][1]] ^ index[1][1]] ^ index[0][1]];
185 v[ 8] = noisetable[r[r[r[index[3][1]] ^ index[2][0]] ^ index[1][0]] ^ index[0][0]];
186 v[ 9] = noisetable[r[r[r[index[3][1]] ^ index[2][0]] ^ index[1][0]] ^ index[0][1]];
187 v[10] = noisetable[r[r[r[index[3][1]] ^ index[2][0]] ^ index[1][1]] ^ index[0][0]];
188 v[11] = noisetable[r[r[r[index[3][1]] ^ index[2][0]] ^ index[1][1]] ^ index[0][1]];
189 v[12] = noisetable[r[r[r[index[3][1]] ^ index[2][1]] ^ index[1][0]] ^ index[0][0]];
190 v[13] = noisetable[r[r[r[index[3][1]] ^ index[2][1]] ^ index[1][0]] ^ index[0][1]];
191 v[14] = noisetable[r[r[r[index[3][1]] ^ index[2][1]] ^ index[1][1]] ^ index[0][0]];
192 v[15] = noisetable[r[r[r[index[3][1]] ^ index[2][1]] ^ index[1][1]] ^ index[0][1]];
193 v[16] = frac[0][0] * v[ 0] + frac[0][1] * v[ 1];
194 v[17] = frac[0][0] * v[ 2] + frac[0][1] * v[ 3];
195 v[18] = frac[0][0] * v[ 4] + frac[0][1] * v[ 5];
196 v[19] = frac[0][0] * v[ 6] + frac[0][1] * v[ 7];
197 v[20] = frac[0][0] * v[ 8] + frac[0][1] * v[ 9];
198 v[21] = frac[0][0] * v[10] + frac[0][1] * v[11];
199 v[22] = frac[0][0] * v[12] + frac[0][1] * v[13];
200 v[23] = frac[0][0] * v[14] + frac[0][1] * v[15];
201 v[24] = frac[1][0] * v[16] + frac[1][1] * v[17];
202 v[25] = frac[1][0] * v[18] + frac[1][1] * v[19];
203 v[26] = frac[1][0] * v[20] + frac[1][1] * v[21];
204 v[27] = frac[1][0] * v[22] + frac[1][1] * v[23];
205 v[28] = frac[2][0] * v[24] + frac[2][1] * v[25];
206 v[29] = frac[2][0] * v[26] + frac[2][1] * v[27];
207 return frac[3][0] * v[28] + frac[3][1] * v[29];
210 for (l = 0;l < 2;l++)
212 for (k = 0;k < 2;k++)
214 for (j = 0;j < 2;j++)
216 for (i = 0;i < 2;i++)
217 v[l][k][j][i] = noisetable[r[r[r[index[l][3]] ^ index[k][2]] ^ index[j][1]] ^ index[i][0]];
218 v[l][k][j][2] = frac[0][0] * v[l][k][j][0] + frac[0][1] * v[l][k][j][1];
220 v[l][k][2][2] = frac[1][0] * v[l][k][0][2] + frac[1][1] * v[l][k][1][2];
222 v[l][2][2][2] = frac[2][0] * v[l][0][2][2] + frac[2][1] * v[l][1][2][2];
224 v[2][2][2][2] = frac[3][0] * v[0][2][2][2] + frac[3][1] * v[1][2][2][2];