-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEngine.c
302 lines (256 loc) · 8.79 KB
/
Engine.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "Engine.h"
#include "Trig.h"
typedef struct FixVector2_
{
Fixed x;
Fixed y;
} FixVector2;
static int x0_cache[2];
static int y0_cache[2];
static FixVector2 grid_cache[2][2][2];
int randoms = 0;
int rotations = 0;
int gradients = 0;
int interpolations = 0;
#ifndef __P2K__
float to_float(Fixed fp)
{
int int_part = fp >> FRAC_BITS;
int frac_part = fp & FRAC_MASK;
if (fp & 0x80000000)
{
int_part |= SIGN_MASK;
}
return (float)int_part + (((float)frac_part) / (1 << FRAC_BITS));
}
#endif
FixVector2 randomGradient(int ix, int iy)
{
Fixed random;
FixVector2 v;
// No precomputed gradients mean this works for any number of grid coordinates
unsigned a = ix, b = iy;
a *= 3284157443u;
b ^= a << 16 | a >> 16;
b *= 1911520717u;
a ^= b << 16 | b >> 16;
a *= 2048419325u;
random = FIX_MUL((a >> 22), FIX(360)); // in [0, 360]
v.x = fix_cos(random);
v.y = fix_sin(random);
randoms++;
return v;
}
// Rotate vector by angle
FixVector2 fix_Vector2Rotate(FixVector2 v, Fixed angle)
{
FixVector2 result = {0};
Fixed cosres = fix_cos(angle);
Fixed sinres = fix_sin(angle);
result.x = FIX_MUL(v.x, cosres) - FIX_MUL(v.y, sinres);
result.y = FIX_MUL(v.x, sinres) + FIX_MUL(v.y, cosres);
rotations++;
return result;
}
// Calculate two vectors dot product
Fixed fix_Vector2DotProduct(FixVector2 v1, const FixVector2 *v2)
{
int result = (FIX_MUL(v1.x, v2->x) + FIX_MUL(v1.y, v2->y));
return result;
}
// Computes the dot product of the distance and gradient vectors.
Fixed dotGridGradient(const FixVector2 *gradient, int ix, int iy, Fixed x, Fixed y)
{
// Compute the distance vector
Fixed result;
FixVector2 dist;
dist.x = x - FIX(ix);
dist.y = y - FIX(iy);
gradients++;
result = fix_Vector2DotProduct(dist, gradient);
// if (ix == 3 && iy == 2)
// {
// printf(" %d %d: %f %f; %f %f: %f\n", ix, iy, to_float(gradient->x), to_float(gradient->y), to_float(x), to_float(y), to_float(result));
// }
return result;
}
// cubic interpolation
Fixed interpolate(Fixed a0, Fixed a1, Fixed w)
{
interpolations++;
return FIX_MUL(FIX_MUL(FIX_MUL((a1 - a0), (FIX(3) - FIX_MUL(w, FIX(2)))), w), w) + a0;
// return a0 + FIX_MUL(w, (a1 - a0));
}
// Compute Perlin noise at coordinates x, y
Fixed perlin(Fixed x, Fixed y, Fixed angle, int cache_idx)
{
// Determine grid cell coordinates
int x0 = TO_INT(x);
int x1 = x0 + 1;
int y0 = TO_INT(y);
int y1 = y0 + 1;
// Determine interpolation weights
// Could also use higher order polynomial/s-curve here
Fixed sx = x - FIX(x0);
Fixed sy = y - FIX(y0);
// Interpolate between grid point gradients
Fixed n0, n1, ix0, ix1, value;
if (x0 != x0_cache[cache_idx] || y0 != y0_cache[cache_idx])
{
grid_cache[cache_idx][0][0] = fix_Vector2Rotate(randomGradient(x0, y0), angle);
grid_cache[cache_idx][0][1] = fix_Vector2Rotate(randomGradient(x0, y1), angle);
grid_cache[cache_idx][1][0] = fix_Vector2Rotate(randomGradient(x1, y0), angle);
grid_cache[cache_idx][1][1] = fix_Vector2Rotate(randomGradient(x1, y1), angle);
x0_cache[cache_idx] = x0;
y0_cache[cache_idx] = y0;
}
n0 = dotGridGradient(&grid_cache[cache_idx][0][0], x0, y0, x, y);
n1 = dotGridGradient(&grid_cache[cache_idx][1][0], x1, y0, x, y);
ix0 = interpolate(n0, n1, sx);
n0 = dotGridGradient(&grid_cache[cache_idx][0][1], x0, y1, x, y);
n1 = dotGridGradient(&grid_cache[cache_idx][1][1], x1, y1, x, y);
ix1 = interpolate(n0, n1, sx);
value = interpolate(ix0, ix1, sy);
return (value + ONE) / 2;
}
//typedef unsigned char uchar;
// Generate 1-bit Perlin noise with cell size = 16
void perlin16_fast(int width, int height, Fixed angle)
{
FixVector2 cache[2][2];
Fixed dx = ONE >> 4;
Fixed dy = ONE >> 4;
int cell_width = width >> 4;
int cell_height = height >> 4;
uchar x0, y0;
for (y0 = 0; y0 < cell_height; y0++)
{
if (y0 == 0)
{
cache[1][0] = fix_Vector2Rotate(randomGradient(1, 0), angle);
}
else
{
cache[1][0] = cache[1][1];
}
cache[1][1] = fix_Vector2Rotate(randomGradient(1, y0 + 1), angle);
for (x0 = 0; x0 < cell_width; x0++)
{
uchar x1;
uchar y1;
Fixed x, y;
int i;
Fixed ix0, ix1;
if (x0 == 0)
{
cache[0][0] = fix_Vector2Rotate(randomGradient(0, y0), angle);
cache[0][1] = fix_Vector2Rotate(randomGradient(0, y0 + 1), angle);
}
else
{
cache[0][0] = cache[1][0];
cache[0][1] = cache[1][1];
}
cache[1][0] = fix_Vector2Rotate(randomGradient(x0 + 1, y0), angle);
cache[1][1] = fix_Vector2Rotate(randomGradient(x0 + 1, y0 + 1), angle);
x1 = x0 + 1;
y1 = y0 + 1;
i = x0 << 4;
// if (x0 == 3 && y0 == 2)
// {
// printf("------- start\n");
// }
for (x = FIX(x0); x < FIX(x1); x += dx, i++)
{
int j = y0 << 4;
Fixed n0_up_start = dotGridGradient(&cache[0][0], x0, y0, x, FIX(y0));
Fixed n0_up_end = dotGridGradient(&cache[0][0], x0, y0, x, FIX(y1));
Fixed d_n0_up = (n0_up_end - n0_up_start) >> 4;
Fixed n0_up, n1_up_start, n1_up_end, d_n1_up, n1_up, n0_low_start, n0_low_end, d_n1_low, d_n0_low;
Fixed n0_low, n1_low_start, n1_low_end, n1_low, sx, ix0_start, ix0_end, d_ix0;
Fixed ix1_start, ix1_end, d_ix1;
if (d_n0_up & 0x08000000)
{
d_n0_up |= 0xF0000000;
}
n0_up = n0_up_start;
n1_up_start = dotGridGradient(&cache[1][0], x1, y0, x, FIX(y0));
n1_up_end = dotGridGradient(&cache[1][0], x1, y0, x, FIX(y1));
d_n1_up = (n1_up_end - n1_up_start) >> 4;
if (d_n1_up & 0x08000000)
{
d_n1_up |= 0xF0000000;
}
n1_up = n1_up_start;
n0_low_start = dotGridGradient(&cache[0][1], x0, y1, x, FIX(y0));
n0_low_end = dotGridGradient(&cache[0][1], x0, y1, x, FIX(y1));
d_n0_low = (n0_low_end - n0_low_start) >> 4;
if (d_n0_low & 0x08000000)
{
d_n0_low |= 0xF0000000;
}
n0_low = n0_low_start;
n1_low_start = dotGridGradient(&cache[1][1], x1, y1, x, FIX(y0));
n1_low_end = dotGridGradient(&cache[1][1], x1, y1, x, FIX(y1));
d_n1_low = (n1_low_end - n1_low_start) >> 4;
if (d_n1_low & 0x08000000)
{
d_n1_low |= 0xF0000000;
}
n1_low = n1_low_start;
sx = x - FIX(x0);
ix0_start = interpolate(n0_up_start, n1_up_start, sx);
ix0_end = interpolate(n0_up_end, n1_up_end, sx);
ix0 = ix0_start;
d_ix0 = (ix0_end - ix0_start) >> 4;
if (d_ix0 & 0x80000000)
{
d_ix0 |= 0xF0000000;
}
ix1_start = interpolate(n0_low_start, n1_low_start, sx);
ix1_end = interpolate(n0_low_end, n1_low_end, sx);
ix1 = ix1_start;
d_ix1 = (ix1_end - ix1_start) >> 4;
if (d_ix1 & 0x80000000)
{
d_ix1 |= 0xF0000000;
}
for (y = FIX(y0); y < FIX(y1); y += dy)
{
if ((i + j) & 1)
{
Fixed sy = y - FIX(y0);
// n0 = dotGridGradient(&cache[0][0], x0, y0, x, y);
// n1 = dotGridGradient(&cache[1][0], x1, y0, x, y);
// ix0 = interpolate(n0, n1, sx);
// ix0 = interpolate(n0_up, n1_up, sx);
// n0 = dotGridGradient(&cache[0][1], x0, y1, x, y);
// n1 = dotGridGradient(&cache[1][1], x1, y1, x, y);
// ix1 = interpolate(n0_low, n1_low, sx);
Fixed value = (interpolate(ix0, ix1, sy) + ONE) >> 1;
if (value > 512)
{
draw_pixel(i, j);
draw_pixel(i + 1, j);
}
else if (value > 450)
{
draw_pixel(i, j);
}
}
j++;
n0_up += d_n0_up;
n1_up += d_n1_up;
n0_low += d_n0_low;
n1_low += d_n1_low;
ix0 += d_ix0;
ix1 += d_ix1;
}
// if (x0 == 3 && y0 == 2)
// {
// printf("------- end\n");
// }
}
}
}
}