10 /* hardcoded dimensions for the GBA */
23 #define XLERP(a, b, t, fp) \
24 ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
32 unsigned char *height;
34 int xshift, xmask, ymask;
40 int fbwidth, fbheight;
54 int zfog; /* fog start Z (0: no fog) */
60 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
64 if(!(vox = calloc(1, sizeof *vox))) {
72 vox->xmask = vox->xsz - 1;
73 vox->ymask = vox->ysz - 1;
82 vox->proj_dist = 4; /* TODO */
87 void vox_free(struct voxscape *vox)
98 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
101 memcpy(vox->color, data, vox->xsz * vox->ysz);
106 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
109 memcpy(vox->height, data, vox->xsz * vox->ysz);
114 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
117 vox->fogcolor = color;
121 vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
123 vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
126 #define vox_height(vox, x, y) H(x, y)
127 #define vox_color(vox, x, y) C(x, y)
131 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
134 int h00, h01, h10, h11, h0, h1;
141 h01 = H(x, y + 0x10000);
142 h10 = H(x + 0x10000, y);
143 h11 = H(x + 0x10000, y + 0x10000);
148 h0 = XLERP(h00, h01, v, 16);
149 h1 = XLERP(h10, h11, v, 16);
150 return XLERP(h0, h1, u, 16);
153 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
156 int c00, c01, c10, c11, c0, c1;
163 c01 = C(x, y + 0x10000);
164 c10 = C(x + 0x10000, y);
165 c11 = C(x + 0x10000, y + 0x10000);
170 c0 = XLERP(c00, c01, v, 16);
171 c1 = XLERP(c10, c11, v, 16);
172 return XLERP(c0, c1, u, 16);
174 #endif /* !NO_LERP */
177 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
183 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
185 if(xres != vox->fbwidth) {
186 if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
187 panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
193 vox->fbheight = yres;
194 vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
197 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
200 h = vox_height(vox, x, y) - h;
208 vox->valid &= ~SLICELEN;
211 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
217 vox->nslices = vox->zfar - vox->znear;
219 if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
220 panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
224 vox->valid &= ~SLICELEN;
228 * calculate extents of horizontal equidistant line from the viewer based on fov
229 * for each column step along this line and compute height for each pixel
230 * fill the visible (top) part of each column
233 void vox_render(struct voxscape *vox)
238 for(i=0; i<vox->nslices; i++) {
239 vox_render_slice(vox, i);
243 void vox_begin(struct voxscape *vox)
247 memset(vox->fb, 0, FBWIDTH * FBHEIGHT);
248 memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
250 if(!(vox->valid & SLICELEN)) {
251 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
252 for(i=0; i<vox->nslices; i++) {
253 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
255 vox->valid |= SLICELEN;
260 void vox_render_slice(struct voxscape *vox, int n)
262 int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
263 int32_t x, y, len, xstep, ystep;
264 uint8_t color, last_col;
269 len = vox->slicelen[n] >> 8;
270 xstep = (((COS(vox->angle) >> 4) * len) >> 4) / FBWIDTH;
271 ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / FBWIDTH;
273 x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 2);
274 y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 2);
276 for(i=1; i<FBWIDTH / 2; i++) {
278 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
279 if(offs == last_offs) {
283 hval = vox->height[offs] - vox->vheight;
284 hval = hval * 40 / (vox->znear + n) + vox->horizon;
285 if(hval > FBHEIGHT) hval = FBHEIGHT;
286 color = vox->color[offs];
291 if(hval > vox->coltop[col]) {
292 colstart = FBHEIGHT - hval;
293 colheight = hval - vox->coltop[col];
294 fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
296 for(j=0; j<colheight; j++) {
298 fbptr += FBWIDTH / 2;
300 vox->coltop[col] = hval;
306 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
307 if(offs == last_offs) {
311 hval = vox->height[offs] - vox->vheight;
312 hval = hval * 40 / (vox->znear + n) + vox->horizon;
313 if(hval > FBHEIGHT) hval = FBHEIGHT;
314 color = vox->color[offs];
319 if(hval > vox->coltop[col]) {
320 colstart = FBHEIGHT - hval;
321 colheight = hval - vox->coltop[col];
322 fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
324 for(j=0; j<colheight; j++) {
325 *fbptr |= ((uint16_t)color << 8);
326 fbptr += FBWIDTH / 2;
328 vox->coltop[col] = hval;
336 void vox_sky_solid(struct voxscape *vox, uint8_t color)
338 int i, j, colh0, colh1, colhboth;
341 for(i=1; i<FBWIDTH / 2; i++) {
343 colh0 = FBHEIGHT - vox->coltop[i << 1];
344 colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1];
345 colhboth = colh0 < colh1 ? colh0 : colh1;
347 for(j=0; j<colhboth; j++) {
348 *fbptr = color | ((uint16_t)color << 8);
349 fbptr += FBWIDTH / 2;
353 for(j=colhboth; j<colh0; j++) {
355 fbptr += FBWIDTH / 2;
358 for(j=colhboth; j<colh1; j++) {
359 *fbptr |= (uint16_t)color << 8;
360 fbptr += FBWIDTH / 2;
366 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
368 int i, j, colh0, colh1, colhboth, t;
369 int d = FBHEIGHT - vox->horizon;
370 uint8_t grad[FBHEIGHT];
375 grad[i] = XLERP(ctop, chor, t, 16);
377 for(i=d; i<FBHEIGHT; i++) {
381 for(i=1; i<FBWIDTH / 2; i++) {
383 colh0 = FBHEIGHT - vox->coltop[i << 1];
384 colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1];
385 colhboth = colh0 < colh1 ? colh0 : colh1;
387 for(j=0; j<colhboth; j++) {
388 *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
389 fbptr += FBWIDTH / 2;
393 for(j=colhboth; j<colh0; j++) {
395 fbptr += FBWIDTH / 2;
398 for(j=colhboth; j<colh1; j++) {
399 *fbptr |= (uint16_t)grad[j] << 8;
400 fbptr += FBWIDTH / 2;