10 /* hardcoded dimensions for the GBA */
25 #define XLERP(a, b, t, fp) \
26 ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
34 unsigned char *height;
36 int xshift, xmask, ymask;
42 int fbwidth, fbheight;
56 int zfog; /* fog start Z (0: no fog) */
62 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
66 assert(xsz == XSZ && ysz == YSZ);
68 if(!(vox = calloc(1, sizeof *vox))) {
76 vox->xmask = vox->xsz - 1;
77 vox->ymask = vox->ysz - 1;
86 vox->proj_dist = 4; /* TODO */
91 void vox_free(struct voxscape *vox)
102 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
105 memcpy(vox->color, data, vox->xsz * vox->ysz);
110 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
113 memcpy(vox->height, data, vox->xsz * vox->ysz);
118 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
121 vox->fogcolor = color;
125 vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
127 vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
130 #define vox_height(vox, x, y) H(x, y)
131 #define vox_color(vox, x, y) C(x, y)
135 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
138 int h00, h01, h10, h11, h0, h1;
145 h01 = H(x, y + 0x10000);
146 h10 = H(x + 0x10000, y);
147 h11 = H(x + 0x10000, y + 0x10000);
152 h0 = XLERP(h00, h01, v, 16);
153 h1 = XLERP(h10, h11, v, 16);
154 return XLERP(h0, h1, u, 16);
157 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
160 int c00, c01, c10, c11, c0, c1;
167 c01 = C(x, y + 0x10000);
168 c10 = C(x + 0x10000, y);
169 c11 = C(x + 0x10000, y + 0x10000);
174 c0 = XLERP(c00, c01, v, 16);
175 c1 = XLERP(c10, c11, v, 16);
176 return XLERP(c0, c1, u, 16);
178 #endif /* !NO_LERP */
181 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
187 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
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);
197 vox->fbheight = yres;
198 vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
201 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
204 h = vox_height(vox, x, y) - h;
212 vox->valid &= ~SLICELEN;
215 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
221 vox->nslices = vox->zfar - vox->znear;
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);
228 vox->valid &= ~SLICELEN;
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
237 void vox_render(struct voxscape *vox)
242 for(i=0; i<vox->nslices; i++) {
243 /*if(i >= 10 && (i & 1) == 0) {
246 vox_render_slice(vox, i);
251 void vox_begin(struct voxscape *vox)
255 memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
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);
262 vox->valid |= SLICELEN;
267 void vox_render_slice(struct voxscape *vox, int n)
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;
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);
280 x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
281 y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
283 for(i=0; i<FBWIDTH/2; i++) {
285 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
286 if(offs == last_offs) {
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];
298 if(hval > vox->coltop[col]) {
299 colstart = FBHEIGHT - hval;
300 colheight = hval - vox->coltop[col];
301 fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
303 for(j=0; j<colheight; j++) {
304 *fbptr = color | ((uint16_t)color << 8);
305 fbptr += FBPITCH / 2;
307 vox->coltop[col] = hval;
315 void vox_sky_solid(struct voxscape *vox, uint8_t color)
320 for(i=0; i<FBWIDTH / 2; i++) {
322 colheight = FBHEIGHT - vox->coltop[i << 1];
324 for(j=0; j<colheight; j++) {
325 *fbptr = color | ((uint16_t)color << 8);
326 fbptr += FBPITCH / 2;
332 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
334 int i, j, colheight, t;
335 int d = FBHEIGHT - vox->horizon;
336 uint8_t grad[FBHEIGHT];
341 grad[i] = XLERP(ctop, chor, t, 16);
343 for(i=d; i<FBHEIGHT; i++) {
347 for(i=0; i<FBWIDTH / 2; i++) {
349 colheight = FBHEIGHT - vox->coltop[i << 1];
351 for(j=0; j<colheight; j++) {
352 *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
353 fbptr += FBPITCH / 2;