Original Post
There is something slightly wrong with my algorithm that converts a grayscale height map into a normal, per pixel. I wanted to stay away from going into tangent space, and go directly into world space (which is what the pixel shader in HLSL is in).
Anyway, given a normal, the height map is sampled in 5 places, the current pixel and the 4-connected pixels surrounding it. These values form an "offset vector" that is transformed into world space, added to the normal, and the normal is normalized and used for lighting calculations.
This creates nice looking bump-mapped surfaces, but there is something slightly off about the lighting. Any ideas?
Anyway, given a normal, the height map is sampled in 5 places, the current pixel and the 4-connected pixels surrounding it. These values form an "offset vector" that is transformed into world space, added to the normal, and the normal is normalized and used for lighting calculations.
float me = tex2D(heightMapSampler,IN.tex).x;
float n = tex2D(heightMapSampler,float2(IN.tex.x,IN.tex.y+1.0/heightMapSizeY)).x;
float s = tex2D(heightMapSampler,float2(IN.tex.x,IN.tex.y-1.0/heightMapSizeY)).x;
float e = tex2D(heightMapSampler,float2(IN.tex.x+1.0/heightMapSizeX,IN.tex.y)).x;
float w = tex2D(heightMapSampler,float2(IN.tex.x-1.0/heightMapSizeX,IN.tex.y)).x;
float3 normalOffset = bumpHeightScale*float3((n-me)-(s-me),(e-me)-(w-me),0);
mul(normalOffset,World);
norm += normalOffset;
norm = normalize(norm);
This creates nice looking bump-mapped surfaces, but there is something slightly off about the lighting. Any ideas?