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) */
64 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
68 assert(xsz == XSZ && ysz == YSZ);
70 if(!(vox = calloc(1, sizeof *vox))) {
78 vox->xmask = vox->xsz - 1;
79 vox->ymask = vox->ysz - 1;
88 vox->proj_dist = 4; /* TODO */
93 void vox_free(struct voxscape *vox)
104 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
107 memcpy(vox->color, data, vox->xsz * vox->ysz);
112 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
115 memcpy(vox->height, data, vox->xsz * vox->ysz);
120 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
123 vox->fogcolor = color;
127 vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
129 vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
132 #define vox_height(vox, x, y) H(x, y)
133 #define vox_color(vox, x, y) C(x, y)
137 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
140 int h00, h01, h10, h11, h0, h1;
147 h01 = H(x, y + 0x10000);
148 h10 = H(x + 0x10000, y);
149 h11 = H(x + 0x10000, y + 0x10000);
154 h0 = XLERP(h00, h01, v, 16);
155 h1 = XLERP(h10, h11, v, 16);
156 return XLERP(h0, h1, u, 16);
159 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
162 int c00, c01, c10, c11, c0, c1;
169 c01 = C(x, y + 0x10000);
170 c10 = C(x + 0x10000, y);
171 c11 = C(x + 0x10000, y + 0x10000);
176 c0 = XLERP(c00, c01, v, 16);
177 c1 = XLERP(c10, c11, v, 16);
178 return XLERP(c0, c1, u, 16);
180 #endif /* !NO_LERP */
183 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
189 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
191 if(xres != vox->fbwidth) {
192 if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
193 panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
199 vox->fbheight = yres;
200 vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
203 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
206 h = vox_height(vox, x, y) - h;
214 vox->valid &= ~SLICELEN;
217 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
223 vox->nslices = vox->zfar - vox->znear;
225 if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
226 panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
231 vox->valid &= ~SLICELEN;
235 * calculate extents of horizontal equidistant line from the viewer based on fov
236 * for each column step along this line and compute height for each pixel
237 * fill the visible (top) part of each column
240 void vox_render(struct voxscape *vox)
247 for(i=0; i<vox->nslices; i++) {
248 vox_render_slice(vox, i);
251 for(i=0; i<vox->nslices; i++) {
252 if(i >= 10 && (i & 1) == 0) {
255 vox_render_slice(vox, i);
261 void vox_begin(struct voxscape *vox)
265 memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
267 if(!(vox->valid & SLICELEN)) {
268 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
269 for(i=0; i<vox->nslices; i++) {
270 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
272 vox->valid |= SLICELEN;
277 void vox_render_slice(struct voxscape *vox, int n)
279 int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
280 int32_t x, y, len, xstep, ystep;
281 uint8_t color, last_col;
286 len = vox->slicelen[n] >> 8;
287 xstep = (((COS(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
288 ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
290 x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
291 y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
293 for(i=0; i<FBWIDTH/2; i++) {
295 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
296 if(offs == last_offs) {
300 hval = vox->height[offs] - vox->vheight;
301 hval = hval * HSCALE / (vox->znear + n) + vox->horizon;
302 if(hval > FBHEIGHT) hval = FBHEIGHT;
303 color = vox->color[offs];
308 if(hval > vox->coltop[col]) {
309 colstart = FBHEIGHT - hval;
310 colheight = hval - vox->coltop[col];
311 fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
313 for(j=0; j<colheight; j++) {
314 *fbptr = color | ((uint16_t)color << 8);
315 fbptr += FBPITCH / 2;
317 vox->coltop[col] = hval;
325 void vox_sky_solid(struct voxscape *vox, uint8_t color)
330 for(i=0; i<FBWIDTH / 2; i++) {
332 colheight = FBHEIGHT - vox->coltop[i << 1];
334 for(j=0; j<colheight; j++) {
335 *fbptr = color | ((uint16_t)color << 8);
336 fbptr += FBPITCH / 2;
342 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
344 int i, j, colheight, t;
345 int d = FBHEIGHT - vox->horizon;
346 uint8_t grad[FBHEIGHT];
351 grad[i] = XLERP(ctop, chor, t, 16);
353 for(i=d; i<FBHEIGHT; i++) {
357 for(i=0; i<FBWIDTH / 2; i++) {
359 colheight = FBHEIGHT - vox->coltop[i << 1];
361 for(j=0; j<colheight; j++) {
362 *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
363 fbptr += FBPITCH / 2;