foo
[gbajam22] / src / voxscape.c
index 462ed75..6eb5531 100644 (file)
@@ -7,6 +7,21 @@
 #include "voxscape.h"
 #include "debug.h"
 
+/* hardcoded dimensions for the GBA */
+#define FBWIDTH                240
+#define FBHEIGHT       160
+#define FBPITCH                240
+/* map size */
+#define XSZ                    512
+#define YSZ                    512
+#define XSHIFT         9
+#define XMASK          0x1ff
+#define YMASK          0x1ff
+#define HSCALE         40
+
+
+#define NO_LERP
+
 #define XLERP(a, b, t, fp) \
        ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
 
@@ -44,10 +59,15 @@ struct voxscape {
        unsigned int valid;
 };
 
+int vox_quality = 1;
+int *projlut;
+
 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
 {
        struct voxscape *vox;
 
+       assert(xsz == XSZ && ysz == YSZ);
+
        if(!(vox = calloc(1, sizeof *vox))) {
                return 0;
        }
@@ -105,10 +125,15 @@ void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
 }
 
 #define H(x, y)        \
-       vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
+       vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
 #define C(x, y) \
-       vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
+       vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
+
+#ifdef NO_LERP
+#define vox_height(vox, x, y)  H(x, y)
+#define vox_color(vox, x, y)   C(x, y)
 
+#else
 
 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
 {
@@ -153,6 +178,7 @@ int vox_color(struct voxscape *vox, int32_t x, int32_t y)
        c1 = XLERP(c10, c11, v, 16);
        return XLERP(c0, c1, u, 16);
 }
+#endif /* !NO_LERP */
 
 
 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
@@ -164,10 +190,8 @@ void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
 {
        if(xres != vox->fbwidth) {
-               free(vox->coltop);
-               if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
-                       fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
-                       return;
+               if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
+                       panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
                }
        }
        vox->fb = fb;
@@ -197,10 +221,13 @@ void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
        vox->zfar = zfar;
 
        vox->nslices = vox->zfar - vox->znear;
-       free(vox->slicelen);
-       if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
-               fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
-               return;
+       if(!vox->slicelen) {
+               if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
+                       panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
+               }
+               if(!(projlut = iwram_sbrk(vox->nslices * sizeof *projlut))) {
+                       panic(get_pc(), "vox_framebuf: failed to allocate projection table (%d)\n", vox->nslices);
+               }
        }
 
        vox->valid &= ~SLICELEN;
@@ -211,110 +238,135 @@ void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
  * for each column step along this line and compute height for each pixel
  * fill the visible (top) part of each column
  */
-
+ARM_IWRAM
 void vox_render(struct voxscape *vox)
 {
        int i;
 
        vox_begin(vox);
-       for(i=0; i<vox->nslices; i++) {
-               vox_render_slice(vox, i);
+
+       if(vox_quality) {
+               for(i=0; i<vox->nslices; i++) {
+                       vox_render_slice(vox, i);
+               }
+       } else {
+               for(i=0; i<vox->nslices; i++) {
+                       if(i >= 10 && (i & 1) == 0) {
+                               continue;
+                       }
+                       vox_render_slice(vox, i);
+               }
        }
 }
 
+ARM_IWRAM
 void vox_begin(struct voxscape *vox)
 {
        int i;
 
-       memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
+       memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
 
        if(!(vox->valid & SLICELEN)) {
                float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
                for(i=0; i<vox->nslices; i++) {
                        vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
+                       projlut[i] = (HSCALE << 8) / (vox->znear + i);
                }
                vox->valid |= SLICELEN;
        }
 }
 
+ARM_IWRAM
 void vox_render_slice(struct voxscape *vox, int n)
 {
-       int i, j, hval, colstart, colheight, z;
+       int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
        int32_t x, y, len, xstep, ystep;
-       uint8_t color;
+       uint8_t color, last_col;
        uint16_t *fbptr;
+       int proj;
 
        z = vox->znear + n;
 
        len = vox->slicelen[n] >> 8;
-       xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
-       ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
-
-       x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
-       y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
-       /* TODO double column */
-       for(i=0; i<vox->fbwidth/2; i++) {
-               hval = vox_height(vox, x, y) - vox->vheight;
-               hval = hval * 160 / (vox->znear + n) + vox->horizon;
-               if(hval > vox->fbheight) hval = vox->fbheight;
-               if(hval > vox->coltop[i]) {
-                       color = vox_color(vox, x, y);
-                       colstart = vox->fbheight - hval;
-                       colheight = hval - vox->coltop[i];
-                       fbptr = vox->fb + colstart * vox->fbwidth / 2 + i / 2;
+       xstep = (((COS(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
+       ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
+
+       x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
+       y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
+
+       /*proj = (HSCALE << 8) / (vox->znear + n);*/
+
+       for(i=0; i<FBWIDTH/2; i++) {
+               col = i << 1;
+               offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
+               if(offs == last_offs) {
+                       hval = last_hval;
+                       color = last_col;
+               } else {
+                       hval = vox->height[offs] - vox->vheight;
+                       hval = ((hval * projlut[n]) >> 8) + vox->horizon;
+                       if(hval > FBHEIGHT) hval = FBHEIGHT;
+                       color = vox->color[offs];
+                       last_offs = offs;
+                       last_hval = hval;
+                       last_col = color;
+               }
+               if(hval > vox->coltop[col]) {
+                       colstart = FBHEIGHT - hval;
+                       colheight = hval - vox->coltop[col];
+                       fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
 
                        for(j=0; j<colheight; j++) {
                                *fbptr = color | ((uint16_t)color << 8);
-                               fbptr += vox->fbwidth >> 1;
+                               fbptr += FBPITCH / 2;
                        }
-                       vox->coltop[i] = hval;
+                       vox->coltop[col] = hval;
                }
-
                x += xstep;
                y += ystep;
        }
 }
 
+ARM_IWRAM
 void vox_sky_solid(struct voxscape *vox, uint8_t color)
 {
        int i, j, colheight;
        uint16_t *fbptr;
 
-       /* TODO double columns */
-       for(i=0; i<vox->fbwidth/2; i++) {
+       for(i=0; i<FBWIDTH / 2; i++) {
                fbptr = vox->fb + i;
-               colheight = vox->fbheight - vox->coltop[i];
+               colheight = FBHEIGHT - vox->coltop[i << 1];
+
                for(j=0; j<colheight; j++) {
                        *fbptr = color | ((uint16_t)color << 8);
-                       fbptr += vox->fbwidth >> 1;
+                       fbptr += FBPITCH / 2;
                }
        }
 }
 
+ARM_IWRAM
 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
 {
        int i, j, colheight, t;
-       int d = vox->fbheight - vox->horizon;
-       uint8_t *grad;
+       int d = FBHEIGHT - vox->horizon;
+       uint8_t grad[FBHEIGHT];
        uint16_t *fbptr;
 
-       grad = alloca(vox->fbheight * sizeof *grad);
-
        for(i=0; i<d; i++) {
-               t = (i << 8) / d;
-               grad[i] = XLERP(ctop, chor, t, 8);
+               t = (i << 16) / d;
+               grad[i] = XLERP(ctop, chor, t, 16);
        }
-       for(i=d; i<vox->fbheight; i++) {
+       for(i=d; i<FBHEIGHT; i++) {
                grad[i] = chor;
        }
 
-       /* TODO double columns */
-       for(i=0; i<vox->fbwidth/2; i++) {
-               fbptr = vox->fb + i / 2;
-               colheight = vox->fbheight - vox->coltop[i];
+       for(i=0; i<FBWIDTH / 2; i++) {
+               fbptr = vox->fb + i;
+               colheight = FBHEIGHT - vox->coltop[i << 1];
+
                for(j=0; j<colheight; j++) {
                        *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
-                       fbptr += vox->fbwidth >> 1;
+                       fbptr += FBPITCH / 2;
                }
        }
 }