17 unsigned char *height;
19 int xshift, xmask, ymask;
23 int fbwidth, fbheight;
37 int zfog; /* fog start Z (0: no fog) */
43 struct voxscape *vox_create(int xsz, int ysz)
47 if(!(vox = calloc(1, sizeof *vox))) {
50 if(!(vox->height = calloc(1, xsz * ysz))) {
51 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
55 if(!(vox->color = calloc(xsz * ysz, sizeof *vox->color))) {
56 fprintf(stderr, "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
64 vox->xmask = vox->xsz - 1;
65 vox->ymask = vox->ysz - 1;
74 vox->proj_dist = 4; /* TODO */
79 struct voxscape *vox_open(const char *hfile, const char *cfile)
83 int i, width, height, cwidth, cheight;
86 if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
87 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
90 if(!(cpix = img_load_pixels(cfile, &cwidth, &cheight, IMG_FMT_RGBA32))) {
91 fprintf(stderr, "vox_open: failed to load color map: %s\n", cfile);
92 img_free_pixels(hpix);
95 if(cwidth != width || cheight != height) {
96 img_free_pixels(hpix);
97 img_free_pixels(cpix);
98 fprintf(stderr, "vox_open: %s and %s have different dimensions\n", hfile, cfile);
102 if(!(vox = vox_create(width, height))) {
103 img_free_pixels(hpix);
104 img_free_pixels(cpix);
108 memcpy(vox->height, hpix, width * height);
110 for(i=0; i<width*height; i++) {
111 vox->color[i] = cpix[i] & 0xffffff; /* discard alpha */
114 img_free_pixels(hpix);
115 img_free_pixels(cpix);
120 void vox_free(struct voxscape *vox)
131 void vox_fog(struct voxscape *vox, int zstart, uint32_t color)
135 vox->fogcolor[0] = color >> 16;
136 vox->fogcolor[1] = (color >> 8) & 0xff;
137 vox->fogcolor[2] = color & 0xff;
140 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
142 int tx = (x >> 16) & vox->xmask;
143 int ty = (y >> 16) & vox->ymask;
145 return vox->height[(ty << vox->xshift) + tx];
148 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb, int horizon)
150 if(xres != vox->fbwidth) {
152 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
153 fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
159 vox->fbheight = yres;
160 vox->horizon = horizon >= 0 ? horizon : vox->fbheight / 2;
163 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
166 h = vox_height(vox, x, y) - h;
174 vox->valid &= ~SLICELEN;
177 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
183 vox->nslices = vox->zfar - vox->znear;
185 if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
186 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
190 vox->valid &= ~SLICELEN;
194 * calculate extents of horizontal equidistant line from the viewer based on fov
195 * for each column step along this line and compute height for each pixel
196 * fill the visible (top) part of each column
199 void vox_render(struct voxscape *vox)
204 for(i=0; i<vox->nslices; i++) {
205 vox_render_slice(vox, i);
209 void vox_begin(struct voxscape *vox)
213 memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
215 if(!(vox->valid & SLICELEN)) {
216 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
217 for(i=0; i<vox->nslices; i++) {
218 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
220 vox->valid |= SLICELEN;
224 void vox_render_slice(struct voxscape *vox, int n)
226 int i, j, tx, ty, hval, colstart, colheight, z, r, g, b;
227 int32_t x, y, len, xstep, ystep, fog;
231 if(vox->zfog > 0 && n > vox->zfog) {
232 fog = ((n - vox->zfog) << 8) / (vox->zfar - vox->zfog);
239 len = vox->slicelen[n] >> 8;
240 xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
241 ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
243 x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
244 y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
245 for(i=0; i<vox->fbwidth; i++) {
246 tx = (x >> 16) & vox->xmask;
247 ty = (y >> 16) & vox->ymask;
249 hval = vox->height[(ty << vox->xshift) + tx] - vox->vheight;
250 hval = hval * 160 / (vox->znear + n) + vox->horizon;
251 if(hval > vox->fbheight) hval = vox->fbheight;
252 if(hval > vox->coltop[i]) {
253 color = vox->color[(ty << vox->xshift) + tx];
254 colstart = vox->fbheight - hval;
255 colheight = hval - vox->coltop[i];
256 fbptr = vox->fb + colstart * vox->fbwidth + i;
260 g = (color >> 8) & 0xff;
262 r = ((r << 8) + (vox->fogcolor[0] - r) * fog) >> 8;
263 g = ((g << 8) + (vox->fogcolor[1] - g) * fog) >> 8;
264 b = ((b << 8) + (vox->fogcolor[2] - b) * fog) >> 8;
265 color = (r << 16) | (g << 8) | b;
268 for(j=0; j<colheight; j++) {
270 fbptr += vox->fbwidth;
272 vox->coltop[i] = hval;
280 void vox_sky_solid(struct voxscape *vox, uint32_t color)
285 for(i=0; i<vox->fbwidth; i++) {
287 colheight = vox->fbheight - vox->coltop[i];
288 for(j=0; j<colheight; j++) {
290 fbptr += vox->fbwidth;
295 void vox_sky_grad(struct voxscape *vox, uint32_t chor, uint32_t ctop)
297 int i, j, colheight, t;
298 int r0, g0, b0, r1, g1, b1, r, g, b;
299 int d = vox->fbheight - vox->horizon;
300 uint32_t *grad, *fbptr;
302 grad = alloca(vox->fbheight * sizeof *grad);
305 g0 = (ctop >> 8) & 0xff;
308 g1 = (chor >> 8) & 0xff;
313 r = ((r0 << 8) + (r1 - r0) * t) >> 8;
314 g = ((g0 << 8) + (g1 - g0) * t) >> 8;
315 b = ((b0 << 8) + (b1 - b0) * t) >> 8;
316 assert(r >= 0 && r < 256);
317 assert(g >= 0 && g < 256);
318 assert(b >= 0 && b < 256);
319 grad[i] = (r << 16) | (g << 8) | b;
321 for(i=d; i<vox->fbheight; i++) {
325 for(i=0; i<vox->fbwidth; i++) {
327 colheight = vox->fbheight - vox->coltop[i];
328 for(j=0; j<colheight; j++) {
330 fbptr += vox->fbwidth;