Recently in use OpenGL toss normal Mapping. To be blunt is to have a texture that stores the normal vector. In the calculation of illumination, the normal vector sampled in the texture is used instead of the original normal vector of the geometry for illumination calculation. The texture of this storage method vector is called normal Map. Sometimes the scene resources will not give you the normal map, but give you a height map, which only stores a channel, is [0,1] gray value, can understand the surface roughness of the geometry of the height, you can generate a normal map according to this height map. The build process is as follows:
Uniform sampler2d heightmaptex;float h21 = Textureoffset (HEIGHTMAPTEX,COORD,IVEC2 (1,0)). R;float h01 = Textureoffset ( HEIGHTMAPTEX,COORD,IVEC2 ( -1,0)). R;float H10 = Textureoffset (HEIGHTMAPTEX,COORD,IVEC2 (0,-1)). R;float H12 = Textureoffset (HEIGHTMAPTEX,COORD,IVEC2 (0,1)). R;VEC3 VR = normalize (VEC3 (2.0f,0.0f,h21-h01)); VEC3 vt = Normalize (vec3 (0.0F,2.0F,H12-H10)); /Normal in tangent spacevec3 normal = normalize (cross (VR,VT));
The normal vector, which is sampled directly from normal map and calculated from height map, is in tangent space. It is worth noting that the value of each axis of the normal vector obtained directly from normal map is the normal vector that is scaled to [0,1], which is to be restored before it can be used, and the method computed from the heightmap may be used directly. The scaling and restore process is as follows:
Zoom normal = normal * 0.5f + VEC3 (0.5f);//restore normal = normal * 2.0F-VEC3 (1.0f);
In the light calculation, we need the direction of the light Lightdir, the direction of the angle of view viewdir, and the normal vector n, these vectors must be converted into the same coordinate system for calculation. In order to improve computational efficiency, we usually transform lightdir and viewdir into tangent space in vertexshader, pass interpolation to fragment shader, and then sample normal map. After obtaining the normal vector of tangent space, the illumination is calculated.
For tangent space to object space transformation matrix construction, you can refer to the following links: http://www.terathon.com/code/tangent.html
OpenGL Normal Mapping on the blind toss of thoughts