Source code: http://yanonsoftware.googlepages.com/ShaderSimpler.zip
Generate normal map from texture
Normal map must be generated from the height map corresponding to a color map. In order to calculate the I, J point's normal, we can first calculate the S, T direction Tangent Vector Based on the height difference:
S (I, j) = <1, 0, AH (I + 1, J)-Ah (I-1, j)>
T (I, j) = <0, 1, AH (I, j + 1)-Ah (I, J-1)>
A is a scaling factor. Normal can be obtained from the two tangent vectors.
N (I, j) = normalize (Cross (S, T) = cross (S, T)/length (Cross (S, T) = <-SZ, -tz, 1>/SQRT (SZ ^ 2 + TZ ^ 2 + 1)
You can then store this vector in the texture file using RGB encoding. In this example, we use a tool provided by NVIDIA to generate a normal map.
Calculate the tangent space
This computation is a little complicated. I remember that NVIDIA had clearly written a document earlier, but I couldn't find it. If anyone knows, please give me a link. I found another solution in Eric Lengyel's book, which is also very thorough.
In this example, d3dxcomputetangent is called. You can also use the nv_meshmender provided in the nvidia sdk for computation.
Illumination computing
The light Computing Using bump map is nothing new than the previous phongshading example. In the preceding example, the normal value of each pixel is simply interpolated from the vertex normal. After the bump map is used, the normal value of each vertex can be searched from the texture; in addition, because this normal operation exists in the tangent space, we need to convert both light Dir and view dir to tangent space for calculation.
Capture: http://yanonsoftware.googlepages.com/bumpMap.jpg
Reference
Eric Lengyel, mathematical methods in 3D games and computer graphics
Normal map maps are generated using NVIDIA Photoshop Plugin-http://developer.nvidia.com/object/nv_texture_tools.html
Wolfgang Engel, implementing lighting models with HLSL -- http://www.gamasutra.com/features/20030418/engel_01.shtml