fog
[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         int horizon;
26
27         /* view */
28         int32_t x, y, angle;
29         int vheight;
30
31         /* projection */
32         int fov, znear, zfar;
33         int nslices;
34         int32_t *slicelen;
35         int proj_dist;
36
37         int zfog;       /* fog start Z (0: no fog) */
38         int fogcolor[3];
39
40         unsigned int valid;
41 };
42
43 struct voxscape *vox_create(int xsz, int ysz)
44 {
45         struct voxscape *vox;
46
47         if(!(vox = calloc(1, sizeof *vox))) {
48                 return 0;
49         }
50         if(!(vox->height = calloc(1, xsz * ysz))) {
51                 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
52                 free(vox);
53                 return 0;
54         }
55         if(!(vox->color = calloc(xsz * ysz, sizeof *vox->color))) {
56                 fprintf(stderr, "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
57                 free(vox->height);
58                 free(vox);
59                 return 0;
60         }
61         vox->xsz = xsz;
62         vox->ysz = ysz;
63
64         vox->xmask = vox->xsz - 1;
65         vox->ymask = vox->ysz - 1;
66
67         vox->xshift = -1;
68         while(xsz) {
69                 xsz >>= 1;
70                 vox->xshift++;
71         }
72
73         vox->vheight = 80;
74         vox->proj_dist = 4;     /* TODO */
75
76         return vox;
77 }
78
79 struct voxscape *vox_open(const char *hfile, const char *cfile)
80 {
81         unsigned char *hpix;
82         uint32_t *cpix;
83         int i, width, height, cwidth, cheight;
84         struct voxscape *vox;
85
86         if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
87                 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
88                 return 0;
89         }
90         if(!(cpix = img_load_pixels(cfile, &cwidth, &cheight, IMG_FMT_RGBA32))) {
91                 fprintf(stderr, "vox_open: failed to load color map: %s\n", cfile);
92                 img_free_pixels(hpix);
93                 return 0;
94         }
95         if(cwidth != width || cheight != height) {
96                 img_free_pixels(hpix);
97                 img_free_pixels(cpix);
98                 fprintf(stderr, "vox_open: %s and %s have different dimensions\n", hfile, cfile);
99                 return 0;
100         }
101
102         if(!(vox = vox_create(width, height))) {
103                 img_free_pixels(hpix);
104                 img_free_pixels(cpix);
105                 return 0;
106         }
107
108         memcpy(vox->height, hpix, width * height);
109
110         for(i=0; i<width*height; i++) {
111                 vox->color[i] = cpix[i] & 0xffffff;     /* discard alpha */
112         }
113
114         img_free_pixels(hpix);
115         img_free_pixels(cpix);
116
117         return vox;
118 }
119
120 void vox_free(struct voxscape *vox)
121 {
122         if(!vox) return;
123
124         free(vox->color);
125         free(vox->height);
126         free(vox->coltop);
127         free(vox->slicelen);
128         free(vox);
129 }
130
131 void vox_fog(struct voxscape *vox, int zstart, uint32_t color)
132 {
133         vox->zfog = zstart;
134
135         vox->fogcolor[0] = color >> 16;
136         vox->fogcolor[1] = (color >> 8) & 0xff;
137         vox->fogcolor[2] = color & 0xff;
138 }
139
140 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
141 {
142         int tx = (x >> 16) & vox->xmask;
143         int ty = (y >> 16) & vox->ymask;
144
145         return vox->height[(ty << vox->xshift) + tx];
146 }
147
148 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb, int horizon)
149 {
150         if(xres != vox->fbwidth) {
151                 free(vox->coltop);
152                 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
153                         fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
154                         return;
155                 }
156         }
157         vox->fb = fb;
158         vox->fbwidth = xres;
159         vox->fbheight = yres;
160         vox->horizon = horizon >= 0 ? horizon : vox->fbheight / 2;
161 }
162
163 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
164 {
165         if(h < 0) {
166                 h = vox_height(vox, x, y) - h;
167         }
168
169         vox->x = x;
170         vox->y = y;
171         vox->vheight = h;
172         vox->angle = angle;
173
174         vox->valid &= ~SLICELEN;
175 }
176
177 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
178 {
179         vox->fov = fov;
180         vox->znear = znear;
181         vox->zfar = zfar;
182
183         vox->nslices = vox->zfar - vox->znear;
184         free(vox->slicelen);
185         if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
186                 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
187                 return;
188         }
189
190         vox->valid &= ~SLICELEN;
191 }
192
193 /* algorithm:
194  * calculate extents of horizontal equidistant line from the viewer based on fov
195  * for each column step along this line and compute height for each pixel
196  * fill the visible (top) part of each column
197  */
198
199 void vox_render(struct voxscape *vox)
200 {
201         int i;
202
203         vox_begin(vox);
204         for(i=0; i<vox->nslices; i++) {
205                 vox_render_slice(vox, i);
206         }
207 }
208
209 void vox_begin(struct voxscape *vox)
210 {
211         int i;
212
213         memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
214
215         if(!(vox->valid & SLICELEN)) {
216                 float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
217                 for(i=0; i<vox->nslices; i++) {
218                         vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
219                 }
220                 vox->valid |= SLICELEN;
221         }
222 }
223
224 void vox_render_slice(struct voxscape *vox, int n)
225 {
226         int i, j, tx, ty, hval, colstart, colheight, z, r, g, b;
227         int32_t x, y, len, xstep, ystep, fog;
228         uint32_t color;
229         uint32_t *fbptr;
230
231         if(vox->zfog > 0 && n > vox->zfog) {
232                 fog = ((n - vox->zfog) << 8) / (vox->zfar - vox->zfog);
233         } else {
234                 fog = 0;
235         }
236
237         z = vox->znear + n;
238
239         len = vox->slicelen[n] >> 8;
240         xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
241         ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
242
243         x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
244         y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
245         for(i=0; i<vox->fbwidth; i++) {
246                 tx = (x >> 16) & vox->xmask;
247                 ty = (y >> 16) & vox->ymask;
248
249                 hval = vox->height[(ty << vox->xshift) + tx] - vox->vheight;
250                 hval = hval * 160 / (vox->znear + n) + vox->horizon;
251                 if(hval > vox->fbheight) hval = vox->fbheight;
252                 if(hval > vox->coltop[i]) {
253                         color = vox->color[(ty << vox->xshift) + tx];
254                         colstart = vox->fbheight - hval;
255                         colheight = hval - vox->coltop[i];
256                         fbptr = vox->fb + colstart * vox->fbwidth + i;
257
258                         if(fog > 0) {
259                                 r = color >> 16;
260                                 g = (color >> 8) & 0xff;
261                                 b = color & 0xff;
262                                 r = ((r << 8) + (vox->fogcolor[0] - r) * fog) >> 8;
263                                 g = ((g << 8) + (vox->fogcolor[1] - g) * fog) >> 8;
264                                 b = ((b << 8) + (vox->fogcolor[2] - b) * fog) >> 8;
265                                 color = (r << 16) | (g << 8) | b;
266                         }
267
268                         for(j=0; j<colheight; j++) {
269                                 *fbptr = color;
270                                 fbptr += vox->fbwidth;
271                         }
272                         vox->coltop[i] = hval;
273                 }
274
275                 x += xstep;
276                 y += ystep;
277         }
278 }
279
280 void vox_sky_solid(struct voxscape *vox, uint32_t color)
281 {
282         int i, j, colheight;
283         uint32_t *fbptr;
284
285         for(i=0; i<vox->fbwidth; i++) {
286                 fbptr = vox->fb + i;
287                 colheight = vox->fbheight - vox->coltop[i];
288                 for(j=0; j<colheight; j++) {
289                         *fbptr = color;
290                         fbptr += vox->fbwidth;
291                 }
292         }
293 }
294
295 void vox_sky_grad(struct voxscape *vox, uint32_t chor, uint32_t ctop)
296 {
297         int i, j, colheight, t;
298         int r0, g0, b0, r1, g1, b1, r, g, b;
299         int d = vox->fbheight - vox->horizon;
300         uint32_t *grad, *fbptr;
301
302         grad = alloca(vox->fbheight * sizeof *grad);
303
304         r0 = ctop >> 16;
305         g0 = (ctop >> 8) & 0xff;
306         b0 = ctop & 0xff;
307         r1 = chor >> 16;
308         g1 = (chor >> 8) & 0xff;
309         b1 = chor & 0xff;
310
311         for(i=0; i<d; i++) {
312                 t = (i << 8) / d;
313                 r = ((r0 << 8) + (r1 - r0) * t) >> 8;
314                 g = ((g0 << 8) + (g1 - g0) * t) >> 8;
315                 b = ((b0 << 8) + (b1 - b0) * t) >> 8;
316                 assert(r >= 0 && r < 256);
317                 assert(g >= 0 && g < 256);
318                 assert(b >= 0 && b < 256);
319                 grad[i] = (r << 16) | (g << 8) | b;
320         }
321         for(i=d; i<vox->fbheight; i++) {
322                 grad[i] = chor;
323         }
324
325         for(i=0; i<vox->fbwidth; i++) {
326                 fbptr = vox->fb + i;
327                 colheight = vox->fbheight - vox->coltop[i];
328                 for(j=0; j<colheight; j++) {
329                         *fbptr = grad[j];
330                         fbptr += vox->fbwidth;
331                 }
332         }
333 }