15 unsigned char *height;
17 int xshift, xmask, ymask;
21 int fbwidth, fbheight;
37 struct voxscape *vox_create(int xsz, int ysz)
41 if(!(vox = calloc(1, sizeof *vox))) {
44 if(!(vox->height = calloc(1, xsz * ysz))) {
45 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
49 if(!(vox->color = calloc(xsz * ysz, sizeof *vox->color))) {
50 fprintf(stderr, "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
58 vox->xmask = vox->xsz - 1;
59 vox->ymask = vox->ysz - 1;
67 vox->proj_dist = 2; /* TODO */
72 struct voxscape *vox_open(const char *hfile, const char *cfile)
76 int width, height, cwidth, cheight;
79 if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
80 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
83 if(!(cpix = img_load_pixels(cfile, &cwidth, &cheight, IMG_FMT_RGBA32))) {
84 fprintf(stderr, "vox_open: failed to load color map: %s\n", cfile);
85 img_free_pixels(hpix);
88 if(cwidth != width || cheight != height) {
89 img_free_pixels(hpix);
90 img_free_pixels(cpix);
91 fprintf(stderr, "vox_open: %s and %s have different dimensions\n", hfile, cfile);
95 if(!(vox = vox_create(width, height))) {
96 img_free_pixels(hpix);
97 img_free_pixels(cpix);
100 memcpy(vox->height, hpix, width * height);
101 memcpy(vox->color, cpix, width * height * sizeof *vox->color);
103 img_free_pixels(hpix);
104 img_free_pixels(cpix);
109 void vox_free(struct voxscape *vox)
120 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb)
122 if(xres != vox->fbwidth) {
124 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
125 fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
131 vox->fbheight = yres;
134 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int32_t angle)
139 /* TODO precalc stuff */
141 vox->valid &= ~SLICELEN;
144 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
150 vox->nslices = vox->zfar - vox->znear;
152 if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
153 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
157 vox->valid &= ~SLICELEN;
161 * calculate extents of horizontal equidistant line from the viewer based on fov
162 * for each column step along this line and compute height for each pixel
163 * fill the visible (top) part of each column
166 void vox_render(struct voxscape *vox)
171 for(i=0; i<vox->nslices; i++) {
172 vox_render_slice(vox, i);
176 void vox_begin(struct voxscape *vox)
181 for(i=0; i<vox->fbwidth; i++) {
182 vox->coltop[i] = vox->fbheight;
185 memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
187 if(!(vox->valid & SLICELEN)) {
188 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
189 for(i=0; i<vox->nslices; i++) {
190 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 2.0f * 65536.0f);
192 vox->valid |= SLICELEN;
196 void vox_render_slice(struct voxscape *vox, int n)
198 int i, j, tx, ty, hval, colstart, colheight;
199 int32_t x, y, len, xstep;
203 len = vox->slicelen[n];
204 xstep = len / vox->fbwidth;
206 x = vox->x - xstep * (vox->fbwidth >> 1);
207 y = vox->y + (vox->znear << 16);
208 for(i=0; i<vox->fbwidth; i++) {
209 tx = (x >> 16) & vox->xmask;
210 ty = (y >> 16) & vox->ymask;
212 hval = vox->height[(ty << vox->xshift) + tx];
213 hval = (hval - vox->vheight) * vox->proj_dist / (vox->znear + i);
214 if(hval > vox->coltop[i]) {
215 color = vox->color[(ty << vox->xshift) + tx];
216 colstart = vox->fbheight - 1 - hval;
217 colheight = hval - vox->coltop[i];
218 fbptr = vox->fb + colstart * vox->fbwidth + i;
220 for(j=0; j<colheight; j++) {
222 fbptr += vox->fbwidth;