13 #define XLERP(a, b, t, fp) \
14 ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
22 unsigned char *height;
24 int xshift, xmask, ymask;
30 int fbwidth, fbheight;
44 int zfog; /* fog start Z (0: no fog) */
50 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
54 if(!(vox = calloc(1, sizeof *vox))) {
62 vox->xmask = vox->xsz - 1;
63 vox->ymask = vox->ysz - 1;
72 vox->proj_dist = 4; /* TODO */
77 void vox_free(struct voxscape *vox)
88 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
91 memcpy(vox->color, data, vox->xsz * vox->ysz);
96 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
99 memcpy(vox->height, data, vox->xsz * vox->ysz);
104 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
107 vox->fogcolor = color;
111 vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
113 vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
116 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
119 int h00, h01, h10, h11, h0, h1;
126 h01 = H(x, y + 0x10000);
127 h10 = H(x + 0x10000, y);
128 h11 = H(x + 0x10000, y + 0x10000);
133 h0 = XLERP(h00, h01, v, 16);
134 h1 = XLERP(h10, h11, v, 16);
135 return XLERP(h0, h1, u, 16);
138 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
141 int c00, c01, c10, c11, c0, c1;
148 c01 = C(x, y + 0x10000);
149 c10 = C(x + 0x10000, y);
150 c11 = C(x + 0x10000, y + 0x10000);
155 c0 = XLERP(c00, c01, v, 16);
156 c1 = XLERP(c10, c11, v, 16);
157 return XLERP(c0, c1, u, 16);
161 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
167 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
169 if(xres != vox->fbwidth) {
171 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
172 fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
178 vox->fbheight = yres;
179 vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
182 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
185 h = vox_height(vox, x, y) - h;
193 vox->valid &= ~SLICELEN;
196 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
202 vox->nslices = vox->zfar - vox->znear;
204 if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
205 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
209 vox->valid &= ~SLICELEN;
213 * calculate extents of horizontal equidistant line from the viewer based on fov
214 * for each column step along this line and compute height for each pixel
215 * fill the visible (top) part of each column
218 void vox_render(struct voxscape *vox)
223 for(i=0; i<vox->nslices; i++) {
224 vox_render_slice(vox, i);
228 void vox_begin(struct voxscape *vox)
232 memset(vox->fb, 0, FBWIDTH * FBHEIGHT);
233 memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
235 if(!(vox->valid & SLICELEN)) {
236 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
237 for(i=0; i<vox->nslices; i++) {
238 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
240 vox->valid |= SLICELEN;
244 void vox_render_slice(struct voxscape *vox, int n)
246 int i, j, hval, colstart, colheight, col, z;
247 int32_t x, y, len, xstep, ystep;
253 len = vox->slicelen[n] >> 8;
254 xstep = (((COS(vox->angle) >> 4) * len) >> 4) / FBWIDTH;
255 ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / FBWIDTH;
257 x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 2);
258 y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 2);
260 for(i=0; i<FBWIDTH / 2; i++) {
262 hval = vox_height(vox, x, y) - vox->vheight;
263 hval = hval * 40 / (vox->znear + n) + vox->horizon;
264 if(hval > FBHEIGHT) hval = FBHEIGHT;
265 if(hval > vox->coltop[col]) {
266 color = vox_color(vox, x, y);
267 colstart = FBHEIGHT - hval;
268 colheight = hval - vox->coltop[col];
269 fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
271 for(j=0; j<colheight; j++) {
273 fbptr += FBWIDTH / 2;
275 vox->coltop[col] = hval;
281 hval = vox_height(vox, x, y) - vox->vheight;
282 hval = hval * 40 / (vox->znear + n) + vox->horizon;
283 if(hval > FBHEIGHT) hval = FBHEIGHT;
284 if(hval > vox->coltop[col]) {
285 color = vox_color(vox, x, y);
286 colstart = FBHEIGHT - hval;
287 colheight = hval - vox->coltop[col];
288 fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
290 for(j=0; j<colheight; j++) {
291 *fbptr |= ((uint16_t)color << 8);
292 fbptr += FBWIDTH / 2;
294 vox->coltop[col] = hval;
301 void vox_sky_solid(struct voxscape *vox, uint8_t color)
303 int i, j, colh0, colh1, colhboth;
306 for(i=0; i<FBWIDTH / 2; i++) {
308 colh0 = FBHEIGHT - vox->coltop[i << 1];
309 colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1];
310 colhboth = colh0 < colh1 ? colh0 : colh1;
312 for(j=0; j<colhboth; j++) {
313 *fbptr = color | ((uint16_t)color << 8);
314 fbptr += FBWIDTH / 2;
318 for(j=colhboth; j<colh0; j++) {
320 fbptr += FBWIDTH / 2;
323 for(j=colhboth; j<colh1; j++) {
324 *fbptr |= (uint16_t)color << 8;
325 fbptr += FBWIDTH / 2;
331 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
333 int i, j, colh0, colh1, colhboth, t;
334 int d = FBHEIGHT - vox->horizon;
335 uint8_t grad[FBHEIGHT];
340 grad[i] = XLERP(ctop, chor, t, 16);
342 for(i=d; i<FBHEIGHT; i++) {
346 for(i=0; i<FBWIDTH / 2; i++) {
348 colh0 = FBHEIGHT - vox->coltop[i << 1];
349 colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1];
350 colhboth = colh0 < colh1 ? colh0 : colh1;
352 for(j=0; j<colhboth; j++) {
353 *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
354 fbptr += FBWIDTH / 2;
358 for(j=colhboth; j<colh0; j++) {
360 fbptr += FBWIDTH / 2;
363 for(j=colhboth; j<colh1; j++) {
364 *fbptr |= (uint16_t)grad[j] << 8;
365 fbptr += FBWIDTH / 2;