10 #define XLERP(a, b, t, fp) \
11 ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
19 unsigned char *height;
21 int xshift, xmask, ymask;
27 int fbwidth, fbheight;
41 int zfog; /* fog start Z (0: no fog) */
47 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
51 if(!(vox = calloc(1, sizeof *vox))) {
59 vox->xmask = vox->xsz - 1;
60 vox->ymask = vox->ysz - 1;
69 vox->proj_dist = 4; /* TODO */
74 void vox_free(struct voxscape *vox)
85 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
88 memcpy(vox->color, data, vox->xsz * vox->ysz);
93 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
96 memcpy(vox->height, data, vox->xsz * vox->ysz);
101 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
104 vox->fogcolor = color;
108 vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
110 vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
113 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
116 int h00, h01, h10, h11, h0, h1;
123 h01 = H(x, y + 0x10000);
124 h10 = H(x + 0x10000, y);
125 h11 = H(x + 0x10000, y + 0x10000);
130 h0 = XLERP(h00, h01, v, 16);
131 h1 = XLERP(h10, h11, v, 16);
132 return XLERP(h0, h1, u, 16);
135 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
138 int c00, c01, c10, c11, c0, c1;
145 c01 = C(x, y + 0x10000);
146 c10 = C(x + 0x10000, y);
147 c11 = C(x + 0x10000, y + 0x10000);
152 c0 = XLERP(c00, c01, v, 16);
153 c1 = XLERP(c10, c11, v, 16);
154 return XLERP(c0, c1, u, 16);
158 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
164 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
166 if(xres != vox->fbwidth) {
168 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
169 fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
175 vox->fbheight = yres;
176 vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
179 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
182 h = vox_height(vox, x, y) - h;
190 vox->valid &= ~SLICELEN;
193 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
199 vox->nslices = vox->zfar - vox->znear;
201 if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
202 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
206 vox->valid &= ~SLICELEN;
210 * calculate extents of horizontal equidistant line from the viewer based on fov
211 * for each column step along this line and compute height for each pixel
212 * fill the visible (top) part of each column
215 void vox_render(struct voxscape *vox)
220 for(i=0; i<vox->nslices; i++) {
221 vox_render_slice(vox, i);
225 void vox_begin(struct voxscape *vox)
229 memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
231 if(!(vox->valid & SLICELEN)) {
232 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
233 for(i=0; i<vox->nslices; i++) {
234 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
236 vox->valid |= SLICELEN;
240 void vox_render_slice(struct voxscape *vox, int n)
242 int i, j, hval, colstart, colheight, z;
243 int32_t x, y, len, xstep, ystep;
249 len = vox->slicelen[n] >> 8;
250 xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
251 ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
253 x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
254 y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
255 /* TODO double column */
256 for(i=0; i<vox->fbwidth/2; i++) {
257 hval = vox_height(vox, x, y) - vox->vheight;
258 hval = hval * 160 / (vox->znear + n) + vox->horizon;
259 if(hval > vox->fbheight) hval = vox->fbheight;
260 if(hval > vox->coltop[i]) {
261 color = vox_color(vox, x, y);
262 colstart = vox->fbheight - hval;
263 colheight = hval - vox->coltop[i];
264 fbptr = vox->fb + colstart * vox->fbwidth / 2 + i / 2;
266 for(j=0; j<colheight; j++) {
267 *fbptr = color | ((uint16_t)color << 8);
268 fbptr += vox->fbwidth >> 1;
270 vox->coltop[i] = hval;
278 void vox_sky_solid(struct voxscape *vox, uint8_t color)
283 /* TODO double columns */
284 for(i=0; i<vox->fbwidth/2; i++) {
286 colheight = vox->fbheight - vox->coltop[i];
287 for(j=0; j<colheight; j++) {
288 *fbptr = color | ((uint16_t)color << 8);
289 fbptr += vox->fbwidth >> 1;
294 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
296 int i, j, colheight, t;
297 int d = vox->fbheight - vox->horizon;
301 grad = alloca(vox->fbheight * sizeof *grad);
305 grad[i] = XLERP(ctop, chor, t, 8);
307 for(i=d; i<vox->fbheight; i++) {
311 /* TODO double columns */
312 for(i=0; i<vox->fbwidth/2; i++) {
313 fbptr = vox->fb + i / 2;
314 colheight = vox->fbheight - vox->coltop[i];
315 for(j=0; j<colheight; j++) {
316 *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
317 fbptr += vox->fbwidth >> 1;