fog
[voxscape] / src / voxscape.c
index b9a18c3..60d80a0 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
@@ -14,19 +16,26 @@ struct voxscape {
        int xsz, ysz;
        unsigned char *height;
        uint32_t *color;
+       int xshift, xmask, ymask;
 
        /* framebuffer */
        uint32_t *fb;
        int fbwidth, fbheight;
        int *coltop;
+       int horizon;
 
        /* 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;
+
+       int zfog;       /* fog start Z (0: no fog) */
+       int fogcolor[3];
 
        unsigned int valid;
 };
@@ -52,6 +61,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 = 80;
+       vox->proj_dist = 4;     /* TODO */
+
        return vox;
 }
 
@@ -59,7 +80,7 @@ struct voxscape *vox_open(const char *hfile, const char *cfile)
 {
        unsigned char *hpix;
        uint32_t *cpix;
-       int width, height, cwidth, cheight;
+       int i, width, height, cwidth, cheight;
        struct voxscape *vox;
 
        if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
@@ -83,8 +104,12 @@ struct voxscape *vox_open(const char *hfile, const char *cfile)
                img_free_pixels(cpix);
                return 0;
        }
+
        memcpy(vox->height, hpix, width * height);
-       memcpy(vox->color, cpix, width * height * sizeof *vox->color);
+
+       for(i=0; i<width*height; i++) {
+               vox->color[i] = cpix[i] & 0xffffff;     /* discard alpha */
+       }
 
        img_free_pixels(hpix);
        img_free_pixels(cpix);
@@ -103,7 +128,24 @@ void vox_free(struct voxscape *vox)
        free(vox);
 }
 
-void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb)
+void vox_fog(struct voxscape *vox, int zstart, uint32_t color)
+{
+       vox->zfog = zstart;
+
+       vox->fogcolor[0] = color >> 16;
+       vox->fogcolor[1] = (color >> 8) & 0xff;
+       vox->fogcolor[2] = color & 0xff;
+}
+
+int vox_height(struct voxscape *vox, int32_t x, int32_t y)
+{
+       int tx = (x >> 16) & vox->xmask;
+       int ty = (y >> 16) & vox->ymask;
+
+       return vox->height[(ty << vox->xshift) + tx];
+}
+
+void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb, int horizon)
 {
        if(xres != vox->fbwidth) {
                free(vox->coltop);
@@ -115,22 +157,25 @@ void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb)
        vox->fb = fb;
        vox->fbwidth = xres;
        vox->fbheight = yres;
+       vox->horizon = horizon >= 0 ? horizon : vox->fbheight / 2;
 }
 
-void vox_view(struct voxscape *vox, int32_t x, int32_t y, int32_t angle)
+void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
 {
+       if(h < 0) {
+               h = vox_height(vox, x, y) - h;
+       }
+
        vox->x = x;
        vox->y = y;
+       vox->vheight = h;
        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 +187,7 @@ void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
                return;
        }
 
-       valid &= ~SLICELEN;
+       vox->valid &= ~SLICELEN;
 }
 
 /* algorithm:
@@ -165,14 +210,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 * 256.0f)
+                       vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
                }
                vox->valid |= SLICELEN;
        }
@@ -180,9 +223,111 @@ 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, z, r, g, b;
+       int32_t x, y, len, xstep, ystep, fog;
+       uint32_t color;
+       uint32_t *fbptr;
+
+       if(vox->zfog > 0 && n > vox->zfog) {
+               fog = ((n - vox->zfog) << 8) / (vox->zfar - vox->zfog);
+       } else {
+               fog = 0;
+       }
+
+       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 - 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;
+               hval = hval * 160 / (vox->znear + n) + vox->horizon;
+               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;
+
+                       if(fog > 0) {
+                               r = color >> 16;
+                               g = (color >> 8) & 0xff;
+                               b = color & 0xff;
+                               r = ((r << 8) + (vox->fogcolor[0] - r) * fog) >> 8;
+                               g = ((g << 8) + (vox->fogcolor[1] - g) * fog) >> 8;
+                               b = ((b << 8) + (vox->fogcolor[2] - b) * fog) >> 8;
+                               color = (r << 16) | (g << 8) | b;
+                       }
+
+                       for(j=0; j<colheight; j++) {
+                               *fbptr = color;
+                               fbptr += vox->fbwidth;
+                       }
+                       vox->coltop[i] = hval;
+               }
+
+               x += xstep;
+               y += ystep;
+       }
+}
+
+void vox_sky_solid(struct voxscape *vox, uint32_t color)
+{
+       int i, j, colheight;
+       uint32_t *fbptr;
 
-       len = vox->slicelen[n];
+       for(i=0; i<vox->fbwidth; i++) {
+               fbptr = vox->fb + i;
+               colheight = vox->fbheight - vox->coltop[i];
+               for(j=0; j<colheight; j++) {
+                       *fbptr = color;
+                       fbptr += vox->fbwidth;
+               }
+       }
+}
 
-       /* TODO cont. */
+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 - vox->horizon;
+       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;
+               }
+       }
 }