foo
[voxscape] / src / voxscape.c
diff --git a/src/voxscape.c b/src/voxscape.c
new file mode 100644 (file)
index 0000000..b9a18c3
--- /dev/null
@@ -0,0 +1,188 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <math.h>
+#include <imago2.h>
+#include "voxscape.h"
+
+enum {
+       SLICELEN        = 1
+};
+
+struct voxscape {
+       int xsz, ysz;
+       unsigned char *height;
+       uint32_t *color;
+
+       /* framebuffer */
+       uint32_t *fb;
+       int fbwidth, fbheight;
+       int *coltop;
+
+       /* view */
+       int32_t x, y, angle;
+
+       /* projection */
+       int fov, znear, zfar;
+       int nslices;
+       int32_t *slicelen;      /* 24.8 */
+
+       unsigned int valid;
+};
+
+struct voxscape *vox_create(int xsz, int ysz)
+{
+       struct voxscape *vox;
+
+       if(!(vox = calloc(1, sizeof *vox))) {
+               return 0;
+       }
+       if(!(vox->height = calloc(1, xsz * ysz))) {
+               fprintf(stderr, "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz);
+               free(vox);
+               return 0;
+       }
+       if(!(vox->color = calloc(xsz * ysz, sizeof *vox->color))) {
+               fprintf(stderr, "vox_create: failed to allocate %dx%d color map\n", xsz, ysz);
+               free(vox->height);
+               free(vox);
+               return 0;
+       }
+       vox->xsz = xsz;
+       vox->ysz = ysz;
+
+       return vox;
+}
+
+struct voxscape *vox_open(const char *hfile, const char *cfile)
+{
+       unsigned char *hpix;
+       uint32_t *cpix;
+       int width, height, cwidth, cheight;
+       struct voxscape *vox;
+
+       if(!(hpix = img_load_pixels(hfile, &width, &height, IMG_FMT_GREY8))) {
+               fprintf(stderr, "vox_open: failed to load heightmap: %s\n", hfile);
+               return 0;
+       }
+       if(!(cpix = img_load_pixels(cfile, &cwidth, &cheight, IMG_FMT_RGBA32))) {
+               fprintf(stderr, "vox_open: failed to load color map: %s\n", cfile);
+               img_free_pixels(hpix);
+               return 0;
+       }
+       if(cwidth != width || cheight != height) {
+               img_free_pixels(hpix);
+               img_free_pixels(cpix);
+               fprintf(stderr, "vox_open: %s and %s have different dimensions\n", hfile, cfile);
+               return 0;
+       }
+
+       if(!(vox = vox_create(width, height))) {
+               img_free_pixels(hpix);
+               img_free_pixels(cpix);
+               return 0;
+       }
+       memcpy(vox->height, hpix, width * height);
+       memcpy(vox->color, cpix, width * height * sizeof *vox->color);
+
+       img_free_pixels(hpix);
+       img_free_pixels(cpix);
+
+       return vox;
+}
+
+void vox_free(struct voxscape *vox)
+{
+       if(!vox) return;
+
+       free(vox->color);
+       free(vox->height);
+       free(vox->coltop);
+       free(vox->slicelen);
+       free(vox);
+}
+
+void vox_framebuf(struct voxscape *vox, int xres, int yres, uint32_t *fb)
+{
+       if(xres != vox->fbwidth) {
+               free(vox->coltop);
+               if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
+                       fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
+                       return;
+               }
+       }
+       vox->fb = fb;
+       vox->fbwidth = xres;
+       vox->fbheight = yres;
+}
+
+void vox_view(struct voxscape *vox, int32_t x, int32_t y, int32_t angle)
+{
+       vox->x = x;
+       vox->y = y;
+       vox->angle = angle;
+       /* TODO precalc stuff */
+
+       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;
+
+       vox->nslices = vox->zfar - vox->znear;
+       free(vox->slicelen);
+       if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
+               fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
+               return;
+       }
+
+       valid &= ~SLICELEN;
+}
+
+/* algorithm:
+ * calculate extents of horizontal equidistant line from the viewer based on fov
+ * for each column step along this line and compute height for each pixel
+ * fill the visible (top) part of each column
+ */
+
+void vox_render(struct voxscape *vox)
+{
+       int i;
+
+       vox_begin(vox);
+       for(i=0; i<vox->nslices; i++) {
+               vox_render_slice(vox, i);
+       }
+}
+
+void vox_begin(struct voxscape *vox)
+{
+       int i;
+
+       for(i=0; i<vox->fbwidth; i++) {
+               vox->coltop[i] = vox->fbheight;
+       }
+
+       if(!(vox->valid & SLICELEN)) {
+               float theta = (float)vox->angle * 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->valid |= SLICELEN;
+       }
+}
+
+void vox_render_slice(struct voxscape *vox, int n)
+{
+       int32_t len;
+
+       len = vox->slicelen[n];
+
+       /* TODO cont. */
+}