int xsz, ysz;
unsigned char *height;
uint32_t *color;
+ int xshift, xmask, ymask;
/* framebuffer */
uint32_t *fb;
/* view */
int32_t x, y, angle;
+ int vheight;
/* projection */
int fov, znear, zfar;
int nslices;
- int32_t *slicelen; /* 24.8 */
+ int32_t *slicelen;
+ int proj_dist;
unsigned int valid;
};
vox->xsz = xsz;
vox->ysz = ysz;
+ vox->xmask = vox->xsz - 1;
+ vox->ymask = vox->ysz - 1;
+
+ vox->xshift = -1;
+ while(xsz) {
+ xsz >>= 1;
+ vox->xshift++;
+ }
+
+ vox->proj_dist = 2; /* TODO */
+
return vox;
}
vox->angle = angle;
/* TODO precalc stuff */
- valid &= ~SLICELEN;
+ vox->valid &= ~SLICELEN;
}
void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
{
- int i;
-
vox->fov = fov;
vox->znear = znear;
vox->zfar = zfar;
return;
}
- valid &= ~SLICELEN;
+ vox->valid &= ~SLICELEN;
}
/* algorithm:
{
int i;
+ /*
for(i=0; i<vox->fbwidth; i++) {
vox->coltop[i] = vox->fbheight;
}
+ */
+ memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
if(!(vox->valid & SLICELEN)) {
float theta = (float)vox->angle * M_PI / 360.0f; /* half angle */
for(i=0; i<vox->nslices; i++) {
- vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 2.0f * 256.0f)
+ vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 2.0f * 65536.0f);
}
vox->valid |= SLICELEN;
}
void vox_render_slice(struct voxscape *vox, int n)
{
- int32_t len;
+ int i, j, tx, ty, hval, colstart, colheight;
+ int32_t x, y, len, xstep;
+ uint32_t color;
+ uint32_t *fbptr;
len = vox->slicelen[n];
+ xstep = len / vox->fbwidth;
- /* TODO cont. */
+ x = vox->x - xstep * (vox->fbwidth >> 1);
+ y = vox->y + (vox->znear << 16);
+ for(i=0; i<vox->fbwidth; i++) {
+ tx = (x >> 16) & vox->xmask;
+ ty = (y >> 16) & vox->ymask;
+
+ hval = (vox->height[(ty << vox->xshift) + tx] - vox->vheight) * vox->proj_dist / y;
+ if(hval > vox->coltop[i]) {
+ color = vox->color[(ty << vox->xshift) + tx];
+ colstart = vox->fbheight - 1 - hval;
+ colheight = hval - vox->coltop[i];
+ fbptr = vox->fb + colstart * vox->fbwidth + i;
+
+ for(j=0; j<colheight; j++) {
+ *fbptr = color;
+ fbptr += vox->fbwidth;
+ }
+ }
+
+ x += xstep;
+ }
}