OpenGL Normal Mapping on the blind toss of thoughts

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.