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) */
61 struct vox_object *obj;
62 int num_obj, obj_stride;
68 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
72 assert(xsz == XSZ && ysz == YSZ);
74 if(!(vox = calloc(1, sizeof *vox))) {
82 vox->xmask = vox->xsz - 1;
83 vox->ymask = vox->ysz - 1;
92 vox->proj_dist = 4; /* TODO */
97 void vox_free(struct voxscape *vox)
108 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
111 memcpy(vox->color, data, vox->xsz * vox->ysz);
116 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
119 memcpy(vox->height, data, vox->xsz * vox->ysz);
124 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
127 vox->fogcolor = color;
131 vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
133 vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
136 #define vox_height(vox, x, y) H(x, y)
137 #define vox_color(vox, x, y) C(x, y)
141 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
144 int h00, h01, h10, h11, h0, h1;
151 h01 = H(x, y + 0x10000);
152 h10 = H(x + 0x10000, y);
153 h11 = H(x + 0x10000, y + 0x10000);
158 h0 = XLERP(h00, h01, v, 16);
159 h1 = XLERP(h10, h11, v, 16);
160 return XLERP(h0, h1, u, 16);
163 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
166 int c00, c01, c10, c11, c0, c1;
173 c01 = C(x, y + 0x10000);
174 c10 = C(x + 0x10000, y);
175 c11 = C(x + 0x10000, y + 0x10000);
180 c0 = XLERP(c00, c01, v, 16);
181 c1 = XLERP(c10, c11, v, 16);
182 return XLERP(c0, c1, u, 16);
184 #endif /* !NO_LERP */
187 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
193 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
195 if(xres != vox->fbwidth) {
196 if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
197 panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
202 vox->fbheight = yres;
203 vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
206 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
209 h = vox_height(vox, x, y) - h;
217 vox->valid &= ~SLICELEN;
220 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
226 vox->nslices = vox->zfar - vox->znear;
228 if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
229 panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
231 if(!(projlut = iwram_sbrk(vox->nslices * sizeof *projlut))) {
232 panic(get_pc(), "vox_framebuf: failed to allocate projection table (%d)\n", vox->nslices);
236 vox->valid &= ~SLICELEN;
240 * calculate extents of horizontal equidistant line from the viewer based on fov
241 * for each column step along this line and compute height for each pixel
242 * fill the visible (top) part of each column
245 void vox_render(struct voxscape *vox)
252 for(i=0; i<vox->nslices; i++) {
253 vox_render_slice(vox, i);
256 for(i=0; i<vox->nslices; i++) {
257 if(i >= 10 && (i & 1) == 0) {
260 vox_render_slice(vox, i);
266 void vox_begin(struct voxscape *vox)
270 memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
272 if(!(vox->valid & SLICELEN)) {
273 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
274 for(i=0; i<vox->nslices; i++) {
275 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
276 projlut[i] = (HSCALE << 8) / (vox->znear + i);
278 vox->valid |= SLICELEN;
283 void vox_render_slice(struct voxscape *vox, int n)
285 int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
286 int32_t x, y, len, xstep, ystep;
287 uint8_t color, last_col;
290 struct vox_object *obj;
294 len = vox->slicelen[n] >> 8;
295 xstep = (((COS(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
296 ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
298 x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
299 y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
301 /*proj = (HSCALE << 8) / (vox->znear + n);*/
303 for(i=0; i<FBWIDTH/2; i++) {
305 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
306 if(offs == last_offs) {
310 hval = vox->height[offs] - vox->vheight;
311 hval = ((hval * projlut[n]) >> 8) + vox->horizon;
312 if(hval > FBHEIGHT) hval = FBHEIGHT;
313 color = vox->color[offs];
318 if(hval > vox->coltop[col]) {
319 colstart = FBHEIGHT - hval;
320 colheight = hval - vox->coltop[col];
321 fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
323 for(j=0; j<colheight; j++) {
324 *fbptr = color | ((uint16_t)color << 8);
325 fbptr += FBPITCH / 2;
327 vox->coltop[col] = hval;
329 /* check to see if there's an object here */
331 for(j=0; j<vox->num_obj; j++) {
332 if(obj->offs == offs) {
335 obj->scale = projlut[n];
337 obj = (struct vox_object*)((char*)obj + vox->obj_stride);
346 void vox_sky_solid(struct voxscape *vox, uint8_t color)
351 for(i=0; i<FBWIDTH / 2; i++) {
353 colheight = FBHEIGHT - vox->coltop[i << 1];
355 for(j=0; j<colheight; j++) {
356 *fbptr = color | ((uint16_t)color << 8);
357 fbptr += FBPITCH / 2;
363 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
365 int i, j, colheight, t;
366 int d = FBHEIGHT - vox->horizon;
367 uint8_t grad[FBHEIGHT];
372 grad[i] = XLERP(ctop, chor, t, 16);
374 for(i=d; i<FBHEIGHT; i++) {
378 for(i=0; i<FBWIDTH / 2; i++) {
380 colheight = FBHEIGHT - vox->coltop[i << 1];
382 for(j=0; j<colheight; j++) {
383 *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
384 fbptr += FBPITCH / 2;
389 void vox_objects(struct voxscape *vox, struct vox_object *ptr, int count, int stride)
392 struct vox_object *obj;
395 vox->num_obj = count;
396 vox->obj_stride = stride;
399 for(i=0; i<count; i++) {
400 obj->offs = obj->y * XSZ + obj->x;
401 emuprint("%d,%d -> %d", obj->x, obj->y, obj->offs);
402 obj = (struct vox_object*)((char*)obj + stride);