]> git.xonotic.org Git - xonotic/xonotic.git/blobdiff - misc/tools/fft-normalmap-to-heightmap.c
normal/heightmap tool: allow setting the bias relatively to the median
[xonotic/xonotic.git] / misc / tools / fft-normalmap-to-heightmap.c
index 2143407ae30acc23ea3f0f21f4008c79f1e005bf..e025150b64448bdc2404e489cd1a1cadcedaa229 100644 (file)
 
 #define TWO_PI (4*atan2(1,1) * 2)
 
-void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, double scale, double offset, const double *filter, int filterw, int filterh)
+int floatcmp(const void *a_, const void *b_)
+{
+       float a = *(float *)a_;
+       float b = *(float *)b_;
+       if(a < b)
+               return -1;
+       if(a > b)
+               return +1;
+       return 0;
+}
+
+void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h, double scale, double offset, const double *filter, int filterw, int filterh, int renormalize, double highpass, int use_median)
 {
        int x, y;
        int i, j;
@@ -74,11 +85,26 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h,
                imgspace1[(w*y+x)] =  nx / nz * w; /* = dz/dx */
                imgspace2[(w*y+x)] = -ny / nz * h; /* = dz/dy */
 #else
-               imgspace1[(w*y+x)][0] =  nx / nz; /* = dz/dx */
+               imgspace1[(w*y+x)][0] =  nx / nz * w; /* = dz/dx */
                imgspace1[(w*y+x)][1] = 0;
-               imgspace2[(w*y+x)][0] = -ny / nz; /* = dz/dy */
+               imgspace2[(w*y+x)][0] = -ny / nz * h; /* = dz/dy */
                imgspace2[(w*y+x)][1] = 0;
 #endif
+
+               if(renormalize)
+               {
+                       double v = nx * nx + ny * ny + nz * nz;
+                       if(v > 0)
+                       {
+                               v = 1/sqrt(v);
+                               nx *= v;
+                               ny *= v;
+                               nz *= v;
+                               map[(w*y+x)*4+2] = floor(nx * 127.5 + 128);
+                               map[(w*y+x)*4+1] = floor(ny * 127.5 + 128);
+                               map[(w*y+x)*4+0] = floor(nz * 127.5 + 128);
+                       }
+               }
        }
 
        /* see http://www.gamedev.net/community/forums/topic.asp?topic_id=561430 */
@@ -91,6 +117,10 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h,
        {
                fx = x * 1.0 / w;
                fy = y * 1.0 / h;
+               if(fx > 0.5)
+                       fx -= 1;
+               if(fy > 0.5)
+                       fy -= 1;
                if(filter)
                {
                        // discontinous case
@@ -128,19 +158,20 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h,
                        for(i = -filterh / 2; i <= filterh / 2; ++i)
                                for(j = -filterw / 2; j <= filterw / 2; ++j)
                                {
-                                       response_x[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(TWO_PI * (j * fx + i * fy));
-                                       response_x[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(TWO_PI * (j * fx + i * fy));
-                                       response_y[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(TWO_PI * (i * fx + j * fy));
-                                       response_y[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(TWO_PI * (i * fx + j * fy));
+                                       response_x[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (j * fx + i * fy));
+                                       response_x[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (j * fx + i * fy));
+                                       response_y[0] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * cos(-TWO_PI * (i * fx + j * fy));
+                                       response_y[1] += filter[(i + filterh / 2) * filterw + j + filterw / 2] * sin(-TWO_PI * (i * fx + j * fy));
                                }
 
-                       sum = response_x[0] * response_x[0] + response_x[1] + response_x[1]
-                           + response_y[0] * response_y[0] + response_y[1] + response_y[1];
+                       sum = response_x[0] * response_x[0] + response_x[1] * response_x[1]
+                           + response_y[0] * response_y[0] + response_y[1] * response_y[1];
 
                        if(sum > 0)
                        {
-                               freqspace1[(w*y+x)][0] = (response_x[0] * freqspace1[(w*y+x)][0] + response_x[1] * freqspace1[(w*y+x)][1] + response_y[0] * freqspace2[(w*y+x)][0] + response_y[1] * freqspace2[(w*y+x)][1]) / sum;
-                               freqspace1[(w*y+x)][1] = (response_x[0] * freqspace1[(w*y+x)][1] - response_x[1] * freqspace1[(w*y+x)][0] + response_y[0] * freqspace2[(w*y+x)][1] - response_y[1] * freqspace2[(w*y+x)][0]) / sum;
+                               double s = freqspace1[(w*y+x)][0];
+                               freqspace1[(w*y+x)][0] = (response_x[0] * s                      + response_x[1] * freqspace1[(w*y+x)][1] + response_y[0] * freqspace2[(w*y+x)][0] + response_y[1] * freqspace2[(w*y+x)][1]) / sum;
+                               freqspace1[(w*y+x)][1] = (response_x[0] * freqspace1[(w*y+x)][1] - response_x[1] * s                      + response_y[0] * freqspace2[(w*y+x)][1] - response_y[1] * freqspace2[(w*y+x)][0]) / sum;
                        }
                        else
                        {
@@ -152,10 +183,6 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h,
                else
                {
                        // continuous integration case
-                       if(fx > 0.5)
-                               fx -= 1;
-                       if(fy > 0.5)
-                               fy -= 1;
                        /* these must have the same sign as fx and fy (so ffx*fx + ffy*fy is nonzero), otherwise do not matter */
                        /* it basically decides how artifacts are distributed */
                        ffx = fx;
@@ -177,6 +204,22 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h,
                                freqspace1[(w*y+x)][0] = 0;
                                freqspace1[(w*y+x)][1] = 0;
                        }
+#endif
+               }
+               if(highpass > 0)
+               {
+                       double f1 = (fabs(fx)*highpass);
+                       double f2 = (fabs(fy)*highpass);
+                       // if either of them is < 1, phase out (min at 0.5)
+                       double f =
+                               (f1 <= 0.5 ? 0 : (f1 >= 1 ? 1 : ((f1 - 0.5) * 2.0)))
+                               *
+                               (f2 <= 0.5 ? 0 : (f2 >= 1 ? 1 : ((f2 - 0.5) * 2.0)));
+#ifdef C99
+                       freqspace1[(w*y+x)] *= f;
+#else
+                       freqspace1[(w*y+x)][0] *= f;
+                       freqspace1[(w*y+x)][1] *= f;
 #endif
                }
        }
@@ -192,7 +235,8 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h,
                v = creal(imgspace1[(w*y+x)] /= pow(w*h, 1.5));
 #else
                v = (imgspace1[(w*y+x)][0] /= pow(w*h, 1.5));
-               imgspace1[(w*y+x)][1] /= pow(w*h, 1.5);
+               // imgspace1[(w*y+x)][1] /= pow(w*h, 1.5);
+               // this value is never used
 #endif
                if(v < vmin || (x == 0 && y == 0))
                        vmin = v;
@@ -261,6 +305,38 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h,
                scale = 2 / (vmax - vmin);
                offset = -(vmax + vmin) / (vmax - vmin);
        }
+       else if(use_median)
+       {
+               // negative scale = match median to offset
+
+               fprintf(stderr, "Calculating median...\n");
+
+               float *medianbuf = malloc(sizeof(float) * w * h);
+               float vmed;
+
+               fprintf(stderr, "  converting...\n");
+               /* renormalize, find min/max */
+               for(y = 0; y < h; ++y)
+               for(x = 0; x < w; ++x)
+               {
+#ifdef C99
+                       v = creal(imgspace1[(w*y+x)]);
+#else
+                       v = (imgspace1[(w*y+x)][0]);
+#endif
+                       medianbuf[w*y+x] = v;
+               }
+               fprintf(stderr, "  sorting...\n");
+               qsort(medianbuf, w*h, sizeof(*medianbuf), floatcmp);
+               fprintf(stderr, "  done.\n");
+               if(w*h % 2)
+                       vmed = medianbuf[(w*h-1)/2];
+               else
+                       vmed = (medianbuf[(w*h)/2] + medianbuf[(w*h-2)/2]) * 0.5;
+
+               // we actually want (v - vmed) * scale + offset
+               offset -= vmed * scale;
+       }
 
        printf("Min: %f\nAvg: %f\nMax: %f\nScale: %f\nOffset: %f\nScaled-Min: %f\nScaled-Avg: %f\nScaled-Max: %f\n", 
                vmin, 0.0, vmax, scale, offset, vmin * scale + offset, offset, vmax * scale + offset);
@@ -1016,6 +1092,9 @@ int main(int argc, char **argv)
        const char *infile, *outfile, *reffile;
        double scale, offset;
        int nmaplen, w, h;
+       int use_median = 0;
+       int renormalize = 0;
+       double highpass = 0;
        unsigned char *nmapdata, *nmap, *refmap;
        const char *filtertype;
        const double *filter = NULL;
@@ -1059,6 +1138,14 @@ int main(int argc, char **argv)
        else
                reffile = NULL;
 
+       // experimental features
+       if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_RENORMALIZE"))
+               renormalize = atoi(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_RENORMALIZE"));
+       if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_HIGHPASS"))
+               highpass = atof(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_HIGHPASS"));
+       if(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_USE_MEDIAN"))
+               use_median = atof(getenv("FFT_NORMALMAP_TO_HEIGHTMAP_USE_MEDIAN"));
+
        nmapdata = FS_LoadFile(infile, &nmaplen);
        if(!nmapdata)
        {
@@ -1120,7 +1207,7 @@ int main(int argc, char **argv)
                        hmap_to_nmap(nmap, image_width, image_height, -scale-1, offset);
        }
        else
-               nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset, filter, filterw, filterh);
+               nmap_to_hmap(nmap, refmap, image_width, image_height, scale, offset, filter, filterw, filterh, renormalize, highpass, use_median);
 
        if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
        {