X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fvoxscape.c;h=5f337c42ffcd198469150e1e598cfc0d484c4fba;hb=09ff9fd9a87d495d8c2d55203af89d0f651b5d3a;hp=462ed75d06560d8cdd8c18a5a88ab72120bbf0db;hpb=a4b392b15da0d7da367b1121ec5949f26e10628a;p=gbajam22 diff --git a/src/voxscape.c b/src/voxscape.c index 462ed75..5f337c4 100644 --- a/src/voxscape.c +++ b/src/voxscape.c @@ -6,6 +6,24 @@ #include #include "voxscape.h" #include "debug.h" +#include "data.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 + +/* XXX */ +#define OBJ_STRIDE_SHIFT 5 + +#define NO_LERP #define XLERP(a, b, t, fp) \ ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp) @@ -14,196 +32,104 @@ enum { SLICELEN = 1 }; -struct voxscape { - int xsz, ysz; - unsigned char *height; - unsigned char *color; - int xshift, xmask, ymask; - - int hfilt, cfilt; - - /* framebuffer */ - uint16_t *fb; - int fbwidth, fbheight; - int *coltop; - int horizon; +static unsigned char *vox_hmap; +static unsigned char *vox_color; +/* framebuffer */ +static uint16_t *vox_fb; +static int *vox_coltop; +static int vox_horizon; +/* view */ +static int32_t vox_x, vox_y, vox_angle; +static int vox_vheight; +/* projection */ +static int vox_fov, vox_znear, vox_zfar; +static int vox_nslices; +static int32_t *vox_slicelen; - /* view */ - int32_t x, y, angle; - int vheight; +static unsigned int vox_valid; - /* projection */ - int fov, znear, zfar; - int nslices; - int32_t *slicelen; - int proj_dist; +static struct vox_object *vox_obj; +static int vox_num_obj, vox_obj_stride; - int zfog; /* fog start Z (0: no fog) */ - uint8_t fogcolor; +int *projlut; - unsigned int valid; -}; - -struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg) +int vox_init(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; - } - vox->height = himg; - vox->color = cimg; - vox->xsz = xsz; - vox->ysz = ysz; - - vox->xmask = vox->xsz - 1; - vox->ymask = vox->ysz - 1; - - vox->xshift = -1; - while(xsz) { - xsz >>= 1; - vox->xshift++; - } + vox_hmap = himg; + vox_color = cimg; - vox->vheight = 80; - vox->proj_dist = 4; /* TODO */ + vox_fb = 0; + vox_coltop = 0; + vox_horizon = 0; + vox_x = vox_y = vox_angle = 0; + vox_fov = 0; + vox_znear = vox_zfar = 0; + vox_nslices = 0; + vox_slicelen = 0; + vox_valid = 0; + projlut = 0; - return vox; -} - -void vox_free(struct voxscape *vox) -{ - if(!vox) return; - - free(vox->color); - free(vox->height); - free(vox->coltop); - free(vox->slicelen); - free(vox); -} - -uint8_t *vox_texture(struct voxscape *vox, uint8_t *data) -{ - if(data) { - memcpy(vox->color, data, vox->xsz * vox->ysz); - } - return vox->color; -} + vox_vheight = 80; -uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data) -{ - if(data) { - memcpy(vox->height, data, vox->xsz * vox->ysz); - } - return vox->height; + return 0; } -void vox_fog(struct voxscape *vox, int zstart, uint8_t color) +void vox_destroy(void) { - vox->zfog = zstart; - vox->fogcolor = color; + /* XXX we rely on the screen to clear up any allocated IWRAM */ } #define H(x, y) \ - vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)] + vox_hmap[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)] #define C(x, y) \ - vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)] - - -int vox_height(struct voxscape *vox, int32_t x, int32_t y) -{ - int32_t u, v; - int h00, h01, h10, h11, h0, h1; - - if(!vox->hfilt) { - return H(x, y); - } - - h00 = H(x, y); - h01 = H(x, y + 0x10000); - h10 = H(x + 0x10000, y); - h11 = H(x + 0x10000, y + 0x10000); - - u = x & 0xffff; - v = y & 0xffff; - - h0 = XLERP(h00, h01, v, 16); - h1 = XLERP(h10, h11, v, 16); - return XLERP(h0, h1, u, 16); -} - -int vox_color(struct voxscape *vox, int32_t x, int32_t y) -{ - int32_t u, v; - int c00, c01, c10, c11, c0, c1; - - if(!vox->cfilt) { - return C(x, y); - } - - c00 = C(x, y); - c01 = C(x, y + 0x10000); - c10 = C(x + 0x10000, y); - c11 = C(x + 0x10000, y + 0x10000); + vox_color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)] - u = x & 0xffff; - v = y & 0xffff; - - c0 = XLERP(c00, c01, v, 16); - c1 = XLERP(c10, c11, v, 16); - return XLERP(c0, c1, u, 16); -} - - -void vox_filter(struct voxscape *vox, int hfilt, int cfilt) +void vox_framebuf(int xres, int yres, void *fb, int horizon) { - vox->hfilt = hfilt; - vox->cfilt = 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) { + 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; - vox->fbwidth = xres; - vox->fbheight = yres; - vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1); + vox_fb = fb; + vox_horizon = horizon >= 0 ? horizon : (FBHEIGHT >> 1); } -void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle) +int vox_view(int32_t x, int32_t y, int h, int32_t angle) { if(h < 0) { - h = vox_height(vox, x, y) - h; + h = H(x, y) - h; } - vox->x = x; - vox->y = y; - vox->vheight = h; - vox->angle = angle; + vox_x = x; + vox_y = y; + vox_vheight = h; + vox_angle = angle; - vox->valid &= ~SLICELEN; + vox_valid &= ~SLICELEN; + + return h; } -void vox_proj(struct voxscape *vox, int fov, int znear, int zfar) +void vox_proj(int fov, int znear, int zfar) { - vox->fov = fov; - vox->znear = znear; - 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; + vox_fov = fov; + vox_znear = znear; + vox_zfar = zfar; + + vox_nslices = vox_zfar - vox_znear; + 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; + vox_valid &= ~SLICELEN; } /* algorithm: @@ -211,110 +137,168 @@ 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 */ - -void vox_render(struct voxscape *vox) +ARM_IWRAM +void vox_render(void) { int i; - vox_begin(vox); - for(i=0; inslices; i++) { - vox_render_slice(vox, i); + vox_begin(); + + for(i=0; icoltop, 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; inslices; i++) { - vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f); + if(!(vox_valid & SLICELEN)) { + float theta = (float)vox_fov * M_PI / 360.0f; /* half angle */ + for(i=0; ivalid |= SLICELEN; + vox_valid |= SLICELEN; } } -void vox_render_slice(struct voxscape *vox, int n) +ARM_IWRAM +void vox_render_slice(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; - - 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; ifbwidth/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; + /*int proj;*/ + struct vox_object *obj; + + z = vox_znear + n; + + len = vox_slicelen[n] >> 8; + 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> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK); + if(offs == last_offs) { + hval = last_hval; + color = last_col; + } else { + hval = vox_hmap[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; jfbwidth >> 1; + fbptr += FBPITCH / 2; + } + vox_coltop[col] = hval; + + /* check to see if there's an object here */ + if(color >= CMAP_SPAWN0) { + int idx = color - CMAP_SPAWN0; + obj = (struct vox_object*)((char*)vox_obj + (idx << OBJ_STRIDE_SHIFT)); + obj->px = col; + obj->py = colstart; + obj->scale = projlut[n]; } - vox->coltop[i] = hval; } - x += xstep; y += ystep; } } -void vox_sky_solid(struct voxscape *vox, uint8_t color) +ARM_IWRAM +void vox_sky_solid(uint8_t color) { int i, j, colheight; uint16_t *fbptr; - /* TODO double columns */ - for(i=0; ifbwidth/2; i++) { - fbptr = vox->fb + i; - colheight = vox->fbheight - vox->coltop[i]; + for(i=0; ifbwidth >> 1; + fbptr += FBPITCH / 2; } } } -void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop) +ARM_IWRAM +void vox_sky_grad(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; ifbheight; i++) { + for(i=d; ifbwidth/2; i++) { - fbptr = vox->fb + i / 2; - colheight = vox->fbheight - vox->coltop[i]; + for(i=0; ifbwidth >> 1; + fbptr += FBPITCH / 2; } } } + +void vox_objects(struct vox_object *ptr, int count, int stride) +{ + int i; + struct vox_object *obj; + + if(stride != 1 << OBJ_STRIDE_SHIFT) { + panic(get_pc(), "optimization requires %d byte vox obj (got %d)", + 1 << OBJ_STRIDE_SHIFT, stride); + } + + vox_obj = ptr; + vox_num_obj = count; + vox_obj_stride = stride; + + obj = ptr; + for(i=0; ioffs = obj->y * XSZ + obj->x; + obj = (struct vox_object*)((char*)obj + stride); + } +} + +int vox_height(int x, int y) +{ + return H(x, y); +} + +int vox_check_vis(int32_t x0, int32_t y0, int32_t x1, int32_t y1) +{ + /* TODO */ + return 0; +}