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