rotation
[voxscape] / src / voxscape.c
index df34f20..0ea8f06 100644 (file)
@@ -3,8 +3,10 @@
 #include <string.h>
 #include <stdint.h>
 #include <math.h>
+#include <assert.h>
 #include <imago2.h>
 #include "voxscape.h"
+#include "lut.h"
 
 enum {
        SLICELEN        = 1
@@ -64,7 +66,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;
 }
@@ -177,17 +180,12 @@ 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 * 65536.0f);
+                       vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
                }
                vox->valid |= SLICELEN;
        }
@@ -195,24 +193,29 @@ void vox_begin(struct voxscape *vox)
 
 void vox_render_slice(struct voxscape *vox, int n)
 {
-       int i, j, tx, ty, hval, colstart, colheight;
-       int32_t x, y, len, xstep;
+       int i, j, tx, ty, hval, colstart, colheight, z;
+       int32_t x, y, len, xstep, ystep;
        uint32_t color;
        uint32_t *fbptr;
 
-       len = vox->slicelen[n];
-       xstep = len / vox->fbwidth;
+       z = vox->znear + n;
+
+       len = vox->slicelen[n] >> 8;
+       xstep = ((COS(vox->angle) >> 8) * len) / vox->fbwidth;
+       ystep = ((SIN(vox->angle) >> 8) * len) / vox->fbwidth;
 
-       x = vox->x - xstep * (vox->fbwidth >> 1);
-       y = vox->y + (vox->znear << 16);
+       x = vox->x - SIN(vox->angle) * z - xstep * (vox->fbwidth >> 1);
+       y = vox->y + COS(vox->angle) * z - ystep * (vox->fbwidth >> 1);
        for(i=0; i<vox->fbwidth; i++) {
                tx = (x >> 16) & vox->xmask;
                ty = (y >> 16) & vox->ymask;
 
-               hval = (vox->height[(ty << vox->xshift) + tx] - vox->vheight) * vox->proj_dist / y;
+               hval = vox->height[(ty << vox->xshift) + tx] - 80;
+               hval = hval * 160 / (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;
 
@@ -220,8 +223,50 @@ void vox_render_slice(struct voxscape *vox, int n)
                                *fbptr = color;
                                fbptr += vox->fbwidth;
                        }
+                       vox->coltop[i] = hval;
                }
 
                x += xstep;
+               y += ystep;
+       }
+}
+
+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;
+               }
        }
 }