df34f20ea535cfeb36a303b2de389f71ea05e20f
[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 <imago2.h>
7 #include "voxscape.h"
8
9 enum {
10         SLICELEN        = 1
11 };
12
13 struct voxscape {
14         int xsz, ysz;
15         unsigned char *height;
16         uint32_t *color;
17         int xshift, xmask, ymask;
18
19         /* framebuffer */
20         uint32_t *fb;
21         int fbwidth, fbheight;
22         int *coltop;
23
24         /* view */
25         int32_t x, y, angle;
26         int vheight;
27
28         /* projection */
29         int fov, znear, zfar;
30         int nslices;
31         int32_t *slicelen;
32         int proj_dist;
33
34         unsigned int valid;
35 };
36
37 struct voxscape *vox_create(int xsz, int ysz)
38 {
39         struct voxscape *vox;
40
41         if(!(vox = calloc(1, sizeof *vox))) {
42                 return 0;
43         }
44         if(!(vox->height = calloc(1, xsz * ysz))) {
45                 fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
46                 free(vox);
47                 return 0;
48         }
49         if(!(vox->color = calloc(xsz * ysz, sizeof *vox->color))) {
50                 fprintf(stderr, "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
51                 free(vox->height);
52                 free(vox);
53                 return 0;
54         }
55         vox->xsz = xsz;
56         vox->ysz = ysz;
57
58         vox->xmask = vox->xsz - 1;
59         vox->ymask = vox->ysz - 1;
60
61         vox->xshift = -1;
62         while(xsz) {
63                 xsz >>= 1;
64                 vox->xshift++;
65         }
66
67         vox->proj_dist = 2;     /* TODO */
68
69         return vox;
70 }
71
72 struct voxscape *vox_open(const char *hfile, const char *cfile)
73 {
74         unsigned char *hpix;
75         uint32_t *cpix;
76         int width, height, cwidth, cheight;
77         struct voxscape *vox;
78
79         if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
80                 fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
81                 return 0;
82         }
83         if(!(cpix = img_load_pixels(cfile, &cwidth, &cheight, IMG_FMT_RGBA32))) {
84                 fprintf(stderr, "vox_open: failed to load color map: %s\n", cfile);
85                 img_free_pixels(hpix);
86                 return 0;
87         }
88         if(cwidth != width || cheight != height) {
89                 img_free_pixels(hpix);
90                 img_free_pixels(cpix);
91                 fprintf(stderr, "vox_open: %s and %s have different dimensions\n", hfile, cfile);
92                 return 0;
93         }
94
95         if(!(vox = vox_create(width, height))) {
96                 img_free_pixels(hpix);
97                 img_free_pixels(cpix);
98                 return 0;
99         }
100         memcpy(vox->height, hpix, width * height);
101         memcpy(vox->color, cpix, width * height * sizeof *vox->color);
102
103         img_free_pixels(hpix);
104         img_free_pixels(cpix);
105
106         return vox;
107 }
108
109 void vox_free(struct voxscape *vox)
110 {
111         if(!vox) return;
112
113         free(vox->color);
114         free(vox->height);
115         free(vox->coltop);
116         free(vox->slicelen);
117         free(vox);
118 }
119
120 void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb)
121 {
122         if(xres != vox->fbwidth) {
123                 free(vox->coltop);
124                 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
125                         fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
126                         return;
127                 }
128         }
129         vox->fb = fb;
130         vox->fbwidth = xres;
131         vox->fbheight = yres;
132 }
133
134 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int32_t angle)
135 {
136         vox->x = x;
137         vox->y = y;
138         vox->angle = angle;
139         /* TODO precalc stuff */
140
141         vox->valid &= ~SLICELEN;
142 }
143
144 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
145 {
146         vox->fov = fov;
147         vox->znear = znear;
148         vox->zfar = zfar;
149
150         vox->nslices = vox->zfar - vox->znear;
151         free(vox->slicelen);
152         if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
153                 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
154                 return;
155         }
156
157         vox->valid &= ~SLICELEN;
158 }
159
160 /* algorithm:
161  * calculate extents of horizontal equidistant line from the viewer based on fov
162  * for each column step along this line and compute height for each pixel
163  * fill the visible (top) part of each column
164  */
165
166 void vox_render(struct voxscape *vox)
167 {
168         int i;
169
170         vox_begin(vox);
171         for(i=0; i<vox->nslices; i++) {
172                 vox_render_slice(vox, i);
173         }
174 }
175
176 void vox_begin(struct voxscape *vox)
177 {
178         int i;
179
180         /*
181         for(i=0; i<vox->fbwidth; i++) {
182                 vox->coltop[i] = vox->fbheight;
183         }
184         */
185         memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
186
187         if(!(vox->valid & SLICELEN)) {
188                 float theta = (float)vox->angle * M_PI / 360.0f;        /* half angle */
189                 for(i=0; i<vox->nslices; i++) {
190                         vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 2.0f * 65536.0f);
191                 }
192                 vox->valid |= SLICELEN;
193         }
194 }
195
196 void vox_render_slice(struct voxscape *vox, int n)
197 {
198         int i, j, tx, ty, hval, colstart, colheight;
199         int32_t x, y, len, xstep;
200         uint32_t color;
201         uint32_t *fbptr;
202
203         len = vox->slicelen[n];
204         xstep = len / vox->fbwidth;
205
206         x = vox->x - xstep * (vox->fbwidth >> 1);
207         y = vox->y + (vox->znear << 16);
208         for(i=0; i<vox->fbwidth; i++) {
209                 tx = (x >> 16) & vox->xmask;
210                 ty = (y >> 16) & vox->ymask;
211
212                 hval = (vox->height[(ty << vox->xshift) + tx] - vox->vheight) * vox->proj_dist / y;
213                 if(hval > vox->coltop[i]) {
214                         color = vox->color[(ty << vox->xshift) + tx];
215                         colstart = vox->fbheight - 1 - hval;
216                         colheight = hval - vox->coltop[i];
217                         fbptr = vox->fb + colstart * vox->fbwidth + i;
218
219                         for(j=0; j<colheight; j++) {
220                                 *fbptr = color;
221                                 fbptr += vox->fbwidth;
222                         }
223                 }
224
225                 x += xstep;
226         }
227 }