voxelscape port
[gbajam22] / 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 "voxscape.h"
8 #include "util.h"
9 #include "debug.h"
10
11 #define XLERP(a, b, t, fp) \
12         ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
13
14 enum {
15         SLICELEN        = 1
16 };
17
18 struct voxscape {
19         int xsz, ysz;
20         unsigned char *height;
21         unsigned char *color;
22         int xshift, xmask, ymask;
23
24         int hfilt, cfilt;
25
26         /* framebuffer */
27         uint16_t *fb;
28         int fbwidth, fbheight;
29         int *coltop;
30         int horizon;
31
32         /* view */
33         int32_t x, y, angle;
34         int vheight;
35
36         /* projection */
37         int fov, znear, zfar;
38         int nslices;
39         int32_t *slicelen;
40         int proj_dist;
41
42         int zfog;       /* fog start Z (0: no fog) */
43         uint8_t fogcolor;
44
45         unsigned int valid;
46 };
47
48 struct voxscape *vox_create(int xsz, int ysz)
49 {
50         struct voxscape *vox;
51
52         if(!(vox = calloc(1, sizeof *vox))) {
53                 return 0;
54         }
55         if(!(vox->height = calloc(xsz * ysz, 1))) {
56                 panic(get_pc(), "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
57         }
58         if(!(vox->color = calloc(xsz * ysz, 1))) {
59                 panic(get_pc(), "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
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 void vox_free(struct voxscape *vox)
80 {
81         if(!vox) return;
82
83         free(vox->color);
84         free(vox->height);
85         free(vox->coltop);
86         free(vox->slicelen);
87         free(vox);
88 }
89
90 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
91 {
92         if(data) {
93                 memcpy(vox->color, data, vox->xsz * vox->ysz);
94         }
95         return vox->color;
96 }
97
98 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
99 {
100         if(data) {
101                 memcpy(vox->height, data, vox->xsz * vox->ysz);
102         }
103         return vox->height;
104 }
105
106 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
107 {
108         vox->zfog = zstart;
109         vox->fogcolor = color;
110 }
111
112 #define H(x, y) \
113         vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
114 #define C(x, y) \
115         vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
116
117
118 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
119 {
120         int32_t u, v;
121         int h00, h01, h10, h11, h0, h1;
122
123         if(!vox->hfilt) {
124                 return H(x, y);
125         }
126
127         h00 = H(x, y);
128         h01 = H(x, y + 0x10000);
129         h10 = H(x + 0x10000, y);
130         h11 = H(x + 0x10000, y + 0x10000);
131
132         u = x & 0xffff;
133         v = y & 0xffff;
134
135         h0 = XLERP(h00, h01, v, 16);
136         h1 = XLERP(h10, h11, v, 16);
137         return XLERP(h0, h1, u, 16);
138 }
139
140 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
141 {
142         int32_t u, v;
143         int c00, c01, c10, c11, c0, c1;
144
145         if(!vox->cfilt) {
146                 return C(x, y);
147         }
148
149         c00 = C(x, y);
150         c01 = C(x, y + 0x10000);
151         c10 = C(x + 0x10000, y);
152         c11 = C(x + 0x10000, y + 0x10000);
153
154         u = x & 0xffff;
155         v = y & 0xffff;
156
157         c0 = XLERP(c00, c01, v, 16);
158         c1 = XLERP(c10, c11, v, 16);
159         return XLERP(c0, c1, u, 16);
160 }
161
162
163 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
164 {
165         vox->hfilt = hfilt;
166         vox->cfilt = cfilt;
167 }
168
169 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
170 {
171         if(xres != vox->fbwidth) {
172                 free(vox->coltop);
173                 if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
174                         fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
175                         return;
176                 }
177         }
178         vox->fb = fb;
179         vox->fbwidth = xres;
180         vox->fbheight = yres;
181         vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
182 }
183
184 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
185 {
186         if(h < 0) {
187                 h = vox_height(vox, x, y) - h;
188         }
189
190         vox->x = x;
191         vox->y = y;
192         vox->vheight = h;
193         vox->angle = angle;
194
195         vox->valid &= ~SLICELEN;
196 }
197
198 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
199 {
200         vox->fov = fov;
201         vox->znear = znear;
202         vox->zfar = zfar;
203
204         vox->nslices = vox->zfar - vox->znear;
205         free(vox->slicelen);
206         if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
207                 fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
208                 return;
209         }
210
211         vox->valid &= ~SLICELEN;
212 }
213
214 /* algorithm:
215  * calculate extents of horizontal equidistant line from the viewer based on fov
216  * for each column step along this line and compute height for each pixel
217  * fill the visible (top) part of each column
218  */
219
220 void vox_render(struct voxscape *vox)
221 {
222         int i;
223
224         vox_begin(vox);
225         for(i=0; i<vox->nslices; i++) {
226                 vox_render_slice(vox, i);
227         }
228 }
229
230 void vox_begin(struct voxscape *vox)
231 {
232         int i;
233
234         memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
235
236         if(!(vox->valid & SLICELEN)) {
237                 float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
238                 for(i=0; i<vox->nslices; i++) {
239                         vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
240                 }
241                 vox->valid |= SLICELEN;
242         }
243 }
244
245 void vox_render_slice(struct voxscape *vox, int n)
246 {
247         int i, j, hval, colstart, colheight, z;
248         int32_t x, y, len, xstep, ystep;
249         uint8_t color;
250         uint16_t *fbptr;
251
252         z = vox->znear + n;
253
254         len = vox->slicelen[n] >> 8;
255         xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
256         ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
257
258         x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
259         y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
260         /* TODO double column */
261         for(i=0; i<vox->fbwidth/2; i++) {
262                 hval = vox_height(vox, x, y) - vox->vheight;
263                 hval = hval * 160 / (vox->znear + n) + vox->horizon;
264                 if(hval > vox->fbheight) hval = vox->fbheight;
265                 if(hval > vox->coltop[i]) {
266                         color = vox_color(vox, x, y);
267                         colstart = vox->fbheight - hval;
268                         colheight = hval - vox->coltop[i];
269                         fbptr = vox->fb + colstart * vox->fbwidth + i;
270
271                         for(j=0; j<colheight; j++) {
272                                 *fbptr = color | ((uint16_t)color << 8);
273                                 fbptr += vox->fbwidth >> 1;
274                         }
275                         vox->coltop[i] = hval;
276                 }
277
278                 x += xstep;
279                 y += ystep;
280         }
281 }
282
283 void vox_sky_solid(struct voxscape *vox, uint8_t color)
284 {
285         int i, j, colheight;
286         uint16_t *fbptr;
287
288         /* TODO double columns */
289         for(i=0; i<vox->fbwidth/2; i++) {
290                 fbptr = vox->fb + i;
291                 colheight = vox->fbheight - vox->coltop[i];
292                 for(j=0; j<colheight; j++) {
293                         *fbptr = color | ((uint16_t)color << 8);
294                         fbptr += vox->fbwidth >> 1;
295                 }
296         }
297 }
298
299 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
300 {
301         int i, j, colheight, t;
302         int d = vox->fbheight - vox->horizon;
303         uint8_t *grad;
304         uint16_t *fbptr;
305
306         grad = alloca(vox->fbheight * sizeof *grad);
307
308         for(i=0; i<d; i++) {
309                 t = (i << 8) / d;
310                 grad[i] = XLERP(ctop, chor, t, 8);
311         }
312         for(i=d; i<vox->fbheight; i++) {
313                 grad[i] = chor;
314         }
315
316         /* TODO double columns */
317         for(i=0; i<vox->fbwidth/2; i++) {
318                 fbptr = vox->fb + i;
319                 colheight = vox->fbheight - vox->coltop[i];
320                 for(j=0; j<colheight; j++) {
321                         *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
322                         fbptr += vox->fbwidth >> 1;
323                 }
324         }
325 }