17 unsigned char *height;
19 int xshift, xmask, ymask;
23 int fbwidth, fbheight;
39 struct voxscape *vox_create(int xsz, int ysz)
43 if(!(vox = calloc(1, sizeof *vox))) {
46 if(!(vox->height = calloc(1, xsz * ysz))) {
47 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
51 if(!(vox->color = calloc(xsz * ysz, sizeof *vox->color))) {
52 fprintf(stderr, "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
60 vox->xmask = vox->xsz - 1;
61 vox->ymask = vox->ysz - 1;
70 vox->proj_dist = 4; /* TODO */
75 struct voxscape *vox_open(const char *hfile, const char *cfile)
79 int width, height, cwidth, cheight;
82 if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
83 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
86 if(!(cpix = img_load_pixels(cfile, &cwidth, &cheight, IMG_FMT_RGBA32))) {
87 fprintf(stderr, "vox_open: failed to load color map: %s\n", cfile);
88 img_free_pixels(hpix);
91 if(cwidth != width || cheight != height) {
92 img_free_pixels(hpix);
93 img_free_pixels(cpix);
94 fprintf(stderr, "vox_open: %s and %s have different dimensions\n", hfile, cfile);
98 if(!(vox = vox_create(width, height))) {
99 img_free_pixels(hpix);
100 img_free_pixels(cpix);
103 memcpy(vox->height, hpix, width * height);
104 memcpy(vox->color, cpix, width * height * sizeof *vox->color);
106 img_free_pixels(hpix);
107 img_free_pixels(cpix);
112 void vox_free(struct voxscape *vox)
123 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb)
125 if(xres != vox->fbwidth) {
127 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
128 fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
134 vox->fbheight = yres;
137 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int32_t angle)
142 /* TODO precalc stuff */
144 vox->valid &= ~SLICELEN;
147 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
153 vox->nslices = vox->zfar - vox->znear;
155 if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
156 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
160 vox->valid &= ~SLICELEN;
164 * calculate extents of horizontal equidistant line from the viewer based on fov
165 * for each column step along this line and compute height for each pixel
166 * fill the visible (top) part of each column
169 void vox_render(struct voxscape *vox)
174 for(i=0; i<vox->nslices; i++) {
175 vox_render_slice(vox, i);
179 void vox_begin(struct voxscape *vox)
183 memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
185 if(!(vox->valid & SLICELEN)) {
186 float theta = (float)vox->fov * M_PI / 360.0f; /* half angle */
187 for(i=0; i<vox->nslices; i++) {
188 vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
190 vox->valid |= SLICELEN;
194 void vox_render_slice(struct voxscape *vox, int n)
196 int i, j, tx, ty, hval, colstart, colheight, z;
197 int32_t x, y, len, xstep, ystep;
203 len = vox->slicelen[n] >> 8;
204 xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
205 ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
207 x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
208 y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
209 for(i=0; i<vox->fbwidth; i++) {
210 tx = (x >> 16) & vox->xmask;
211 ty = (y >> 16) & vox->ymask;
213 hval = vox->height[(ty << vox->xshift) + tx] - 80;
214 hval = hval * 160 / (vox->znear + n) + 250;
215 if(hval > vox->fbheight) hval = vox->fbheight;
216 if(hval > vox->coltop[i]) {
217 color = vox->color[(ty << vox->xshift) + tx];
218 colstart = vox->fbheight - hval;
219 colheight = hval - vox->coltop[i];
220 fbptr = vox->fb + colstart * vox->fbwidth + i;
222 for(j=0; j<colheight; j++) {
224 fbptr += vox->fbwidth;
226 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;