10 /* hardcoded dimensions for the GBA */
24 #define XLERP(a, b, t, fp) \
25 ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
27 static inline uint16_t lerp_rgb(int r0, int g0, int b0, int r1, int g1, int b1, int32_t t);
37 int xshift, xmask, ymask;
43 int fbwidth, fbheight;
58 int zfog; /* fog start Z (0: no fog) */
65 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint16_t *cimg)
69 assert(xsz == XSZ && ysz == YSZ);
71 if(!(vox = calloc(1, sizeof *vox))) {
79 vox->xmask = vox->xsz - 1;
80 vox->ymask = vox->ysz - 1;
89 vox->proj_dist = 4; /* TODO */
94 void vox_free(struct voxscape *vox)
105 uint16_t *vox_texture(struct voxscape *vox, uint16_t *data)
108 memcpy(vox->color, data, vox->xsz * vox->ysz * sizeof *vox->color);
113 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
116 memcpy(vox->height, data, vox->xsz * vox->ysz);
122 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
125 vox->fogcolor = color;
130 vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
132 vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
135 #define vox_height(vox, x, y) H(x, y)
136 #define vox_color(vox, x, y) C(x, y)
140 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
143 int h00, h01, h10, h11, h0, h1;
150 h01 = H(x, y + 0x10000);
151 h10 = H(x + 0x10000, y);
152 h11 = H(x + 0x10000, y + 0x10000);
157 h0 = XLERP(h00, h01, v, 16);
158 h1 = XLERP(h10, h11, v, 16);
159 return XLERP(h0, h1, u, 16);
162 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
165 int c00, c01, c10, c11, c0, c1;
172 c01 = C(x, y + 0x10000);
173 c10 = C(x + 0x10000, y);
174 c11 = C(x + 0x10000, y + 0x10000);
179 c0 = XLERP(c00, c01, v, 16);
180 c1 = XLERP(c10, c11, v, 16);
181 return XLERP(c0, c1, u, 16);
183 #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;
226 if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
227 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
241 void vox_render(struct voxscape *vox)
246 for(i=0; i<vox->nslices; i++) {
247 if(i >= 25 && (i & 1)) continue;
248 vox_render_slice(vox, i);
253 void vox_begin(struct voxscape *vox)
257 memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
259 if(!(vox->valid & SLICELEN)) {
260 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
261 for(i=0; i<vox->nslices; i++) {
262 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
264 vox->valid |= SLICELEN;
269 void vox_render_slice(struct voxscape *vox, int n)
271 int i, j, hval, last_hval, colstart, colheight, z, offs, last_offs = -1;
272 int32_t x, y, len, xstep, ystep;
273 uint16_t color, last_col;
278 len = vox->slicelen[n] >> 8;
279 xstep = (((COS(vox->angle) >> 4) * len) >> 4) / FBWIDTH;
280 ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / FBWIDTH;
282 x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 2);
283 y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 2);
285 for(i=0; i<FBWIDTH; i++) {
286 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
287 if(offs == last_offs) {
291 hval = vox->height[offs] - vox->vheight;
292 hval = hval * HSCALE / z + vox->horizon;
293 if(hval > FBHEIGHT) hval = FBHEIGHT;
294 color = vox->color[offs];
299 if(hval > vox->coltop[i]) {
300 colstart = FBHEIGHT - hval;
301 colheight = hval - vox->coltop[i];
302 fbptr = vox->fb + colstart * FBWIDTH + i;
304 for(j=0; j<colheight; j++) {
308 vox->coltop[i] = hval;
316 void vox_sky_solid(struct voxscape *vox, uint16_t color)
321 for(i=0; i<FBWIDTH; i++) {
323 colheight = FBHEIGHT - vox->coltop[i];
325 for(j=0; j<colheight; j++) {
333 void vox_sky_grad(struct voxscape *vox, uint16_t chor, uint16_t ctop)
335 int i, j, colheight, t;
336 int r0, g0, b0, r1, g1, b1;
337 int d = FBHEIGHT - vox->horizon;
338 uint16_t grad[FBHEIGHT], *fbptr;
341 g0 = (ctop >> 5) & 0x1f;
342 b0 = (ctop >> 10) & 0x1f;
344 g1 = (chor >> 5) & 0x1f;
345 b1 = (chor >> 10) & 0x1f;
349 grad[i] = lerp_rgb(r0, g0, b0, r1, g1, b1, t);
351 for(i=d; i<vox->fbheight; i++) {
355 for(i=0; i<vox->fbwidth; i++) {
357 colheight = FBHEIGHT - vox->coltop[i];
358 for(j=0; j<colheight; j++) {
365 static inline uint16_t lerp_rgb(int r0, int g0, int b0, int r1, int g1, int b1, int32_t t)
367 int r = XLERP(r0, r1, t, 16);
368 int g = XLERP(g0, g1, t, 16);
369 int b = XLERP(b0, b1, t, 16);
370 return r | (g << 5) | (b << 10);