Reputation: 115
I want to generate the normals for vertices displaced by a height map. I have the corresponding normal map however examples I've seen show the the normal vector is equal to the RGB values of the pixel on the normal map. But this means all the normal vectors are positive, when some vectors should have negative values. How would you calculate these using the normal map?
Thanks!
Upvotes: 0
Views: 1580
Reputation: 51923
You just shift the range to <-1.0,+1.0>
so
if you got color channels in range <0.0,1.0>
// a)
nx=(2.0*r)-1.0
ny=(2.0*g)-1.0
nz=(2.0*b)-1.0
or:
// b)
nx=2.0*(r-0.5)
ny=2.0*(g-0.5)
nz=2.0*(b-0.5)
if you got 8bit per channel then the range is <0,255>
nx=(float(r)/127.5)-1.0
ny=(float(g)/127.5)-1.0
nz=(float(b)/127.5)-1.0
If you look at the normal map image you should see the bluish colors because neutral normal=(0,0,1)
pointing up from a flat face is encoded as color=(r=0.5,g=0.5,b=1.0)
like here:
also have a look here: Normal mapping gone horribly wrong where the normal is computed from such texture in GLSL by #1b method:
const vec4 v05=vec4(0.5,0.5,0.5,0.5);
texture2D(txr_normal,pixel_txr.st)-v05)*2.0;
Also the (r,g,b)
can be mapped to (nz,ny,nx)
instead of (nx,ny,nz)
in that case just swap r,b
(the normal map is then red-ish instead)
Upvotes: 1