it works
[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
10 enum {
11         SLICELEN        = 1
12 };
13
14 struct voxscape {
15         int xsz, ysz;
16         unsigned char *height;
17         uint32_t *color;
18         int xshift, xmask, ymask;
19
20         /* framebuffer */
21         uint32_t *fb;
22         int fbwidth, fbheight;
23         int *coltop;
24
25         /* view */
26         int32_t x, y, angle;
27         int vheight;
28
29         /* projection */
30         int fov, znear, zfar;
31         int nslices;
32         int32_t *slicelen;
33         int proj_dist;
34
35         unsigned int valid;
36 };
37
38 struct voxscape *vox_create(int xsz, int ysz)
39 {
40         struct voxscape *vox;
41
42         if(!(vox = calloc(1, sizeof *vox))) {
43                 return 0;
44         }
45         if(!(vox->height = calloc(1, xsz * ysz))) {
46                 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
47                 free(vox);
48                 return 0;
49         }
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);
52                 free(vox->height);
53                 free(vox);
54                 return 0;
55         }
56         vox->xsz = xsz;
57         vox->ysz = ysz;
58
59         vox->xmask = vox->xsz - 1;
60         vox->ymask = vox->ysz - 1;
61
62         vox->xshift = -1;
63         while(xsz) {
64                 xsz >>= 1;
65                 vox->xshift++;
66         }
67
68         vox->vheight = 50;
69         vox->proj_dist = 4;     /* TODO */
70
71         return vox;
72 }
73
74 struct voxscape *vox_open(const char *hfile, const char *cfile)
75 {
76         unsigned char *hpix;
77         uint32_t *cpix;
78         int width, height, cwidth, cheight;
79         struct voxscape *vox;
80
81         if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
82                 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
83                 return 0;
84         }
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);
88                 return 0;
89         }
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);
94                 return 0;
95         }
96
97         if(!(vox = vox_create(width, height))) {
98                 img_free_pixels(hpix);
99                 img_free_pixels(cpix);
100                 return 0;
101         }
102         memcpy(vox->height, hpix, width * height);
103         memcpy(vox->color, cpix, width * height * sizeof *vox->color);
104
105         img_free_pixels(hpix);
106         img_free_pixels(cpix);
107
108         return vox;
109 }
110
111 void vox_free(struct voxscape *vox)
112 {
113         if(!vox) return;
114
115         free(vox->color);
116         free(vox->height);
117         free(vox->coltop);
118         free(vox->slicelen);
119         free(vox);
120 }
121
122 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb)
123 {
124         if(xres != vox->fbwidth) {
125                 free(vox->coltop);
126                 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
127                         fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
128                         return;
129                 }
130         }
131         vox->fb = fb;
132         vox->fbwidth = xres;
133         vox->fbheight = yres;
134 }
135
136 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int32_t angle)
137 {
138         vox->x = x;
139         vox->y = y;
140         vox->angle = angle;
141         /* TODO precalc stuff */
142
143         vox->valid &= ~SLICELEN;
144 }
145
146 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
147 {
148         vox->fov = fov;
149         vox->znear = znear;
150         vox->zfar = zfar;
151
152         vox->nslices = vox->zfar - vox->znear;
153         free(vox->slicelen);
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);
156                 return;
157         }
158
159         vox->valid &= ~SLICELEN;
160 }
161
162 /* algorithm:
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
166  */
167
168 void vox_render(struct voxscape *vox)
169 {
170         int i;
171
172         vox_begin(vox);
173         for(i=0; i<vox->nslices; i++) {
174                 vox_render_slice(vox, i);
175         }
176 }
177
178 void vox_begin(struct voxscape *vox)
179 {
180         int i;
181
182         /*
183         for(i=0; i<vox->fbwidth; i++) {
184                 vox->coltop[i] = vox->fbheight;
185         }
186         */
187         memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
188
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);
193                 }
194                 vox->valid |= SLICELEN;
195         }
196 }
197
198 void vox_render_slice(struct voxscape *vox, int n)
199 {
200         int i, j, tx, ty, hval, colstart, colheight;
201         int32_t x, y, len, xstep;
202         uint32_t color;
203         uint32_t *fbptr;
204
205         len = vox->slicelen[n];
206         xstep = len / vox->fbwidth;
207
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;
213
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;
222
223                         for(j=0; j<colheight; j++) {
224                                 *fbptr = color;
225                                 fbptr += vox->fbwidth;
226                         }
227                         vox->coltop[i] = hval;
228                 }
229
230                 x += xstep;
231         }
232 }
233
234 void vox_sky_grad(struct voxscape *vox, uint32_t chor, uint32_t ctop)
235 {
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;
240
241         grad = alloca(vox->fbheight * sizeof *grad);
242
243         r0 = ctop >> 16;
244         g0 = (ctop >> 8) & 0xff;
245         b0 = ctop & 0xff;
246         r1 = chor >> 16;
247         g1 = (chor >> 8) & 0xff;
248         b1 = chor & 0xff;
249
250         for(i=0; i<d; i++) {
251                 t = (i << 8) / d;
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;
259         }
260         for(i=d; i<vox->fbheight; i++) {
261                 grad[i] = chor;
262         }
263
264         for(i=0; i<vox->fbwidth; i++) {
265                 fbptr = vox->fb + i;
266                 colheight = vox->fbheight - vox->coltop[i];
267                 for(j=0; j<colheight; j++) {
268                         *fbptr = grad[j];
269                         fbptr += vox->fbwidth;
270                 }
271         }
272 }