fixed the tunnel precalc to produce a continuous 10 bit depth value
[gbajam21] / tools / tungen.c
index 4849ec3..d1ff400 100644 (file)
@@ -1,13 +1,23 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <stdint.h>
 #include <math.h>
 
+struct vec2 {
+       int x, y;
+};
+
 int main(int argc, char **argv)
 {
-       int i, j, xsz = 240, ysz = 160, texsz = 128;
+       int i, j, frm, imgrad, out_nlines, xsz = 240, ysz = 160, texsz = 128;
+       int half_y = 0;
+       int center = 0;
        struct vec2 *tunbuf, *tun;
-       float aspect;
+       float prev_r;
+       struct vec2 *buf, *ptr;
+       char *endp;
+       int num_frames = 1;
 
        for(i=1; i<argc; i++) {
                if(argv[i][0] == '-') {
@@ -27,6 +37,33 @@ int main(int argc, char **argv)
                                }
                                break;
 
+                       case 'y':
+                               half_y = 1;
+                               break;
+
+                       case 'c':
+                               if(!argv[++i]) {
+                                       fprintf(stderr, "-c must be followed by a center pixel\n");
+                                       return 1;
+                               }
+                               center = strtol(argv[i], &endp, 10);
+                               if(endp == argv[i]) {
+                                       fprintf(stderr, "-c invalid center position: %s\n", argv[i]);
+                                       return 1;
+                               }
+                               break;
+
+                       case 'n':
+                               if(!argv[++i]) {
+                                       fprintf(stderr, "-n must be followed by the number of frames\n");
+                                       return 1;
+                               }
+                               if(!(num_frames = atoi(argv[i]))) {
+                                       fprintf(stderr, "-n invalid number of frames: %s\n", argv[i]);
+                                       return 1;
+                               }
+                               break;
+
                        default:
                                goto invalopt;
                        }
@@ -36,37 +73,76 @@ invalopt:   fprintf(stderr, "invalid argument: %s\n", argv[i]);
                }
        }
 
+       out_nlines = half_y ? ysz / 2 : ysz;
+
+       if(!(buf = malloc(xsz * ysz * sizeof *buf))) {
+               perror("failed to allocate buffer");
+               return 1;
+       }
+       imgrad = sqrt(xsz * xsz + ysz * ysz);
+
        FILE *fp = fopen("tun_preview.ppm", "wb");
        if(fp) {
-               fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
+               fprintf(fp, "P6\n%d %d\n255\n", xsz, out_nlines * num_frames);
        }
 
-       aspect = (float)xsz / (float)ysz;
 
-       tun = tunbuf;
-       for(i=0; i<ysz; i++) {
-               float y = 2.0f * (float)i / (float)(ysz - 1) - 1.0f;
-               for(j=0; j<xsz; j++) {
-                       float x = (2.0f * (float)j / (float)(xsz - 1) - 1.0f) * aspect;
+       for(frm=0; frm<num_frames; frm++) {
+               int coffs = num_frames > 1 ? frm * center / (num_frames - 1) : center;
+
+               memset(buf, 0xff, xsz * ysz * sizeof *buf);
 
-                       float r = sqrt(x * x + y * y);
-                       float theta = atan2(y, x);
+#define UDIV   2048
+#define VDIV   32768
+               prev_r = 0.0f;
+#pragma omp parallel for private(i, j, prev_r, ptr) schedule(dynamic)
+               for(i=0; i<VDIV; i++) {
+                       float v = (float)(VDIV - i) / (float)VDIV;
+                       float r = 4.0 / v + 16;
+                       float z = v * coffs;
 
-                       float u = 0.5f * theta / M_PI + 0.5f;
-                       float v = r;
+                       /* don't bother drawing rings < 1 pixel apart */
+                       if(r < 0 || fabs(r - prev_r) < 0.05) continue;
 
-                       uint32_t out = ((uint32_t)(u * 65535.0f) & 0xffff) |
-                               (((uint32_t)(v * 65535.0f) & 0xffff) << 16);
-                       fwrite(&out, sizeof out, 1, stdout);
+                       for(j=0; j<UDIV; j++) {
+                               float u = (float)j / (float)(UDIV - 1);
+                               float theta = 2.0f * u * M_PI;
 
-                       if(fp) {
-                               int cr = (int)(u * 2048.0f) & 0xff;
-                               int cb = (int)(r * 2048.0f) & 0xff;
-                               fputc(cr, fp);
-                               fputc(0, fp);
-                               fputc(cb, fp);
+                               int x = (int)(cos(theta) * r - z) + xsz / 2;
+                               int y = (int)(sin(theta) * r) + ysz / 2;
+
+                               if(x >= 0 && x < xsz && y >= 0 && y < ysz) {
+                                       ptr = buf + y * xsz + x;
+                                       ptr->x = (j << 8) / UDIV;
+                                       ptr->y = ((VDIV - i) << 11) / VDIV;
+                               }
                        }
+                       prev_r = r;
                }
+
+               ptr = buf;
+               for(i=0; i<out_nlines; i++) {
+                       for(j=0; j<xsz; j++) {
+                               int u = ptr->x;
+                               int v = ptr->y;
+                               int r = (u << 3) & 0xff;
+                               int g = (v >> 3) & 0xff;
+
+                               /*if(v > 2.0) r = g = b = 0;*/
+
+                               ptr++;
+
+                               uint16_t out = ((uint16_t)u & 0x3f) | (((uint16_t)v & 0x3ff) << 6);
+                               fwrite(&out, sizeof out, 1, stdout);
+
+                               if(fp) {
+                                       fputc(r, fp);
+                                       fputc(g, fp);
+                                       fputc(0, fp);
+                               }
+                       }
+               }
+
        }
        fflush(stdout);