11 /* hardcoded dimensions for the GBA */
24 #define OBJ_STRIDE_SHIFT 5
28 #define XLERP(a, b, t, fp) \
29 ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
37 unsigned char *height;
39 int xshift, xmask, ymask;
45 int fbwidth, fbheight;
59 int zfog; /* fog start Z (0: no fog) */
64 struct vox_object *obj;
65 int num_obj, obj_stride;
71 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
75 assert(xsz == XSZ && ysz == YSZ);
77 if(!(vox = calloc(1, sizeof *vox))) {
85 vox->xmask = vox->xsz - 1;
86 vox->ymask = vox->ysz - 1;
95 vox->proj_dist = 4; /* TODO */
100 void vox_free(struct voxscape *vox)
111 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
114 memcpy(vox->color, data, vox->xsz * vox->ysz);
119 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
122 memcpy(vox->height, data, vox->xsz * vox->ysz);
127 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
130 vox->fogcolor = color;
134 vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
136 vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
139 #define vox_height(vox, x, y) H(x, y)
140 #define vox_color(vox, x, y) C(x, y)
144 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
147 int h00, h01, h10, h11, h0, h1;
154 h01 = H(x, y + 0x10000);
155 h10 = H(x + 0x10000, y);
156 h11 = H(x + 0x10000, y + 0x10000);
161 h0 = XLERP(h00, h01, v, 16);
162 h1 = XLERP(h10, h11, v, 16);
163 return XLERP(h0, h1, u, 16);
166 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
169 int c00, c01, c10, c11, c0, c1;
176 c01 = C(x, y + 0x10000);
177 c10 = C(x + 0x10000, y);
178 c11 = C(x + 0x10000, y + 0x10000);
183 c0 = XLERP(c00, c01, v, 16);
184 c1 = XLERP(c10, c11, v, 16);
185 return XLERP(c0, c1, u, 16);
187 #endif /* !NO_LERP */
190 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
196 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
198 if(xres != vox->fbwidth) {
199 if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
200 panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
205 vox->fbheight = yres;
206 vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
209 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
212 h = vox_height(vox, x, y) - h;
220 vox->valid &= ~SLICELEN;
223 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
229 vox->nslices = vox->zfar - vox->znear;
231 if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
232 panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
234 if(!(projlut = iwram_sbrk(vox->nslices * sizeof *projlut))) {
235 panic(get_pc(), "vox_framebuf: failed to allocate projection table (%d)\n", vox->nslices);
239 vox->valid &= ~SLICELEN;
243 * calculate extents of horizontal equidistant line from the viewer based on fov
244 * for each column step along this line and compute height for each pixel
245 * fill the visible (top) part of each column
248 void vox_render(struct voxscape *vox)
255 for(i=0; i<vox->nslices; i++) {
256 vox_render_slice(vox, i);
259 for(i=0; i<vox->nslices; i++) {
260 if(i >= 10 && (i & 1) == 0) {
263 vox_render_slice(vox, i);
269 void vox_begin(struct voxscape *vox)
273 memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
275 if(!(vox->valid & SLICELEN)) {
276 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
277 for(i=0; i<vox->nslices; i++) {
278 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
279 projlut[i] = (HSCALE << 8) / (vox->znear + i);
281 vox->valid |= SLICELEN;
286 void vox_render_slice(struct voxscape *vox, int n)
288 int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
289 int32_t x, y, len, xstep, ystep;
290 uint8_t color, last_col;
293 struct vox_object *obj;
297 len = vox->slicelen[n] >> 8;
298 xstep = (((COS(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
299 ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
301 x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
302 y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
304 /*proj = (HSCALE << 8) / (vox->znear + n);*/
306 for(i=0; i<FBWIDTH/2; i++) {
308 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
309 if(offs == last_offs) {
313 hval = vox->height[offs] - vox->vheight;
314 hval = ((hval * projlut[n]) >> 8) + vox->horizon;
315 if(hval > FBHEIGHT) hval = FBHEIGHT;
316 color = vox->color[offs];
321 if(hval > vox->coltop[col]) {
322 colstart = FBHEIGHT - hval;
323 colheight = hval - vox->coltop[col];
324 fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
326 for(j=0; j<colheight; j++) {
327 *fbptr = color | ((uint16_t)color << 8);
328 fbptr += FBPITCH / 2;
330 vox->coltop[col] = hval;
332 /* check to see if there's an object here */
333 if(color >= CMAP_SPAWN0) {
334 int idx = color - CMAP_SPAWN0;
335 obj = (struct vox_object*)((char*)vox->obj + (idx << OBJ_STRIDE_SHIFT));
338 obj->scale = projlut[n];
347 void vox_sky_solid(struct voxscape *vox, uint8_t color)
352 for(i=0; i<FBWIDTH / 2; i++) {
354 colheight = FBHEIGHT - vox->coltop[i << 1];
356 for(j=0; j<colheight; j++) {
357 *fbptr = color | ((uint16_t)color << 8);
358 fbptr += FBPITCH / 2;
364 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
366 int i, j, colheight, t;
367 int d = FBHEIGHT - vox->horizon;
368 uint8_t grad[FBHEIGHT];
373 grad[i] = XLERP(ctop, chor, t, 16);
375 for(i=d; i<FBHEIGHT; i++) {
379 for(i=0; i<FBWIDTH / 2; i++) {
381 colheight = FBHEIGHT - vox->coltop[i << 1];
383 for(j=0; j<colheight; j++) {
384 *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
385 fbptr += FBPITCH / 2;
390 void vox_objects(struct voxscape *vox, struct vox_object *ptr, int count, int stride)
393 struct vox_object *obj;
395 if(stride != 1 << OBJ_STRIDE_SHIFT) {
396 panic(get_pc(), "optimization requires %d byte vox obj (got %d)",
397 1 << OBJ_STRIDE_SHIFT, stride);
401 vox->num_obj = count;
402 vox->obj_stride = stride;
405 for(i=0; i<count; i++) {
406 obj->offs = obj->y * XSZ + obj->x;
407 obj = (struct vox_object*)((char*)obj + stride);