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