foo
[gbajam22] / src / voxscape.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <math.h>
6 #include <assert.h>
7 #include "voxscape.h"
8 #include "debug.h"
9
10 /* hardcoded dimensions for the GBA */
11 #define FBWIDTH         240
12 #define FBHEIGHT        160
13 #define FBPITCH         240
14 /* map size */
15 #define XSZ                     512
16 #define YSZ                     512
17 #define XSHIFT          9
18 #define XMASK           0x1ff
19 #define YMASK           0x1ff
20 #define HSCALE          40
21
22
23 #define NO_LERP
24
25 #define XLERP(a, b, t, fp) \
26         ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
27
28 enum {
29         SLICELEN        = 1
30 };
31
32 struct voxscape {
33         int xsz, ysz;
34         unsigned char *height;
35         unsigned char *color;
36         int xshift, xmask, ymask;
37
38         int hfilt, cfilt;
39
40         /* framebuffer */
41         uint16_t *fb;
42         int fbwidth, fbheight;
43         int *coltop;
44         int horizon;
45
46         /* view */
47         int32_t x, y, angle;
48         int vheight;
49
50         /* projection */
51         int fov, znear, zfar;
52         int nslices;
53         int32_t *slicelen;
54         int proj_dist;
55
56         int zfog;       /* fog start Z (0: no fog) */
57         uint8_t fogcolor;
58
59         unsigned int valid;
60 };
61
62 int vox_quality = 1;
63 int *projlut;
64
65 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
66 {
67         struct voxscape *vox;
68
69         assert(xsz == XSZ && ysz == YSZ);
70
71         if(!(vox = calloc(1, sizeof *vox))) {
72                 return 0;
73         }
74         vox->height = himg;
75         vox->color = cimg;
76         vox->xsz = xsz;
77         vox->ysz = ysz;
78
79         vox->xmask = vox->xsz - 1;
80         vox->ymask = vox->ysz - 1;
81
82         vox->xshift = -1;
83         while(xsz) {
84                 xsz >>= 1;
85                 vox->xshift++;
86         }
87
88         vox->vheight = 80;
89         vox->proj_dist = 4;     /* TODO */
90
91         return vox;
92 }
93
94 void vox_free(struct voxscape *vox)
95 {
96         if(!vox) return;
97
98         free(vox->color);
99         free(vox->height);
100         free(vox->coltop);
101         free(vox->slicelen);
102         free(vox);
103 }
104
105 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
106 {
107         if(data) {
108                 memcpy(vox->color, data, vox->xsz * vox->ysz);
109         }
110         return vox->color;
111 }
112
113 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
114 {
115         if(data) {
116                 memcpy(vox->height, data, vox->xsz * vox->ysz);
117         }
118         return vox->height;
119 }
120
121 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
122 {
123         vox->zfog = zstart;
124         vox->fogcolor = color;
125 }
126
127 #define H(x, y) \
128         vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
129 #define C(x, y) \
130         vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
131
132 #ifdef NO_LERP
133 #define vox_height(vox, x, y)   H(x, y)
134 #define vox_color(vox, x, y)    C(x, y)
135
136 #else
137
138 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
139 {
140         int32_t u, v;
141         int h00, h01, h10, h11, h0, h1;
142
143         if(!vox->hfilt) {
144                 return H(x, y);
145         }
146
147         h00 = H(x, y);
148         h01 = H(x, y + 0x10000);
149         h10 = H(x + 0x10000, y);
150         h11 = H(x + 0x10000, y + 0x10000);
151
152         u = x & 0xffff;
153         v = y & 0xffff;
154
155         h0 = XLERP(h00, h01, v, 16);
156         h1 = XLERP(h10, h11, v, 16);
157         return XLERP(h0, h1, u, 16);
158 }
159
160 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
161 {
162         int32_t u, v;
163         int c00, c01, c10, c11, c0, c1;
164
165         if(!vox->cfilt) {
166                 return C(x, y);
167         }
168
169         c00 = C(x, y);
170         c01 = C(x, y + 0x10000);
171         c10 = C(x + 0x10000, y);
172         c11 = C(x + 0x10000, y + 0x10000);
173
174         u = x & 0xffff;
175         v = y & 0xffff;
176
177         c0 = XLERP(c00, c01, v, 16);
178         c1 = XLERP(c10, c11, v, 16);
179         return XLERP(c0, c1, u, 16);
180 }
181 #endif  /* !NO_LERP */
182
183
184 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
185 {
186         vox->hfilt = hfilt;
187         vox->cfilt = cfilt;
188 }
189
190 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
191 {
192         if(xres != vox->fbwidth) {
193                 if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
194                         panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
195                 }
196         }
197         vox->fb = fb;
198         vox->fbwidth = xres;
199         vox->fbheight = yres;
200         vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
201 }
202
203 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
204 {
205         if(h < 0) {
206                 h = vox_height(vox, x, y) - h;
207         }
208
209         vox->x = x;
210         vox->y = y;
211         vox->vheight = h;
212         vox->angle = angle;
213
214         vox->valid &= ~SLICELEN;
215 }
216
217 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
218 {
219         vox->fov = fov;
220         vox->znear = znear;
221         vox->zfar = zfar;
222
223         vox->nslices = vox->zfar - vox->znear;
224         if(!vox->slicelen) {
225                 if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
226                         panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
227                 }
228                 if(!(projlut = iwram_sbrk(vox->nslices * sizeof *projlut))) {
229                         panic(get_pc(), "vox_framebuf: failed to allocate projection table (%d)\n", vox->nslices);
230                 }
231         }
232
233         vox->valid &= ~SLICELEN;
234 }
235
236 /* algorithm:
237  * calculate extents of horizontal equidistant line from the viewer based on fov
238  * for each column step along this line and compute height for each pixel
239  * fill the visible (top) part of each column
240  */
241 ARM_IWRAM
242 void vox_render(struct voxscape *vox)
243 {
244         int i;
245
246         vox_begin(vox);
247
248         if(vox_quality) {
249                 for(i=0; i<vox->nslices; i++) {
250                         vox_render_slice(vox, i);
251                 }
252         } else {
253                 for(i=0; i<vox->nslices; i++) {
254                         if(i >= 10 && (i & 1) == 0) {
255                                 continue;
256                         }
257                         vox_render_slice(vox, i);
258                 }
259         }
260 }
261
262 ARM_IWRAM
263 void vox_begin(struct voxscape *vox)
264 {
265         int i;
266
267         memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
268
269         if(!(vox->valid & SLICELEN)) {
270                 float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
271                 for(i=0; i<vox->nslices; i++) {
272                         vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
273                         projlut[i] = (HSCALE << 8) / (vox->znear + i);
274                 }
275                 vox->valid |= SLICELEN;
276         }
277 }
278
279 ARM_IWRAM
280 void vox_render_slice(struct voxscape *vox, int n)
281 {
282         int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
283         int32_t x, y, len, xstep, ystep;
284         uint8_t color, last_col;
285         uint16_t *fbptr;
286         int proj;
287
288         z = vox->znear + n;
289
290         len = vox->slicelen[n] >> 8;
291         xstep = (((COS(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
292         ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
293
294         x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
295         y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
296
297         /*proj = (HSCALE << 8) / (vox->znear + n);*/
298
299         for(i=0; i<FBWIDTH/2; i++) {
300                 col = i << 1;
301                 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
302                 if(offs == last_offs) {
303                         hval = last_hval;
304                         color = last_col;
305                 } else {
306                         hval = vox->height[offs] - vox->vheight;
307                         hval = ((hval * projlut[n]) >> 8) + vox->horizon;
308                         if(hval > FBHEIGHT) hval = FBHEIGHT;
309                         color = vox->color[offs];
310                         last_offs = offs;
311                         last_hval = hval;
312                         last_col = color;
313                 }
314                 if(hval > vox->coltop[col]) {
315                         colstart = FBHEIGHT - hval;
316                         colheight = hval - vox->coltop[col];
317                         fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
318
319                         for(j=0; j<colheight; j++) {
320                                 *fbptr = color | ((uint16_t)color << 8);
321                                 fbptr += FBPITCH / 2;
322                         }
323                         vox->coltop[col] = hval;
324                 }
325                 x += xstep;
326                 y += ystep;
327         }
328 }
329
330 ARM_IWRAM
331 void vox_sky_solid(struct voxscape *vox, uint8_t color)
332 {
333         int i, j, colheight;
334         uint16_t *fbptr;
335
336         for(i=0; i<FBWIDTH / 2; i++) {
337                 fbptr = vox->fb + i;
338                 colheight = FBHEIGHT - vox->coltop[i << 1];
339
340                 for(j=0; j<colheight; j++) {
341                         *fbptr = color | ((uint16_t)color << 8);
342                         fbptr += FBPITCH / 2;
343                 }
344         }
345 }
346
347 ARM_IWRAM
348 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
349 {
350         int i, j, colheight, t;
351         int d = FBHEIGHT - vox->horizon;
352         uint8_t grad[FBHEIGHT];
353         uint16_t *fbptr;
354
355         for(i=0; i<d; i++) {
356                 t = (i << 16) / d;
357                 grad[i] = XLERP(ctop, chor, t, 16);
358         }
359         for(i=d; i<FBHEIGHT; i++) {
360                 grad[i] = chor;
361         }
362
363         for(i=0; i<FBWIDTH / 2; i++) {
364                 fbptr = vox->fb + i;
365                 colheight = FBHEIGHT - vox->coltop[i << 1];
366
367                 for(j=0; j<colheight; j++) {
368                         *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
369                         fbptr += FBPITCH / 2;
370                 }
371         }
372 }