fix red/blue order (imago reads BGRA, output expects RGBA)
[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 #define XLERP(a, b, t, fp) \
12         ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
13
14 static uint32_t lerp_rgb(int r0, int g0, int b0, int r1, int g1, int b1, int32_t t);
15 static uint32_t lerp_color(uint32_t ca, uint32_t cb, int32_t t);
16 static uint32_t lerp_pcol_rgb(uint32_t pcol, int r, int g, int b, int32_t t);
17
18 enum {
19         SLICELEN        = 1
20 };
21
22 struct voxscape {
23         int xsz, ysz;
24         unsigned char *height;
25         uint32_t *color;
26         int xshift, xmask, ymask;
27
28         int hfilt, cfilt;
29
30         /* framebuffer */
31         uint32_t *fb;
32         int fbwidth, fbheight;
33         int *coltop;
34         int horizon;
35
36         /* view */
37         int32_t x, y, angle;
38         int vheight;
39
40         /* projection */
41         int fov, znear, zfar;
42         int nslices;
43         int32_t *slicelen;
44         int proj_dist;
45
46         int zfog;       /* fog start Z (0: no fog) */
47         int fogcolor[3];
48
49         unsigned int valid;
50 };
51
52 struct voxscape *vox_create(int xsz, int ysz)
53 {
54         struct voxscape *vox;
55
56         if(!(vox = calloc(1, sizeof *vox))) {
57                 return 0;
58         }
59         if(!(vox->height = calloc(1, xsz * ysz))) {
60                 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
61                 free(vox);
62                 return 0;
63         }
64         if(!(vox->color = calloc(xsz * ysz, sizeof *vox->color))) {
65                 fprintf(stderr, "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
66                 free(vox->height);
67                 free(vox);
68                 return 0;
69         }
70         vox->xsz = xsz;
71         vox->ysz = ysz;
72
73         vox->xmask = vox->xsz - 1;
74         vox->ymask = vox->ysz - 1;
75
76         vox->xshift = -1;
77         while(xsz) {
78                 xsz >>= 1;
79                 vox->xshift++;
80         }
81
82         vox->vheight = 80;
83         vox->proj_dist = 4;     /* TODO */
84
85         return vox;
86 }
87
88 struct voxscape *vox_open(const char *hfile, const char *cfile)
89 {
90         unsigned char *hpix;
91         uint32_t *cpix;
92         int i, width, height, cwidth, cheight;
93         struct voxscape *vox;
94
95         if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
96                 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
97                 return 0;
98         }
99         if(!(cpix = img_load_pixels(cfile, &cwidth, &cheight, IMG_FMT_RGBA32))) {
100                 fprintf(stderr, "vox_open: failed to load color map: %s\n", cfile);
101                 img_free_pixels(hpix);
102                 return 0;
103         }
104         if(cwidth != width || cheight != height) {
105                 img_free_pixels(hpix);
106                 img_free_pixels(cpix);
107                 fprintf(stderr, "vox_open: %s and %s have different dimensions\n", hfile, cfile);
108                 return 0;
109         }
110
111         if(!(vox = vox_create(width, height))) {
112                 img_free_pixels(hpix);
113                 img_free_pixels(cpix);
114                 return 0;
115         }
116
117         memcpy(vox->height, hpix, width * height);
118
119         /* swap r/b and discard alpha while copying */
120         for(i=0; i<width*height; i++) {
121                 vox->color[i] = (cpix[i] & 0xff00) | ((cpix[i] & 0xff) << 16) |
122                         ((cpix[i] & 0xff0000) >> 16);
123         }
124
125         img_free_pixels(hpix);
126         img_free_pixels(cpix);
127
128         return vox;
129 }
130
131 void vox_free(struct voxscape *vox)
132 {
133         if(!vox) return;
134
135         free(vox->color);
136         free(vox->height);
137         free(vox->coltop);
138         free(vox->slicelen);
139         free(vox);
140 }
141
142 void vox_fog(struct voxscape *vox, int zstart, uint32_t color)
143 {
144         vox->zfog = zstart;
145
146         vox->fogcolor[2] = color >> 16;
147         vox->fogcolor[1] = (color >> 8) & 0xff;
148         vox->fogcolor[0] = color & 0xff;
149 }
150
151 #define H(x, y) \
152         vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
153 #define C(x, y) \
154         vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
155
156
157 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
158 {
159         int32_t u, v;
160         int h00, h01, h10, h11, h0, h1;
161
162         if(!vox->hfilt) {
163                 return H(x, y);
164         }
165
166         h00 = H(x, y);
167         h01 = H(x, y + 0x10000);
168         h10 = H(x + 0x10000, y);
169         h11 = H(x + 0x10000, y + 0x10000);
170
171         u = x & 0xffff;
172         v = y & 0xffff;
173
174         h0 = XLERP(h00, h01, v, 16);
175         h1 = XLERP(h10, h11, v, 16);
176         return XLERP(h0, h1, u, 16);
177 }
178
179 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
180 {
181         int32_t u, v;
182         uint32_t c00, c01, c10, c11, c0, c1;
183
184         if(!vox->cfilt) {
185                 return C(x, y);
186         }
187
188         c00 = C(x, y);
189         c01 = C(x, y + 0x10000);
190         c10 = C(x + 0x10000, y);
191         c11 = C(x + 0x10000, y + 0x10000);
192
193         u = x & 0xffff;
194         v = y & 0xffff;
195
196         c0 = lerp_color(c00, c01, v);
197         c1 = lerp_color(c10, c11, v);
198         return lerp_color(c0, c1, u);
199 }
200
201
202 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
203 {
204         vox->hfilt = hfilt;
205         vox->cfilt = cfilt;
206 }
207
208 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb, int horizon)
209 {
210         if(xres != vox->fbwidth) {
211                 free(vox->coltop);
212                 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
213                         fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
214                         return;
215                 }
216         }
217         vox->fb = fb;
218         vox->fbwidth = xres;
219         vox->fbheight = yres;
220         vox->horizon = horizon >= 0 ? horizon : vox->fbheight / 2;
221 }
222
223 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
224 {
225         if(h < 0) {
226                 h = vox_height(vox, x, y) - h;
227         }
228
229         vox->x = x;
230         vox->y = y;
231         vox->vheight = h;
232         vox->angle = angle;
233
234         vox->valid &= ~SLICELEN;
235 }
236
237 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
238 {
239         vox->fov = fov;
240         vox->znear = znear;
241         vox->zfar = zfar;
242
243         vox->nslices = vox->zfar - vox->znear;
244         free(vox->slicelen);
245         if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
246                 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
247                 return;
248         }
249
250         vox->valid &= ~SLICELEN;
251 }
252
253 /* algorithm:
254  * calculate extents of horizontal equidistant line from the viewer based on fov
255  * for each column step along this line and compute height for each pixel
256  * fill the visible (top) part of each column
257  */
258
259 void vox_render(struct voxscape *vox)
260 {
261         int i;
262
263         vox_begin(vox);
264         for(i=0; i<vox->nslices; i++) {
265                 vox_render_slice(vox, i);
266         }
267 }
268
269 void vox_begin(struct voxscape *vox)
270 {
271         int i;
272
273         memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
274
275         if(!(vox->valid & SLICELEN)) {
276                 float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
277                 for(i=0; i<vox->nslices; i++) {
278                         vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
279                 }
280                 vox->valid |= SLICELEN;
281         }
282 }
283
284 void vox_render_slice(struct voxscape *vox, int n)
285 {
286         int i, j, hval, colstart, colheight, z;
287         int32_t x, y, len, xstep, ystep, fog;
288         uint32_t color;
289         uint32_t *fbptr;
290
291         if(vox->zfog > 0 && n > vox->zfog) {
292                 fog = ((n - vox->zfog) << 16) / (vox->zfar - vox->zfog);
293         } else {
294                 fog = 0;
295         }
296
297         z = vox->znear + n;
298
299         len = vox->slicelen[n] >> 8;
300         xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
301         ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
302
303         x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
304         y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
305         for(i=0; i<vox->fbwidth; i++) {
306                 hval = vox_height(vox, x, y) - vox->vheight;
307                 hval = hval * 160 / (vox->znear + n) + vox->horizon;
308                 if(hval > vox->fbheight) hval = vox->fbheight;
309                 if(hval > vox->coltop[i]) {
310                         color = vox_color(vox, x, y);
311                         colstart = vox->fbheight - hval;
312                         colheight = hval - vox->coltop[i];
313                         fbptr = vox->fb + colstart * vox->fbwidth + i;
314
315                         if(fog > 0) {
316                                 color = lerp_pcol_rgb(color, vox->fogcolor[0], vox->fogcolor[1], vox->fogcolor[2], fog);
317                         }
318
319                         for(j=0; j<colheight; j++) {
320                                 *fbptr = color;
321                                 fbptr += vox->fbwidth;
322                         }
323                         vox->coltop[i] = hval;
324                 }
325
326                 x += xstep;
327                 y += ystep;
328         }
329 }
330
331 void vox_sky_solid(struct voxscape *vox, uint32_t color)
332 {
333         int i, j, colheight;
334         uint32_t *fbptr;
335
336         for(i=0; i<vox->fbwidth; i++) {
337                 fbptr = vox->fb + i;
338                 colheight = vox->fbheight - vox->coltop[i];
339                 for(j=0; j<colheight; j++) {
340                         *fbptr = color;
341                         fbptr += vox->fbwidth;
342                 }
343         }
344 }
345
346 void vox_sky_grad(struct voxscape *vox, uint32_t chor, uint32_t ctop)
347 {
348         int i, j, colheight, t;
349         int r0, g0, b0, r1, g1, b1;
350         int d = vox->fbheight - vox->horizon;
351         uint32_t *grad, *fbptr;
352
353         grad = alloca(vox->fbheight * sizeof *grad);
354
355         b0 = ctop >> 16;
356         g0 = (ctop >> 8) & 0xff;
357         r0 = ctop & 0xff;
358         b1 = chor >> 16;
359         g1 = (chor >> 8) & 0xff;
360         r1 = chor & 0xff;
361
362         for(i=0; i<d; i++) {
363                 t = (i << 16) / d;
364                 grad[i] = lerp_rgb(r0, g0, b0, r1, g1, b1, t);
365         }
366         for(i=d; i<vox->fbheight; i++) {
367                 grad[i] = chor;
368         }
369
370         for(i=0; i<vox->fbwidth; i++) {
371                 fbptr = vox->fb + i;
372                 colheight = vox->fbheight - vox->coltop[i];
373                 for(j=0; j<colheight; j++) {
374                         *fbptr = grad[j];
375                         fbptr += vox->fbwidth;
376                 }
377         }
378 }
379
380 static uint32_t lerp_rgb(int r0, int g0, int b0, int r1, int g1, int b1, int32_t t)
381 {
382         int r = XLERP(r0, r1, t, 16);
383         int g = XLERP(g0, g1, t, 16);
384         int b = XLERP(b0, b1, t, 16);
385         return (b << 16) | (g << 8) | r;
386 }
387
388 static uint32_t lerp_color(uint32_t ca, uint32_t cb, int32_t t)
389 {
390         int r0, g0, b0, r1, g1, b1;
391
392         b0 = ca >> 16;
393         g0 = (ca >> 8) & 0xff;
394         r0 = ca & 0xff;
395         b1 = cb >> 16;
396         g1 = (cb >> 8) & 0xff;
397         r1 = cb & 0xff;
398
399         return lerp_rgb(r0, g0, b0, r1, g1, b1, t);
400 }
401
402 static uint32_t lerp_pcol_rgb(uint32_t pcol, int r, int g, int b, int32_t t)
403 {
404         int b0 = pcol >> 16;
405         int g0 = (pcol >> 8) & 0xff;
406         int r0 = pcol & 0xff;
407
408         return lerp_rgb(r0, g0, b0, r, g, b, t);
409 }
410