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) */
66 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
70 assert(xsz == XSZ && ysz == YSZ);
72 if(!(vox = calloc(1, sizeof *vox))) {
80 vox->xmask = vox->xsz - 1;
81 vox->ymask = vox->ysz - 1;
90 vox->proj_dist = 4; /* TODO */
95 void vox_free(struct voxscape *vox)
106 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
109 memcpy(vox->color, data, vox->xsz * vox->ysz);
114 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
117 memcpy(vox->height, data, vox->xsz * vox->ysz);
122 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
125 vox->fogcolor = color;
129 vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
131 vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
134 #define vox_height(vox, x, y) H(x, y)
135 #define vox_color(vox, x, y) C(x, y)
139 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
142 int h00, h01, h10, h11, h0, h1;
149 h01 = H(x, y + 0x10000);
150 h10 = H(x + 0x10000, y);
151 h11 = H(x + 0x10000, y + 0x10000);
156 h0 = XLERP(h00, h01, v, 16);
157 h1 = XLERP(h10, h11, v, 16);
158 return XLERP(h0, h1, u, 16);
161 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
164 int c00, c01, c10, c11, c0, c1;
171 c01 = C(x, y + 0x10000);
172 c10 = C(x + 0x10000, y);
173 c11 = C(x + 0x10000, y + 0x10000);
178 c0 = XLERP(c00, c01, v, 16);
179 c1 = XLERP(c10, c11, v, 16);
180 return XLERP(c0, c1, u, 16);
182 #endif /* !NO_LERP */
185 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
191 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
193 if(xres != vox->fbwidth) {
194 if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
195 panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
201 vox->fbheight = yres;
202 vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
205 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
208 h = vox_height(vox, x, y) - h;
216 vox->valid &= ~SLICELEN;
219 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
225 vox->nslices = vox->zfar - vox->znear;
227 if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
228 panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
230 if(!(projtab = iwram_sbrk(vox->nslices * sizeof *projtab))) {
231 panic(get_pc(), "vox_proj: failed to allocate projection table (%d)\n", vox->nslices);
235 vox->valid &= ~SLICELEN;
239 * calculate extents of horizontal equidistant line from the viewer based on fov
240 * for each column step along this line and compute height for each pixel
241 * fill the visible (top) part of each column
244 void vox_render(struct voxscape *vox)
251 for(i=0; i<vox->nslices; i++) {
252 vox_render_slice(vox, i);
255 for(i=0; i<vox->nslices; i++) {
256 if(i >= 10 && (i & 1) == 0) {
259 vox_render_slice(vox, i);
265 void vox_begin(struct voxscape *vox)
269 memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
271 if(!(vox->valid & SLICELEN)) {
272 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
273 for(i=0; i<vox->nslices; i++) {
274 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
275 projtab[i] = (HSCALE << 8) / (vox->znear + i);
277 vox->valid |= SLICELEN;
282 void vox_render_slice(struct voxscape *vox, int n)
284 int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
285 int32_t x, y, len, xstep, ystep;
286 uint8_t color, last_col;
291 len = vox->slicelen[n] >> 8;
292 xstep = (((COS(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
293 ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
295 x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
296 y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
298 for(i=0; i<FBWIDTH/2; i++) {
300 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
301 if(offs == last_offs) {
305 hval = vox->height[offs] - vox->vheight;
306 /*hval = hval * HSCALE / (vox->znear + n) + vox->horizon;*/
307 hval = ((hval * projtab[n]) >> 8) + vox->horizon;
308 if(hval > FBHEIGHT) hval = FBHEIGHT;
309 color = vox->color[offs];
314 if(hval > vox->coltop[col]) {
315 colstart = FBHEIGHT - hval;
316 colheight = hval - vox->coltop[col];
317 fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
319 for(j=0; j<colheight; j++) {
320 *fbptr = color | ((uint16_t)color << 8);
321 fbptr += FBPITCH / 2;
323 vox->coltop[col] = hval;
331 void vox_sky_solid(struct voxscape *vox, uint8_t color)
336 for(i=0; i<FBWIDTH / 2; i++) {
338 colheight = FBHEIGHT - vox->coltop[i << 1];
340 for(j=0; j<colheight; j++) {
341 *fbptr = color | ((uint16_t)color << 8);
342 fbptr += FBPITCH / 2;
348 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
350 int i, j, colheight, t;
351 int d = FBHEIGHT - vox->horizon;
352 uint8_t grad[FBHEIGHT];
357 grad[i] = XLERP(ctop, chor, t, 16);
359 for(i=d; i<FBHEIGHT; i++) {
363 for(i=0; i<FBWIDTH / 2; i++) {
365 colheight = FBHEIGHT - vox->coltop[i << 1];
367 for(j=0; j<colheight; j++) {
368 *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
369 fbptr += FBPITCH / 2;