X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fvoxscape.c;h=5f337c42ffcd198469150e1e598cfc0d484c4fba;hb=09ff9fd9a87d495d8c2d55203af89d0f651b5d3a;hp=b57cad4bab794470376750086c301e6d369743d5;hpb=d489941c3101412810a6f355e35e027c7d51b350;p=gbajam22 diff --git a/src/voxscape.c b/src/voxscape.c index b57cad4..5f337c4 100644 --- a/src/voxscape.c +++ b/src/voxscape.c @@ -6,6 +6,7 @@ #include #include "voxscape.h" #include "debug.h" +#include "data.h" /* hardcoded dimensions for the GBA */ #define FBWIDTH 240 @@ -19,6 +20,8 @@ #define YMASK 0x1ff #define HSCALE 40 +/* XXX */ +#define OBJ_STRIDE_SHIFT 5 #define NO_LERP @@ -29,203 +32,104 @@ enum { SLICELEN = 1 }; -struct voxscape { - int xsz, ysz; - unsigned char *height; - unsigned char *color; - int xshift, xmask, ymask; +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; - int hfilt, cfilt; +static unsigned int vox_valid; - /* framebuffer */ - uint16_t *fb; - int fbwidth, fbheight; - int *coltop; - int horizon; +static struct vox_object *vox_obj; +static int vox_num_obj, vox_obj_stride; - /* view */ - int32_t x, y, angle; - int vheight; +int *projlut; - /* projection */ - int fov, znear, zfar; - int nslices; - int32_t *slicelen; - int proj_dist; - - int zfog; /* fog start Z (0: no fog) */ - uint8_t fogcolor; - - 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->vheight = 80; - vox->proj_dist = 4; /* TODO */ + vox_hmap = himg; + vox_color = cimg; - return vox; -} - -void vox_free(struct voxscape *vox) -{ - if(!vox) return; + 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; - free(vox->color); - free(vox->height); - free(vox->coltop); - free(vox->slicelen); - free(vox); -} + vox_vheight = 80; -uint8_t *vox_texture(struct voxscape *vox, uint8_t *data) -{ - if(data) { - memcpy(vox->color, data, vox->xsz * vox->ysz); - } - return vox->color; + return 0; } -uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data) +void vox_destroy(void) { - if(data) { - memcpy(vox->height, data, vox->xsz * vox->ysz); - } - return vox->height; -} - -void vox_fog(struct voxscape *vox, int zstart, uint8_t color) -{ - 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) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)] + vox_hmap[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)] #define C(x, y) \ - 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 + vox_color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)] -int vox_height(struct voxscape *vox, int32_t x, int32_t y) +void vox_framebuf(int xres, int yres, void *fb, int horizon) { - 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); - - u = x & 0xffff; - v = y & 0xffff; - - c0 = XLERP(c00, c01, v, 16); - 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) -{ - vox->hfilt = hfilt; - vox->cfilt = cfilt; -} - -void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon) -{ - if(xres != vox->fbwidth) { - if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) { + 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); - return; } } - 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 = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) { - panic(get_pc(), "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: @@ -234,51 +138,54 @@ void vox_proj(struct voxscape *vox, int fov, int znear, int zfar) * fill the visible (top) part of each column */ ARM_IWRAM -void vox_render(struct voxscape *vox) +void vox_render(void) { int i; - vox_begin(vox); - for(i=0; inslices; i++) { - /*if(i >= 10 && (i & 1) == 0) { - continue; - }*/ - vox_render_slice(vox, i); + vox_begin(); + + for(i=0; icoltop, 0, 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; } } ARM_IWRAM -void vox_render_slice(struct voxscape *vox, int n) +void vox_render_slice(int n) { int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1; int32_t x, y, len, xstep, ystep; uint8_t color, last_col; uint16_t *fbptr; + /*int proj;*/ + struct vox_object *obj; + + z = vox_znear + n; - 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); - 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); - 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; iheight[offs] - vox->vheight; - hval = hval * HSCALE / (vox->znear + n) + vox->horizon; + hval = vox_hmap[offs] - vox_vheight; + hval = ((hval * projlut[n]) >> 8) + vox_horizon; if(hval > FBHEIGHT) hval = FBHEIGHT; - color = vox->color[offs]; + color = vox_color[offs]; last_offs = offs; last_hval = hval; last_col = color; } - if(hval > vox->coltop[col]) { + if(hval >= vox_coltop[col]) { colstart = FBHEIGHT - hval; - colheight = hval - vox->coltop[col]; - fbptr = vox->fb + colstart * (FBPITCH / 2) + i; + colheight = hval - vox_coltop[col]; + fbptr = vox_fb + colstart * (FBPITCH / 2) + i; for(j=0; jcoltop[col] = hval; + 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]; + } } x += xstep; y += ystep; @@ -312,14 +228,14 @@ void vox_render_slice(struct voxscape *vox, int n) } ARM_IWRAM -void vox_sky_solid(struct voxscape *vox, uint8_t color) +void vox_sky_solid(uint8_t color) { int i, j, colheight; uint16_t *fbptr; for(i=0; ifb + i; - colheight = FBHEIGHT - vox->coltop[i << 1]; + fbptr = vox_fb + i; + colheight = FBHEIGHT - vox_coltop[i << 1]; for(j=0; jhorizon; + int d = FBHEIGHT - vox_horizon; uint8_t grad[FBHEIGHT]; uint16_t *fbptr; @@ -345,8 +261,8 @@ void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop) } for(i=0; ifb + i; - colheight = FBHEIGHT - vox->coltop[i << 1]; + fbptr = vox_fb + i; + colheight = FBHEIGHT - vox_coltop[i << 1]; for(j=0; joffs = 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; +}