it works
[voxscape] / src / voxscape.c
index 2ccb5f9..28a02bf 100644 (file)
@@ -3,6 +3,7 @@
 #include <string.h>
 #include <stdint.h>
 #include <math.h>
+#include <assert.h>
 #include <imago2.h>
 #include "voxscape.h"
 
@@ -64,7 +65,8 @@ struct voxscape *vox_create(int xsz, int ysz)
                vox->xshift++;
        }
 
-       vox->proj_dist = 2;     /* TODO */
+       vox->vheight = 50;
+       vox->proj_dist = 4;     /* TODO */
 
        return vox;
 }
@@ -187,7 +189,7 @@ void vox_begin(struct voxscape *vox)
        if(!(vox->valid & SLICELEN)) {
                float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
                for(i=0; i<vox->nslices; i++) {
-                       vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 2.0f * 65536.0f);
+                       vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 10.0f * 65536.0f);
                }
                vox->valid |= SLICELEN;
        }
@@ -204,16 +206,17 @@ void vox_render_slice(struct voxscape *vox, int n)
        xstep = len / vox->fbwidth;
 
        x = vox->x - xstep * (vox->fbwidth >> 1);
-       y = vox->y + (vox->znear << 16);
+       y = vox->y + ((vox->znear + n) << 16);
        for(i=0; i<vox->fbwidth; i++) {
                tx = (x >> 16) & vox->xmask;
                ty = (y >> 16) & vox->ymask;
 
-               hval = vox->height[(ty << vox->xshift) + tx];
-               hval = (hval - vox->vheight) * vox->proj_dist / (vox->znear + i);
+               hval = vox->height[(ty << vox->xshift) + tx] - 80;
+               hval = hval * 80 / (vox->znear + n) + 250;
+               if(hval > vox->fbheight) hval = vox->fbheight;
                if(hval > vox->coltop[i]) {
                        color = vox->color[(ty << vox->xshift) + tx];
-                       colstart = vox->fbheight - 1 - hval;
+                       colstart = vox->fbheight - hval;
                        colheight = hval - vox->coltop[i];
                        fbptr = vox->fb + colstart * vox->fbwidth + i;
 
@@ -221,8 +224,49 @@ void vox_render_slice(struct voxscape *vox, int n)
                                *fbptr = color;
                                fbptr += vox->fbwidth;
                        }
+                       vox->coltop[i] = hval;
                }
 
                x += xstep;
        }
 }
+
+void vox_sky_grad(struct voxscape *vox, uint32_t chor, uint32_t ctop)
+{
+       int i, j, colheight, t;
+       int r0, g0, b0, r1, g1, b1, r, g, b;
+       int d = vox->fbheight - 250;
+       uint32_t *grad, *fbptr;
+
+       grad = alloca(vox->fbheight * sizeof *grad);
+
+       r0 = ctop >> 16;
+       g0 = (ctop >> 8) & 0xff;
+       b0 = ctop & 0xff;
+       r1 = chor >> 16;
+       g1 = (chor >> 8) & 0xff;
+       b1 = chor & 0xff;
+
+       for(i=0; i<d; i++) {
+               t = (i << 8) / d;
+               r = ((r0 << 8) + (r1 - r0) * t) >> 8;
+               g = ((g0 << 8) + (g1 - g0) * t) >> 8;
+               b = ((b0 << 8) + (b1 - b0) * t) >> 8;
+               assert(r >= 0 && r < 256);
+               assert(g >= 0 && g < 256);
+               assert(b >= 0 && b < 256);
+               grad[i] = (r << 16) | (g << 8) | b;
+       }
+       for(i=d; i<vox->fbheight; i++) {
+               grad[i] = chor;
+       }
+
+       for(i=0; i<vox->fbwidth; i++) {
+               fbptr = vox->fb + i;
+               colheight = vox->fbheight - vox->coltop[i];
+               for(j=0; j<colheight; j++) {
+                       *fbptr = grad[j];
+                       fbptr += vox->fbwidth;
+               }
+       }
+}