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 for(i=0; i<width*height; i++) {
120 vox->color[i] = cpix[i] & 0xffffff; /* discard alpha */
123 img_free_pixels(hpix);
124 img_free_pixels(cpix);
129 void vox_free(struct voxscape *vox)
140 void vox_fog(struct voxscape *vox, int zstart, uint32_t color)
144 vox->fogcolor[0] = color >> 16;
145 vox->fogcolor[1] = (color >> 8) & 0xff;
146 vox->fogcolor[2] = color & 0xff;
150 vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
152 vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
155 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
158 int h00, h01, h10, h11, h0, h1;
165 h01 = H(x, y + 0x10000);
166 h10 = H(x + 0x10000, y);
167 h11 = H(x + 0x10000, y + 0x10000);
172 h0 = XLERP(h00, h01, v, 16);
173 h1 = XLERP(h10, h11, v, 16);
174 return XLERP(h0, h1, u, 16);
177 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
180 uint32_t c00, c01, c10, c11, c0, c1;
187 c01 = C(x, y + 0x10000);
188 c10 = C(x + 0x10000, y);
189 c11 = C(x + 0x10000, y + 0x10000);
194 c0 = lerp_color(c00, c01, v);
195 c1 = lerp_color(c10, c11, v);
196 return lerp_color(c0, c1, u);
200 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
206 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb, int horizon)
208 if(xres != vox->fbwidth) {
210 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
211 fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
217 vox->fbheight = yres;
218 vox->horizon = horizon >= 0 ? horizon : vox->fbheight / 2;
221 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
224 h = vox_height(vox, x, y) - h;
232 vox->valid &= ~SLICELEN;
235 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
241 vox->nslices = vox->zfar - vox->znear;
243 if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
244 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
248 vox->valid &= ~SLICELEN;
252 * calculate extents of horizontal equidistant line from the viewer based on fov
253 * for each column step along this line and compute height for each pixel
254 * fill the visible (top) part of each column
257 void vox_render(struct voxscape *vox)
262 for(i=0; i<vox->nslices; i++) {
263 vox_render_slice(vox, i);
267 void vox_begin(struct voxscape *vox)
271 memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
273 if(!(vox->valid & SLICELEN)) {
274 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
275 for(i=0; i<vox->nslices; i++) {
276 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
278 vox->valid |= SLICELEN;
282 void vox_render_slice(struct voxscape *vox, int n)
284 int i, j, hval, colstart, colheight, z;
285 int32_t x, y, len, xstep, ystep, fog;
289 if(vox->zfog > 0 && n > vox->zfog) {
290 fog = ((n - vox->zfog) << 8) / (vox->zfar - vox->zfog);
297 len = vox->slicelen[n] >> 8;
298 xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
299 ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
301 x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
302 y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
303 for(i=0; i<vox->fbwidth; i++) {
304 hval = vox_height(vox, x, y) - vox->vheight;
305 hval = hval * 160 / (vox->znear + n) + vox->horizon;
306 if(hval > vox->fbheight) hval = vox->fbheight;
307 if(hval > vox->coltop[i]) {
308 color = vox_color(vox, x, y);
309 colstart = vox->fbheight - hval;
310 colheight = hval - vox->coltop[i];
311 fbptr = vox->fb + colstart * vox->fbwidth + i;
314 color = lerp_pcol_rgb(color, vox->fogcolor[0], vox->fogcolor[1], vox->fogcolor[2], fog);
317 for(j=0; j<colheight; j++) {
319 fbptr += vox->fbwidth;
321 vox->coltop[i] = hval;
329 void vox_sky_solid(struct voxscape *vox, uint32_t color)
334 for(i=0; i<vox->fbwidth; i++) {
336 colheight = vox->fbheight - vox->coltop[i];
337 for(j=0; j<colheight; j++) {
339 fbptr += vox->fbwidth;
344 void vox_sky_grad(struct voxscape *vox, uint32_t chor, uint32_t ctop)
346 int i, j, colheight, t;
347 int r0, g0, b0, r1, g1, b1;
348 int d = vox->fbheight - vox->horizon;
349 uint32_t *grad, *fbptr;
351 grad = alloca(vox->fbheight * sizeof *grad);
354 g0 = (ctop >> 8) & 0xff;
357 g1 = (chor >> 8) & 0xff;
362 grad[i] = lerp_rgb(r0, g0, b0, r1, g1, b1, t);
364 for(i=d; i<vox->fbheight; i++) {
368 for(i=0; i<vox->fbwidth; i++) {
370 colheight = vox->fbheight - vox->coltop[i];
371 for(j=0; j<colheight; j++) {
373 fbptr += vox->fbwidth;
378 static uint32_t lerp_rgb(int r0, int g0, int b0, int r1, int g1, int b1, int32_t t)
380 int r = XLERP(r0, r1, t, 16);
381 int g = XLERP(g0, g1, t, 16);
382 int b = XLERP(b0, b1, t, 16);
383 return (r << 16) | (g << 8) | b;
386 static uint32_t lerp_color(uint32_t ca, uint32_t cb, int32_t t)
388 int r0, g0, b0, r1, g1, b1;
391 g0 = (ca >> 8) & 0xff;
394 g1 = (cb >> 8) & 0xff;
397 return lerp_rgb(r0, g0, b0, r1, g1, b1, t);
400 static uint32_t lerp_pcol_rgb(uint32_t pcol, int r, int g, int b, int32_t t)
403 int g0 = (pcol >> 8) & 0xff;
404 int b0 = pcol & 0xff;
406 return lerp_rgb(r0, g0, b0, r, g, b, t);