X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fvoxscape.c;h=5f337c42ffcd198469150e1e598cfc0d484c4fba;hb=09ff9fd9a87d495d8c2d55203af89d0f651b5d3a;hp=ef11974b10ac7e2c3171e1c285c47403258694b8;hpb=d98ff7c2912943650e17a4c6caf94998f0b0640b;p=gbajam22 diff --git a/src/voxscape.c b/src/voxscape.c index ef11974..5f337c4 100644 --- a/src/voxscape.c +++ b/src/voxscape.c @@ -6,9 +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) @@ -17,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; - - /* view */ - int32_t x, y, angle; - int vheight; +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; - /* projection */ - int fov, znear, zfar; - int nslices; - int32_t *slicelen; - int proj_dist; +static unsigned int vox_valid; - int zfog; /* fog start Z (0: no fog) */ - uint8_t fogcolor; +static struct vox_object *vox_obj; +static int vox_num_obj, vox_obj_stride; - unsigned int valid; -}; +int *projlut; -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; - - 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 */ + assert(xsz == XSZ && ysz == YSZ); - return vox; -} + vox_hmap = himg; + vox_color = cimg; -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); -} - -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)] - + 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); -} - - -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) { - 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: @@ -214,124 +137,118 @@ 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; ifb, 0, FBWIDTH * FBHEIGHT); - memset(vox->coltop, 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; } } -void vox_render_slice(struct voxscape *vox, int n) +ARM_IWRAM +void vox_render_slice(int n) { - int i, j, hval, colstart, colheight, col, 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;*/ + 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; - ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / FBWIDTH; + 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 / 2); - y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 2); + x = vox_x - SIN(vox_angle) * z - xstep * (FBWIDTH / 4); + y = vox_y + COS(vox_angle) * z - ystep * (FBWIDTH / 4); - for(i=0; ivheight; - hval = hval * 40 / (vox->znear + n) + vox->horizon; - if(hval > FBHEIGHT) hval = FBHEIGHT; - if(hval > vox->coltop[col]) { - color = vox_color(vox, x, y); - colstart = FBHEIGHT - hval; - colheight = hval - vox->coltop[col]; - fbptr = vox->fb + colstart * (FBWIDTH / 2) + i; + /*proj = (HSCALE << 8) / (vox_znear + n);*/ - for(j=0; jcoltop[col] = hval; + 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; } - x += xstep; - y += ystep; - - col++; - hval = vox_height(vox, x, y) - vox->vheight; - hval = hval * 40 / (vox->znear + n) + vox->horizon; - if(hval > FBHEIGHT) hval = FBHEIGHT; - if(hval > vox->coltop[col]) { - color = vox_color(vox, x, y); + if(hval >= vox_coltop[col]) { colstart = FBHEIGHT - hval; - colheight = hval - vox->coltop[col]; - fbptr = vox->fb + colstart * (FBWIDTH / 2) + i; + colheight = hval - vox_coltop[col]; + fbptr = vox_fb + colstart * (FBPITCH / 2) + i; for(j=0; j= 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[col] = 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, colh0, colh1, colhboth; + int i, j, colheight; uint16_t *fbptr; for(i=0; ifb + i; - colh0 = FBHEIGHT - vox->coltop[i << 1]; - colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1]; - colhboth = colh0 < colh1 ? colh0 : colh1; + fbptr = vox_fb + i; + colheight = FBHEIGHT - vox_coltop[i << 1]; - for(j=0; j colh1) { - for(j=colhboth; jhorizon; + int i, j, colheight, t; + int d = FBHEIGHT - vox_horizon; uint8_t grad[FBHEIGHT]; uint16_t *fbptr; @@ -344,26 +261,44 @@ void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop) } for(i=0; ifb + i; - colh0 = FBHEIGHT - vox->coltop[i << 1]; - colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1]; - colhboth = colh0 < colh1 ? colh0 : colh1; + fbptr = vox_fb + i; + colheight = FBHEIGHT - vox_coltop[i << 1]; - for(j=0; j colh1) { - for(j=colhboth; 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; }