11 #define XLERP(a, b, t, fp) \
12 ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
20 unsigned char *height;
22 int xshift, xmask, ymask;
28 int fbwidth, fbheight;
42 int zfog; /* fog start Z (0: no fog) */
48 struct voxscape *vox_create(int xsz, int ysz)
52 if(!(vox = calloc(1, sizeof *vox))) {
55 if(!(vox->height = calloc(xsz * ysz, 1))) {
56 panic(get_pc(), "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
58 if(!(vox->color = calloc(xsz * ysz, 1))) {
59 panic(get_pc(), "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 void vox_free(struct voxscape *vox)
90 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
93 memcpy(vox->color, data, vox->xsz * vox->ysz);
98 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
101 memcpy(vox->height, data, vox->xsz * vox->ysz);
106 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
109 vox->fogcolor = color;
113 vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
115 vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
118 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
121 int h00, h01, h10, h11, h0, h1;
128 h01 = H(x, y + 0x10000);
129 h10 = H(x + 0x10000, y);
130 h11 = H(x + 0x10000, y + 0x10000);
135 h0 = XLERP(h00, h01, v, 16);
136 h1 = XLERP(h10, h11, v, 16);
137 return XLERP(h0, h1, u, 16);
140 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
143 int c00, c01, c10, c11, c0, c1;
150 c01 = C(x, y + 0x10000);
151 c10 = C(x + 0x10000, y);
152 c11 = C(x + 0x10000, y + 0x10000);
157 c0 = XLERP(c00, c01, v, 16);
158 c1 = XLERP(c10, c11, v, 16);
159 return XLERP(c0, c1, u, 16);
163 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
169 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
171 if(xres != vox->fbwidth) {
173 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
174 fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
180 vox->fbheight = yres;
181 vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
184 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
187 h = vox_height(vox, x, y) - h;
195 vox->valid &= ~SLICELEN;
198 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
204 vox->nslices = vox->zfar - vox->znear;
206 if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
207 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
211 vox->valid &= ~SLICELEN;
215 * calculate extents of horizontal equidistant line from the viewer based on fov
216 * for each column step along this line and compute height for each pixel
217 * fill the visible (top) part of each column
220 void vox_render(struct voxscape *vox)
225 for(i=0; i<vox->nslices; i++) {
226 vox_render_slice(vox, i);
230 void vox_begin(struct voxscape *vox)
234 memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
236 if(!(vox->valid & SLICELEN)) {
237 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
238 for(i=0; i<vox->nslices; i++) {
239 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
241 vox->valid |= SLICELEN;
245 void vox_render_slice(struct voxscape *vox, int n)
247 int i, j, hval, colstart, colheight, z;
248 int32_t x, y, len, xstep, ystep;
254 len = vox->slicelen[n] >> 8;
255 xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
256 ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
258 x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
259 y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
260 /* TODO double column */
261 for(i=0; i<vox->fbwidth/2; i++) {
262 hval = vox_height(vox, x, y) - vox->vheight;
263 hval = hval * 160 / (vox->znear + n) + vox->horizon;
264 if(hval > vox->fbheight) hval = vox->fbheight;
265 if(hval > vox->coltop[i]) {
266 color = vox_color(vox, x, y);
267 colstart = vox->fbheight - hval;
268 colheight = hval - vox->coltop[i];
269 fbptr = vox->fb + colstart * vox->fbwidth + i;
271 for(j=0; j<colheight; j++) {
272 *fbptr = color | ((uint16_t)color << 8);
273 fbptr += vox->fbwidth >> 1;
275 vox->coltop[i] = hval;
283 void vox_sky_solid(struct voxscape *vox, uint8_t color)
288 /* TODO double columns */
289 for(i=0; i<vox->fbwidth/2; i++) {
291 colheight = vox->fbheight - vox->coltop[i];
292 for(j=0; j<colheight; j++) {
293 *fbptr = color | ((uint16_t)color << 8);
294 fbptr += vox->fbwidth >> 1;
299 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
301 int i, j, colheight, t;
302 int d = vox->fbheight - vox->horizon;
306 grad = alloca(vox->fbheight * sizeof *grad);
310 grad[i] = XLERP(ctop, chor, t, 8);
312 for(i=d; i<vox->fbheight; i++) {
316 /* TODO double columns */
317 for(i=0; i<vox->fbwidth/2; i++) {
319 colheight = vox->fbheight - vox->coltop[i];
320 for(j=0; j<colheight; j++) {
321 *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
322 fbptr += vox->fbwidth >> 1;