it works
[voxscape] / src / voxscape.c
index b9a18c3..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"
 
@@ -14,6 +15,7 @@ struct voxscape {
        int xsz, ysz;
        unsigned char *height;
        uint32_t *color;
+       int xshift, xmask, ymask;
 
        /* framebuffer */
        uint32_t *fb;
@@ -22,11 +24,13 @@ struct voxscape {
 
        /* view */
        int32_t x, y, angle;
+       int vheight;
 
        /* projection */
        int fov, znear, zfar;
        int nslices;
-       int32_t *slicelen;      /* 24.8 */
+       int32_t *slicelen;
+       int proj_dist;
 
        unsigned int valid;
 };
@@ -52,6 +56,18 @@ struct voxscape *vox_create(int xsz, int ysz)
        vox->xsz = xsz;
        vox->ysz = ysz;
 
+       vox->xmask = vox->xsz - 1;
+       vox->ymask = vox->ysz - 1;
+
+       vox->xshift = -1;
+       while(xsz) {
+               xsz >>= 1;
+               vox->xshift++;
+       }
+
+       vox->vheight = 50;
+       vox->proj_dist = 4;     /* TODO */
+
        return vox;
 }
 
@@ -124,13 +140,11 @@ void vox_view(struct voxscape *vox, int32_t x, int32_t y, int32_t angle)
        vox->angle = angle;
        /* TODO precalc stuff */
 
-       valid &= ~SLICELEN;
+       vox->valid &= ~SLICELEN;
 }
 
 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
 {
-       int i;
-
        vox->fov = fov;
        vox->znear = znear;
        vox->zfar = zfar;
@@ -142,7 +156,7 @@ void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
                return;
        }
 
-       valid &= ~SLICELEN;
+       vox->valid &= ~SLICELEN;
 }
 
 /* algorithm:
@@ -165,14 +179,17 @@ void vox_begin(struct voxscape *vox)
 {
        int i;
 
+       /*
        for(i=0; i<vox->fbwidth; i++) {
                vox->coltop[i] = vox->fbheight;
        }
+       */
+       memset(vox->coltop, 0, vox->fbwidth * sizeof *vox->coltop);
 
        if(!(vox->valid & SLICELEN)) {
-               float theta = (float)vox->angle * M_PI / 360.0f;        /* half angle */
+               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 * 256.0f)
+                       vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 10.0f * 65536.0f);
                }
                vox->valid |= SLICELEN;
        }
@@ -180,9 +197,76 @@ void vox_begin(struct voxscape *vox)
 
 void vox_render_slice(struct voxscape *vox, int n)
 {
-       int32_t len;
+       int i, j, tx, ty, hval, colstart, colheight;
+       int32_t x, y, len, xstep;
+       uint32_t color;
+       uint32_t *fbptr;
 
        len = vox->slicelen[n];
+       xstep = len / vox->fbwidth;
 
-       /* TODO cont. */
+       x = vox->x - xstep * (vox->fbwidth >> 1);
+       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] - 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 - hval;
+                       colheight = hval - vox->coltop[i];
+                       fbptr = vox->fb + colstart * vox->fbwidth + i;
+
+                       for(j=0; j<colheight; j++) {
+                               *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;
+               }
+       }
 }