a23a851eaa1fa734fd6a878c06b099b19bafb551
[voxscape] / src / voxscape.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <math.h>
6 #include <assert.h>
7 #include <imago2.h>
8 #include "voxscape.h"
9 #include "lut.h"
10
11 enum {
12         SLICELEN        = 1
13 };
14
15 struct voxscape {
16         int xsz, ysz;
17         unsigned char *height;
18         uint32_t *color;
19         int xshift, xmask, ymask;
20
21         /* framebuffer */
22         uint32_t *fb;
23         int fbwidth, fbheight;
24         int *coltop;
25
26         /* view */
27         int32_t x, y, angle;
28         int vheight;
29
30         /* projection */
31         int fov, znear, zfar;
32         int nslices;
33         int32_t *slicelen;
34         int proj_dist;
35
36         unsigned int valid;
37 };
38
39 struct voxscape *vox_create(int xsz, int ysz)
40 {
41         struct voxscape *vox;
42
43         if(!(vox = calloc(1, sizeof *vox))) {
44                 return 0;
45         }
46         if(!(vox->height = calloc(1, xsz * ysz))) {
47                 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
48                 free(vox);
49                 return 0;
50         }
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);
53                 free(vox->height);
54                 free(vox);
55                 return 0;
56         }
57         vox->xsz = xsz;
58         vox->ysz = ysz;
59
60         vox->xmask = vox->xsz - 1;
61         vox->ymask = vox->ysz - 1;
62
63         vox->xshift = -1;
64         while(xsz) {
65                 xsz >>= 1;
66                 vox->xshift++;
67         }
68
69         vox->vheight = 80;
70         vox->proj_dist = 4;     /* TODO */
71
72         return vox;
73 }
74
75 struct voxscape *vox_open(const char *hfile, const char *cfile)
76 {
77         unsigned char *hpix;
78         uint32_t *cpix;
79         int width, height, cwidth, cheight;
80         struct voxscape *vox;
81
82         if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
83                 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
84                 return 0;
85         }
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);
89                 return 0;
90         }
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);
95                 return 0;
96         }
97
98         if(!(vox = vox_create(width, height))) {
99                 img_free_pixels(hpix);
100                 img_free_pixels(cpix);
101                 return 0;
102         }
103         memcpy(vox->height, hpix, width * height);
104         memcpy(vox->color, cpix, width * height * sizeof *vox->color);
105
106         img_free_pixels(hpix);
107         img_free_pixels(cpix);
108
109         return vox;
110 }
111
112 void vox_free(struct voxscape *vox)
113 {
114         if(!vox) return;
115
116         free(vox->color);
117         free(vox->height);
118         free(vox->coltop);
119         free(vox->slicelen);
120         free(vox);
121 }
122
123 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
124 {
125         int tx = (x >> 16) & vox->xmask;
126         int ty = (y >> 16) & vox->ymask;
127
128         return vox->height[(ty << vox->xshift) + tx];
129 }
130
131 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb)
132 {
133         if(xres != vox->fbwidth) {
134                 free(vox->coltop);
135                 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
136                         fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
137                         return;
138                 }
139         }
140         vox->fb = fb;
141         vox->fbwidth = xres;
142         vox->fbheight = yres;
143 }
144
145 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
146 {
147         if(h < 0) {
148                 h = vox_height(vox, x, y) - h;
149         }
150
151         vox->x = x;
152         vox->y = y;
153         vox->vheight = h;
154         vox->angle = angle;
155
156         vox->valid &= ~SLICELEN;
157 }
158
159 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
160 {
161         vox->fov = fov;
162         vox->znear = znear;
163         vox->zfar = zfar;
164
165         vox->nslices = vox->zfar - vox->znear;
166         free(vox->slicelen);
167         if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
168                 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
169                 return;
170         }
171
172         vox->valid &= ~SLICELEN;
173 }
174
175 /* algorithm:
176  * calculate extents of horizontal equidistant line from the viewer based on fov
177  * for each column step along this line and compute height for each pixel
178  * fill the visible (top) part of each column
179  */
180
181 void vox_render(struct voxscape *vox)
182 {
183         int i;
184
185         vox_begin(vox);
186         for(i=0; i<vox->nslices; i++) {
187                 vox_render_slice(vox, i);
188         }
189 }
190
191 void vox_begin(struct voxscape *vox)
192 {
193         int i;
194
195         memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
196
197         if(!(vox->valid & SLICELEN)) {
198                 float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
199                 for(i=0; i<vox->nslices; i++) {
200                         vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
201                 }
202                 vox->valid |= SLICELEN;
203         }
204 }
205
206 void vox_render_slice(struct voxscape *vox, int n)
207 {
208         int i, j, tx, ty, hval, colstart, colheight, z;
209         int32_t x, y, len, xstep, ystep;
210         uint32_t color;
211         uint32_t *fbptr;
212
213         z = vox->znear + n;
214
215         len = vox->slicelen[n] >> 8;
216         xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
217         ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
218
219         x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
220         y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
221         for(i=0; i<vox->fbwidth; i++) {
222                 tx = (x >> 16) & vox->xmask;
223                 ty = (y >> 16) & vox->ymask;
224
225                 hval = vox->height[(ty << vox->xshift) + tx] - vox->vheight;
226                 hval = hval * 160 / (vox->znear + n) + 250;
227                 if(hval > vox->fbheight) hval = vox->fbheight;
228                 if(hval > vox->coltop[i]) {
229                         color = vox->color[(ty << vox->xshift) + tx];
230                         colstart = vox->fbheight - hval;
231                         colheight = hval - vox->coltop[i];
232                         fbptr = vox->fb + colstart * vox->fbwidth + i;
233
234                         for(j=0; j<colheight; j++) {
235                                 *fbptr = color;
236                                 fbptr += vox->fbwidth;
237                         }
238                         vox->coltop[i] = hval;
239                 }
240
241                 x += xstep;
242                 y += ystep;
243         }
244 }
245
246 void vox_sky_grad(struct voxscape *vox, uint32_t chor, uint32_t ctop)
247 {
248         int i, j, colheight, t;
249         int r0, g0, b0, r1, g1, b1, r, g, b;
250         int d = vox->fbheight - 250;
251         uint32_t *grad, *fbptr;
252
253         grad = alloca(vox->fbheight * sizeof *grad);
254
255         r0 = ctop >> 16;
256         g0 = (ctop >> 8) & 0xff;
257         b0 = ctop & 0xff;
258         r1 = chor >> 16;
259         g1 = (chor >> 8) & 0xff;
260         b1 = chor & 0xff;
261
262         for(i=0; i<d; i++) {
263                 t = (i << 8) / d;
264                 r = ((r0 << 8) + (r1 - r0) * t) >> 8;
265                 g = ((g0 << 8) + (g1 - g0) * t) >> 8;
266                 b = ((b0 << 8) + (b1 - b0) * t) >> 8;
267                 assert(r >= 0 && r < 256);
268                 assert(g >= 0 && g < 256);
269                 assert(b >= 0 && b < 256);
270                 grad[i] = (r << 16) | (g << 8) | b;
271         }
272         for(i=d; i<vox->fbheight; i++) {
273                 grad[i] = chor;
274         }
275
276         for(i=0; i<vox->fbwidth; i++) {
277                 fbptr = vox->fb + i;
278                 colheight = vox->fbheight - vox->coltop[i];
279                 for(j=0; j<colheight; j++) {
280                         *fbptr = grad[j];
281                         fbptr += vox->fbwidth;
282                 }
283         }
284 }