#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;
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 */
{
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
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;
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
}
}
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;
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)
{
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))
{