]> git.xonotic.org Git - xonotic/xonotic.git/blobdiff - misc/tools/fft-normalmap-to-heightmap.c
add a highpass feature too
[xonotic/xonotic.git] / misc / tools / fft-normalmap-to-heightmap.c
index 7b72fcbd8808a4678ed1e868e5ed1bbf8a3c0ecc..c936867434ac3e4d2b0f7a496d033972c9c63eea 100644 (file)
@@ -35,7 +35,7 @@
 
 #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)
+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 x, y;
        int i, j;
@@ -74,11 +74,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 = 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 +106,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
@@ -122,16 +141,37 @@ void nmap_to_hmap(unsigned char *map, const unsigned char *refmap, int w, int h,
                        else
                                freqspace1[(w*y+x)] = 0;
 #else
-                       // not yet implemented
+                       fftw_complex response_x = {0, 0};
+                       fftw_complex response_y = {0, 0};
+                       double sum;
+                       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));
+                               }
+
+                       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)
+                       {
+                               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
+                       {
+                               freqspace1[(w*y+x)][0] = 0;
+                               freqspace1[(w*y+x)][1] = 0;
+                       }
 #endif
                }
                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;
@@ -153,6 +193,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
                }
        }
@@ -168,7 +224,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;
@@ -992,6 +1049,8 @@ int main(int argc, char **argv)
        const char *infile, *outfile, *reffile;
        double scale, offset;
        int nmaplen, w, h;
+       int renormalize = 0;
+       double highpass = 0;
        unsigned char *nmapdata, *nmap, *refmap;
        const char *filtertype;
        const double *filter = NULL;
@@ -1035,6 +1094,11 @@ int main(int argc, char **argv)
        else
                reffile = NULL;
 
+       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"));
+
        nmapdata = FS_LoadFile(infile, &nmaplen);
        if(!nmapdata)
        {
@@ -1096,7 +1160,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);
 
        if(!Image_WriteTGABGRA(outfile, image_width, image_height, nmap))
        {