16 unsigned char *height;
18 int xshift, xmask, ymask;
22 int fbwidth, fbheight;
38 struct voxscape *vox_create(int xsz, int ysz)
42 if(!(vox = calloc(1, sizeof *vox))) {
45 if(!(vox->height = calloc(1, xsz * ysz))) {
46 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
50 if(!(vox->color = calloc(xsz * ysz, sizeof *vox->color))) {
51 fprintf(stderr, "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
59 vox->xmask = vox->xsz - 1;
60 vox->ymask = vox->ysz - 1;
69 vox->proj_dist = 4; /* TODO */
74 struct voxscape *vox_open(const char *hfile, const char *cfile)
78 int width, height, cwidth, cheight;
81 if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
82 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
85 if(!(cpix = img_load_pixels(cfile, &cwidth, &cheight, IMG_FMT_RGBA32))) {
86 fprintf(stderr, "vox_open: failed to load color map: %s\n", cfile);
87 img_free_pixels(hpix);
90 if(cwidth != width || cheight != height) {
91 img_free_pixels(hpix);
92 img_free_pixels(cpix);
93 fprintf(stderr, "vox_open: %s and %s have different dimensions\n", hfile, cfile);
97 if(!(vox = vox_create(width, height))) {
98 img_free_pixels(hpix);
99 img_free_pixels(cpix);
102 memcpy(vox->height, hpix, width * height);
103 memcpy(vox->color, cpix, width * height * sizeof *vox->color);
105 img_free_pixels(hpix);
106 img_free_pixels(cpix);
111 void vox_free(struct voxscape *vox)
122 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb)
124 if(xres != vox->fbwidth) {
126 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
127 fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
133 vox->fbheight = yres;
136 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int32_t angle)
141 /* TODO precalc stuff */
143 vox->valid &= ~SLICELEN;
146 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
152 vox->nslices = vox->zfar - vox->znear;
154 if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
155 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
159 vox->valid &= ~SLICELEN;
163 * calculate extents of horizontal equidistant line from the viewer based on fov
164 * for each column step along this line and compute height for each pixel
165 * fill the visible (top) part of each column
168 void vox_render(struct voxscape *vox)
173 for(i=0; i<vox->nslices; i++) {
174 vox_render_slice(vox, i);
178 void vox_begin(struct voxscape *vox)
183 for(i=0; i<vox->fbwidth; i++) {
184 vox->coltop[i] = vox->fbheight;
187 memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
189 if(!(vox->valid & SLICELEN)) {
190 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
191 for(i=0; i<vox->nslices; i++) {
192 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 10.0f * 65536.0f);
194 vox->valid |= SLICELEN;
198 void vox_render_slice(struct voxscape *vox, int n)
200 int i, j, tx, ty, hval, colstart, colheight;
201 int32_t x, y, len, xstep;
205 len = vox->slicelen[n];
206 xstep = len / vox->fbwidth;
208 x = vox->x - xstep * (vox->fbwidth >> 1);
209 y = vox->y + ((vox->znear + n) << 16);
210 for(i=0; i<vox->fbwidth; i++) {
211 tx = (x >> 16) & vox->xmask;
212 ty = (y >> 16) & vox->ymask;
214 hval = vox->height[(ty << vox->xshift) + tx] - 80;
215 hval = hval * 80 / (vox->znear + n) + 250;
216 if(hval > vox->fbheight) hval = vox->fbheight;
217 if(hval > vox->coltop[i]) {
218 color = vox->color[(ty << vox->xshift) + tx];
219 colstart = vox->fbheight - hval;
220 colheight = hval - vox->coltop[i];
221 fbptr = vox->fb + colstart * vox->fbwidth + i;
223 for(j=0; j<colheight; j++) {
225 fbptr += vox->fbwidth;
227 vox->coltop[i] = hval;
234 void vox_sky_grad(struct voxscape *vox, uint32_t chor, uint32_t ctop)
236 int i, j, colheight, t;
237 int r0, g0, b0, r1, g1, b1, r, g, b;
238 int d = vox->fbheight - 250;
239 uint32_t *grad, *fbptr;
241 grad = alloca(vox->fbheight * sizeof *grad);
244 g0 = (ctop >> 8) & 0xff;
247 g1 = (chor >> 8) & 0xff;
252 r = ((r0 << 8) + (r1 - r0) * t) >> 8;
253 g = ((g0 << 8) + (g1 - g0) * t) >> 8;
254 b = ((b0 << 8) + (b1 - b0) * t) >> 8;
255 assert(r >= 0 && r < 256);
256 assert(g >= 0 && g < 256);
257 assert(b >= 0 && b < 256);
258 grad[i] = (r << 16) | (g << 8) | b;
260 for(i=d; i<vox->fbheight; i++) {
264 for(i=0; i<vox->fbwidth; i++) {
266 colheight = vox->fbheight - vox->coltop[i];
267 for(j=0; j<colheight; j++) {
269 fbptr += vox->fbwidth;