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;
32 int fbwidth, fbheight;
46 int zfog; /* fog start Z (0: no fog) */
52 struct voxscape *vox_create(int xsz, int ysz)
56 if(!(vox = calloc(1, sizeof *vox))) {
59 if(!(vox->height = calloc(1, xsz * ysz))) {
60 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
64 if(!(vox->color = calloc(xsz * ysz, sizeof *vox->color))) {
65 fprintf(stderr, "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
73 vox->xmask = vox->xsz - 1;
74 vox->ymask = vox->ysz - 1;
83 vox->proj_dist = 4; /* TODO */
88 struct voxscape *vox_open(const char *hfile, const char *cfile)
92 int i, width, height, cwidth, cheight;
95 if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
96 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
99 if(!(cpix = img_load_pixels(cfile, &cwidth, &cheight, IMG_FMT_RGBA32))) {
100 fprintf(stderr, "vox_open: failed to load color map: %s\n", cfile);
101 img_free_pixels(hpix);
104 if(cwidth != width || cheight != height) {
105 img_free_pixels(hpix);
106 img_free_pixels(cpix);
107 fprintf(stderr, "vox_open: %s and %s have different dimensions\n", hfile, cfile);
111 if(!(vox = vox_create(width, height))) {
112 img_free_pixels(hpix);
113 img_free_pixels(cpix);
117 memcpy(vox->height, hpix, width * height);
119 /* swap r/b and discard alpha while copying */
120 for(i=0; i<width*height; i++) {
121 vox->color[i] = (cpix[i] & 0xff00) | ((cpix[i] & 0xff) << 16) |
122 ((cpix[i] & 0xff0000) >> 16);
125 img_free_pixels(hpix);
126 img_free_pixels(cpix);
131 void vox_free(struct voxscape *vox)
142 void vox_fog(struct voxscape *vox, int zstart, uint32_t color)
146 vox->fogcolor[2] = color >> 16;
147 vox->fogcolor[1] = (color >> 8) & 0xff;
148 vox->fogcolor[0] = color & 0xff;
152 vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
154 vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
157 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
160 int h00, h01, h10, h11, h0, h1;
167 h01 = H(x, y + 0x10000);
168 h10 = H(x + 0x10000, y);
169 h11 = H(x + 0x10000, y + 0x10000);
174 h0 = XLERP(h00, h01, v, 16);
175 h1 = XLERP(h10, h11, v, 16);
176 return XLERP(h0, h1, u, 16);
179 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
182 uint32_t c00, c01, c10, c11, c0, c1;
189 c01 = C(x, y + 0x10000);
190 c10 = C(x + 0x10000, y);
191 c11 = C(x + 0x10000, y + 0x10000);
196 c0 = lerp_color(c00, c01, v);
197 c1 = lerp_color(c10, c11, v);
198 return lerp_color(c0, c1, u);
202 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
208 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb, int horizon)
210 if(xres != vox->fbwidth) {
212 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
213 fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
219 vox->fbheight = yres;
220 vox->horizon = horizon >= 0 ? horizon : vox->fbheight / 2;
223 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
226 h = vox_height(vox, x, y) - h;
234 vox->valid &= ~SLICELEN;
237 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
243 vox->nslices = vox->zfar - vox->znear;
245 if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
246 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
250 vox->valid &= ~SLICELEN;
254 * calculate extents of horizontal equidistant line from the viewer based on fov
255 * for each column step along this line and compute height for each pixel
256 * fill the visible (top) part of each column
259 void vox_render(struct voxscape *vox)
264 for(i=0; i<vox->nslices; i++) {
265 vox_render_slice(vox, i);
269 void vox_begin(struct voxscape *vox)
273 memset(vox->coltop, 0, vox->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);
280 vox->valid |= SLICELEN;
284 void vox_render_slice(struct voxscape *vox, int n)
286 int i, j, hval, colstart, colheight, z;
287 int32_t x, y, len, xstep, ystep, fog;
291 if(vox->zfog > 0 && n > vox->zfog) {
292 fog = ((n - vox->zfog) << 16) / (vox->zfar - vox->zfog);
299 len = vox->slicelen[n] >> 8;
300 xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
301 ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
303 x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
304 y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
305 for(i=0; i<vox->fbwidth; i++) {
306 hval = vox_height(vox, x, y) - vox->vheight;
307 hval = hval * 160 / (vox->znear + n) + vox->horizon;
308 if(hval > vox->fbheight) hval = vox->fbheight;
309 if(hval > vox->coltop[i]) {
310 color = vox_color(vox, x, y);
311 colstart = vox->fbheight - hval;
312 colheight = hval - vox->coltop[i];
313 fbptr = vox->fb + colstart * vox->fbwidth + i;
316 color = lerp_pcol_rgb(color, vox->fogcolor[0], vox->fogcolor[1], vox->fogcolor[2], fog);
319 for(j=0; j<colheight; j++) {
321 fbptr += vox->fbwidth;
323 vox->coltop[i] = hval;
331 void vox_sky_solid(struct voxscape *vox, uint32_t color)
336 for(i=0; i<vox->fbwidth; i++) {
338 colheight = vox->fbheight - vox->coltop[i];
339 for(j=0; j<colheight; j++) {
341 fbptr += vox->fbwidth;
346 void vox_sky_grad(struct voxscape *vox, uint32_t chor, uint32_t ctop)
348 int i, j, colheight, t;
349 int r0, g0, b0, r1, g1, b1;
350 int d = vox->fbheight - vox->horizon;
351 uint32_t *grad, *fbptr;
353 grad = alloca(vox->fbheight * sizeof *grad);
356 g0 = (ctop >> 8) & 0xff;
359 g1 = (chor >> 8) & 0xff;
364 grad[i] = lerp_rgb(r0, g0, b0, r1, g1, b1, t);
366 for(i=d; i<vox->fbheight; i++) {
370 for(i=0; i<vox->fbwidth; i++) {
372 colheight = vox->fbheight - vox->coltop[i];
373 for(j=0; j<colheight; j++) {
375 fbptr += vox->fbwidth;
380 static uint32_t lerp_rgb(int r0, int g0, int b0, int r1, int g1, int b1, int32_t t)
382 int r = XLERP(r0, r1, t, 16);
383 int g = XLERP(g0, g1, t, 16);
384 int b = XLERP(b0, b1, t, 16);
385 return (b << 16) | (g << 8) | r;
388 static uint32_t lerp_color(uint32_t ca, uint32_t cb, int32_t t)
390 int r0, g0, b0, r1, g1, b1;
393 g0 = (ca >> 8) & 0xff;
396 g1 = (cb >> 8) & 0xff;
399 return lerp_rgb(r0, g0, b0, r1, g1, b1, t);
402 static uint32_t lerp_pcol_rgb(uint32_t pcol, int r, int g, int b, int32_t t)
405 int g0 = (pcol >> 8) & 0xff;
406 int r0 = pcol & 0xff;
408 return lerp_rgb(r0, g0, b0, r, g, b, t);