10 /* hardcoded dimensions for the GBA */
24 #define XLERP(a, b, t, fp) \
25 ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
33 unsigned char *height;
35 int xshift, xmask, ymask;
41 int fbwidth, fbheight;
55 int zfog; /* fog start Z (0: no fog) */
61 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
65 assert(xsz == XSZ && ysz == YSZ);
67 if(!(vox = calloc(1, sizeof *vox))) {
75 vox->xmask = vox->xsz - 1;
76 vox->ymask = vox->ysz - 1;
85 vox->proj_dist = 4; /* TODO */
90 void vox_free(struct voxscape *vox)
101 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
104 memcpy(vox->color, data, vox->xsz * vox->ysz);
109 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
112 memcpy(vox->height, data, vox->xsz * vox->ysz);
117 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
120 vox->fogcolor = color;
124 vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
126 vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
129 #define vox_height(vox, x, y) H(x, y)
130 #define vox_color(vox, x, y) C(x, y)
134 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
137 int h00, h01, h10, h11, h0, h1;
144 h01 = H(x, y + 0x10000);
145 h10 = H(x + 0x10000, y);
146 h11 = H(x + 0x10000, y + 0x10000);
151 h0 = XLERP(h00, h01, v, 16);
152 h1 = XLERP(h10, h11, v, 16);
153 return XLERP(h0, h1, u, 16);
156 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
159 int c00, c01, c10, c11, c0, c1;
166 c01 = C(x, y + 0x10000);
167 c10 = C(x + 0x10000, y);
168 c11 = C(x + 0x10000, y + 0x10000);
173 c0 = XLERP(c00, c01, v, 16);
174 c1 = XLERP(c10, c11, v, 16);
175 return XLERP(c0, c1, u, 16);
177 #endif /* !NO_LERP */
180 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
186 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
188 if(xres != vox->fbwidth) {
189 if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
190 panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
196 vox->fbheight = yres;
197 vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
200 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
203 h = vox_height(vox, x, y) - h;
211 vox->valid &= ~SLICELEN;
214 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
220 vox->nslices = vox->zfar - vox->znear;
222 if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
223 panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
227 vox->valid &= ~SLICELEN;
231 * calculate extents of horizontal equidistant line from the viewer based on fov
232 * for each column step along this line and compute height for each pixel
233 * fill the visible (top) part of each column
236 void vox_render(struct voxscape *vox)
241 for(i=0; i<vox->nslices; i++) {
242 vox_render_slice(vox, i);
246 void vox_begin(struct voxscape *vox)
250 memset(vox->fb, 0, FBWIDTH * FBHEIGHT);
251 memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
253 if(!(vox->valid & SLICELEN)) {
254 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
255 for(i=0; i<vox->nslices; i++) {
256 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
258 vox->valid |= SLICELEN;
263 void vox_render_slice(struct voxscape *vox, int n)
265 int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
266 int32_t x, y, len, xstep, ystep;
267 uint8_t color, last_col;
272 len = vox->slicelen[n] >> 8;
273 xstep = (((COS(vox->angle) >> 4) * len) >> 4) / FBWIDTH;
274 ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / FBWIDTH;
276 x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 2);
277 y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 2);
279 for(i=1; i<FBWIDTH / 2; i++) {
281 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
282 if(offs == last_offs) {
286 hval = vox->height[offs] - vox->vheight;
287 hval = hval * HSCALE / (vox->znear + n) + vox->horizon;
288 if(hval > FBHEIGHT) hval = FBHEIGHT;
289 color = vox->color[offs];
294 if(hval > vox->coltop[col]) {
295 colstart = FBHEIGHT - hval;
296 colheight = hval - vox->coltop[col];
297 fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
299 for(j=0; j<colheight; j++) {
301 fbptr += FBWIDTH / 2;
303 vox->coltop[col] = hval;
309 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
310 if(offs == last_offs) {
314 hval = vox->height[offs] - vox->vheight;
315 hval = hval * HSCALE / (vox->znear + n) + vox->horizon;
316 if(hval > FBHEIGHT) hval = FBHEIGHT;
317 color = vox->color[offs];
322 if(hval > vox->coltop[col]) {
323 colstart = FBHEIGHT - hval;
324 colheight = hval - vox->coltop[col];
325 fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
327 for(j=0; j<colheight; j++) {
328 *fbptr |= ((uint16_t)color << 8);
329 fbptr += FBWIDTH / 2;
331 vox->coltop[col] = hval;
339 void vox_sky_solid(struct voxscape *vox, uint8_t color)
341 int i, j, colh0, colh1, colhboth;
344 for(i=1; i<FBWIDTH / 2; i++) {
346 colh0 = FBHEIGHT - vox->coltop[i << 1];
347 colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1];
348 colhboth = colh0 < colh1 ? colh0 : colh1;
350 for(j=0; j<colhboth; j++) {
351 *fbptr = color | ((uint16_t)color << 8);
352 fbptr += FBWIDTH / 2;
356 for(j=colhboth; j<colh0; j++) {
358 fbptr += FBWIDTH / 2;
361 for(j=colhboth; j<colh1; j++) {
362 *fbptr |= (uint16_t)color << 8;
363 fbptr += FBWIDTH / 2;
369 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
371 int i, j, colh0, colh1, colhboth, t;
372 int d = FBHEIGHT - vox->horizon;
373 uint8_t grad[FBHEIGHT];
378 grad[i] = XLERP(ctop, chor, t, 16);
380 for(i=d; i<FBHEIGHT; i++) {
384 for(i=1; i<FBWIDTH / 2; i++) {
386 colh0 = FBHEIGHT - vox->coltop[i << 1];
387 colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1];
388 colhboth = colh0 < colh1 ? colh0 : colh1;
390 for(j=0; j<colhboth; j++) {
391 *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
392 fbptr += FBWIDTH / 2;
396 for(j=colhboth; j<colh0; j++) {
398 fbptr += FBWIDTH / 2;
401 for(j=colhboth; j<colh1; j++) {
402 *fbptr |= (uint16_t)grad[j] << 8;
403 fbptr += FBWIDTH / 2;