better input, button for quality selection
[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
64 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
65 {
66         struct voxscape *vox;
67
68         assert(xsz == XSZ && ysz == YSZ);
69
70         if(!(vox = calloc(1, sizeof *vox))) {
71                 return 0;
72         }
73         vox->height = himg;
74         vox->color = cimg;
75         vox->xsz = xsz;
76         vox->ysz = ysz;
77
78         vox->xmask = vox->xsz - 1;
79         vox->ymask = vox->ysz - 1;
80
81         vox->xshift = -1;
82         while(xsz) {
83                 xsz >>= 1;
84                 vox->xshift++;
85         }
86
87         vox->vheight = 80;
88         vox->proj_dist = 4;     /* TODO */
89
90         return vox;
91 }
92
93 void vox_free(struct voxscape *vox)
94 {
95         if(!vox) return;
96
97         free(vox->color);
98         free(vox->height);
99         free(vox->coltop);
100         free(vox->slicelen);
101         free(vox);
102 }
103
104 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
105 {
106         if(data) {
107                 memcpy(vox->color, data, vox->xsz * vox->ysz);
108         }
109         return vox->color;
110 }
111
112 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
113 {
114         if(data) {
115                 memcpy(vox->height, data, vox->xsz * vox->ysz);
116         }
117         return vox->height;
118 }
119
120 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
121 {
122         vox->zfog = zstart;
123         vox->fogcolor = color;
124 }
125
126 #define H(x, y) \
127         vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
128 #define C(x, y) \
129         vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
130
131 #ifdef NO_LERP
132 #define vox_height(vox, x, y)   H(x, y)
133 #define vox_color(vox, x, y)    C(x, y)
134
135 #else
136
137 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
138 {
139         int32_t u, v;
140         int h00, h01, h10, h11, h0, h1;
141
142         if(!vox->hfilt) {
143                 return H(x, y);
144         }
145
146         h00 = H(x, y);
147         h01 = H(x, y + 0x10000);
148         h10 = H(x + 0x10000, y);
149         h11 = H(x + 0x10000, y + 0x10000);
150
151         u = x & 0xffff;
152         v = y & 0xffff;
153
154         h0 = XLERP(h00, h01, v, 16);
155         h1 = XLERP(h10, h11, v, 16);
156         return XLERP(h0, h1, u, 16);
157 }
158
159 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
160 {
161         int32_t u, v;
162         int c00, c01, c10, c11, c0, c1;
163
164         if(!vox->cfilt) {
165                 return C(x, y);
166         }
167
168         c00 = C(x, y);
169         c01 = C(x, y + 0x10000);
170         c10 = C(x + 0x10000, y);
171         c11 = C(x + 0x10000, y + 0x10000);
172
173         u = x & 0xffff;
174         v = y & 0xffff;
175
176         c0 = XLERP(c00, c01, v, 16);
177         c1 = XLERP(c10, c11, v, 16);
178         return XLERP(c0, c1, u, 16);
179 }
180 #endif  /* !NO_LERP */
181
182
183 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
184 {
185         vox->hfilt = hfilt;
186         vox->cfilt = cfilt;
187 }
188
189 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
190 {
191         if(xres != vox->fbwidth) {
192                 if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
193                         panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
194                         return;
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                         return;
228                 }
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 ARM_IWRAM
240 void vox_render(struct voxscape *vox)
241 {
242         int i;
243
244         vox_begin(vox);
245
246         if(vox_quality) {
247                 for(i=0; i<vox->nslices; i++) {
248                         vox_render_slice(vox, i);
249                 }
250         } else {
251                 for(i=0; i<vox->nslices; i++) {
252                         if(i >= 10 && (i & 1) == 0) {
253                                 continue;
254                         }
255                         vox_render_slice(vox, i);
256                 }
257         }
258 }
259
260 ARM_IWRAM
261 void vox_begin(struct voxscape *vox)
262 {
263         int i;
264
265         memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
266
267         if(!(vox->valid & SLICELEN)) {
268                 float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
269                 for(i=0; i<vox->nslices; i++) {
270                         vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
271                 }
272                 vox->valid |= SLICELEN;
273         }
274 }
275
276 ARM_IWRAM
277 void vox_render_slice(struct voxscape *vox, int n)
278 {
279         int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
280         int32_t x, y, len, xstep, ystep;
281         uint8_t color, last_col;
282         uint16_t *fbptr;
283
284         z = vox->znear + n;
285
286         len = vox->slicelen[n] >> 8;
287         xstep = (((COS(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
288         ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
289
290         x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
291         y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
292
293         for(i=0; i<FBWIDTH/2; i++) {
294                 col = i << 1;
295                 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
296                 if(offs == last_offs) {
297                         hval = last_hval;
298                         color = last_col;
299                 } else {
300                         hval = vox->height[offs] - vox->vheight;
301                         hval = hval * HSCALE / (vox->znear + n) + vox->horizon;
302                         if(hval > FBHEIGHT) hval = FBHEIGHT;
303                         color = vox->color[offs];
304                         last_offs = offs;
305                         last_hval = hval;
306                         last_col = color;
307                 }
308                 if(hval > vox->coltop[col]) {
309                         colstart = FBHEIGHT - hval;
310                         colheight = hval - vox->coltop[col];
311                         fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
312
313                         for(j=0; j<colheight; j++) {
314                                 *fbptr = color | ((uint16_t)color << 8);
315                                 fbptr += FBPITCH / 2;
316                         }
317                         vox->coltop[col] = hval;
318                 }
319                 x += xstep;
320                 y += ystep;
321         }
322 }
323
324 ARM_IWRAM
325 void vox_sky_solid(struct voxscape *vox, uint8_t color)
326 {
327         int i, j, colheight;
328         uint16_t *fbptr;
329
330         for(i=0; i<FBWIDTH / 2; i++) {
331                 fbptr = vox->fb + i;
332                 colheight = FBHEIGHT - vox->coltop[i << 1];
333
334                 for(j=0; j<colheight; j++) {
335                         *fbptr = color | ((uint16_t)color << 8);
336                         fbptr += FBPITCH / 2;
337                 }
338         }
339 }
340
341 ARM_IWRAM
342 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
343 {
344         int i, j, colheight, t;
345         int d = FBHEIGHT - vox->horizon;
346         uint8_t grad[FBHEIGHT];
347         uint16_t *fbptr;
348
349         for(i=0; i<d; i++) {
350                 t = (i << 16) / d;
351                 grad[i] = XLERP(ctop, chor, t, 16);
352         }
353         for(i=d; i<FBHEIGHT; i++) {
354                 grad[i] = chor;
355         }
356
357         for(i=0; i<FBWIDTH / 2; i++) {
358                 fbptr = vox->fb + i;
359                 colheight = FBHEIGHT - vox->coltop[i << 1];
360
361                 for(j=0; j<colheight; j++) {
362                         *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
363                         fbptr += FBPITCH / 2;
364                 }
365         }
366 }