11 #define XLERP(a, b, t, fp) \
12 ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
14 static uint32_t lerp_rgb(int r0, int g0, int b0, int r1, int g1, int b1, int32_t t);
15 static uint32_t lerp_color(uint32_t ca, uint32_t cb, int32_t t);
16 static uint32_t lerp_pcol_rgb(uint32_t pcol, int r, int g, int b, int32_t t);
24 unsigned char *height;
26 int xshift, xmask, ymask;
34 int fbwidth, fbheight;
48 int zfog; /* fog start Z (0: no fog) */
54 struct voxscape *vox_create(int xsz, int ysz)
58 if(!(vox = calloc(1, sizeof *vox))) {
61 if(!(vox->height = calloc(1, xsz * ysz))) {
62 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
66 if(!(vox->color = calloc(xsz * ysz, sizeof *vox->color))) {
67 fprintf(stderr, "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
75 vox->xmask = vox->xsz - 1;
76 vox->ymask = vox->ysz - 1;
90 struct voxscape *vox_open(const char *hfile, const char *cfile)
94 int i, width, height, cwidth, cheight;
97 if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
98 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
101 if(!(cpix = img_load_pixels(cfile, &cwidth, &cheight, IMG_FMT_RGBA32))) {
102 fprintf(stderr, "vox_open: failed to load color map: %s\n", cfile);
103 img_free_pixels(hpix);
106 if(cwidth != width || cheight != height) {
107 img_free_pixels(hpix);
108 img_free_pixels(cpix);
109 fprintf(stderr, "vox_open: %s and %s have different dimensions\n", hfile, cfile);
113 if(!(vox = vox_create(width, height))) {
114 img_free_pixels(hpix);
115 img_free_pixels(cpix);
119 memcpy(vox->height, hpix, width * height);
121 /* swap r/b and discard alpha while copying */
122 for(i=0; i<width*height; i++) {
123 vox->color[i] = (cpix[i] & 0xff00) | ((cpix[i] & 0xff) << 16) |
124 ((cpix[i] & 0xff0000) >> 16);
127 img_free_pixels(hpix);
128 img_free_pixels(cpix);
133 void vox_free(struct voxscape *vox)
144 void vox_fog(struct voxscape *vox, int zstart, uint32_t color)
148 vox->fogcolor[2] = color >> 16;
149 vox->fogcolor[1] = (color >> 8) & 0xff;
150 vox->fogcolor[0] = color & 0xff;
154 vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
156 vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
159 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
162 int h00, h01, h10, h11, h0, h1;
169 h01 = H(x, y + 0x10000);
170 h10 = H(x + 0x10000, y);
171 h11 = H(x + 0x10000, y + 0x10000);
176 h0 = XLERP(h00, h01, v, 16);
177 h1 = XLERP(h10, h11, v, 16);
178 return XLERP(h0, h1, u, 16);
181 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
184 uint32_t c00, c01, c10, c11, c0, c1;
191 c01 = C(x, y + 0x10000);
192 c10 = C(x + 0x10000, y);
193 c11 = C(x + 0x10000, y + 0x10000);
198 c0 = lerp_color(c00, c01, v);
199 c1 = lerp_color(c10, c11, v);
200 return lerp_color(c0, c1, u);
204 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
210 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb)
212 if(xres != vox->fbwidth) {
214 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
215 fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
221 vox->fbheight = yres;
224 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle, int32_t vangle)
227 h = vox_height(vox, x, y) - h;
235 vox->horizon = (vox->fbheight * ((vangle + 0x10000) >> 1)) >> 16;
237 vox->valid &= ~SLICELEN;
240 void vox_proj(struct voxscape *vox, int hscale, int fov, int znear, int zfar)
245 vox->hscale = hscale;
247 vox->nslices = vox->zfar - vox->znear;
249 if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
250 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
254 if(!(vox->projlut = malloc(vox->nslices * sizeof *vox->projlut))) {
255 fprintf(stderr, "vox_proj: failed to allocate projection table (%d)\n", vox->nslices);
260 vox->valid &= ~SLICELEN;
264 * calculate extents of horizontal equidistant line from the viewer based on fov
265 * for each column step along this line and compute height for each pixel
266 * fill the visible (top) part of each column
269 void vox_render(struct voxscape *vox)
274 for(i=0; i<vox->nslices; i++) {
275 vox_render_slice(vox, i);
279 void vox_begin(struct voxscape *vox)
283 memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
285 if(!(vox->valid & SLICELEN)) {
286 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
287 for(i=0; i<vox->nslices; i++) {
288 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
289 vox->projlut[i] = (vox->hscale << 8) / (vox->znear + i);
291 vox->valid |= SLICELEN;
295 void vox_render_slice(struct voxscape *vox, int n)
297 int i, j, hval, colstart, colheight, z;
298 int32_t x, y, len, xstep, ystep, fog;
302 if(vox->zfog > 0 && n > vox->zfog) {
303 fog = ((n - vox->zfog) << 16) / (vox->zfar - vox->zfog);
310 len = vox->slicelen[n] >> 8;
311 xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
312 ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
314 x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
315 y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
316 for(i=0; i<vox->fbwidth; i++) {
317 hval = vox_height(vox, x, y) - vox->vheight;
318 hval = ((hval * vox->projlut[n]) >> 8) + vox->horizon;
319 if(hval > vox->fbheight) hval = vox->fbheight;
320 if(hval > vox->coltop[i]) {
321 color = vox_color(vox, x, y);
322 colstart = vox->fbheight - hval;
323 colheight = hval - vox->coltop[i];
324 fbptr = vox->fb + colstart * vox->fbwidth + i;
327 color = lerp_pcol_rgb(color, vox->fogcolor[0], vox->fogcolor[1], vox->fogcolor[2], fog);
330 for(j=0; j<colheight; j++) {
332 fbptr += vox->fbwidth;
334 vox->coltop[i] = hval;
342 void vox_sky_solid(struct voxscape *vox, uint32_t color)
347 for(i=0; i<vox->fbwidth; i++) {
349 colheight = vox->fbheight - vox->coltop[i];
350 for(j=0; j<colheight; j++) {
352 fbptr += vox->fbwidth;
357 void vox_sky_grad(struct voxscape *vox, uint32_t chor, uint32_t ctop)
359 int i, j, colheight, t;
360 int r0, g0, b0, r1, g1, b1;
361 int d = vox->fbheight - vox->horizon;
362 uint32_t *grad, *fbptr;
364 grad = alloca(vox->fbheight * sizeof *grad);
367 g0 = (ctop >> 8) & 0xff;
370 g1 = (chor >> 8) & 0xff;
374 if(i >= vox->fbheight) break;
376 grad[i] = lerp_rgb(r0, g0, b0, r1, g1, b1, t);
378 for(i=d; i<vox->fbheight; i++) {
382 for(i=0; i<vox->fbwidth; i++) {
384 colheight = vox->fbheight - vox->coltop[i];
385 for(j=0; j<colheight; j++) {
387 fbptr += vox->fbwidth;
392 static uint32_t lerp_rgb(int r0, int g0, int b0, int r1, int g1, int b1, int32_t t)
394 int r = XLERP(r0, r1, t, 16);
395 int g = XLERP(g0, g1, t, 16);
396 int b = XLERP(b0, b1, t, 16);
397 return (b << 16) | (g << 8) | r;
400 static uint32_t lerp_color(uint32_t ca, uint32_t cb, int32_t t)
402 int r0, g0, b0, r1, g1, b1;
405 g0 = (ca >> 8) & 0xff;
408 g1 = (cb >> 8) & 0xff;
411 return lerp_rgb(r0, g0, b0, r1, g1, b1, t);
414 static uint32_t lerp_pcol_rgb(uint32_t pcol, int r, int g, int b, int32_t t)
417 int g0 = (pcol >> 8) & 0xff;
418 int r0 = pcol & 0xff;
420 return lerp_rgb(r0, g0, b0, r, g, b, t);