
GlUseProgram ( programID ) // Compute the MVP matrix from keyboard and mouse inputĬomputeMatricesFromInputs () glm :: mat4 ProjectionMatrix = getProjectionMatrix () glm :: mat4 ViewMatrix = getViewMatrix () glm :: mat4 ModelMatrix = glm :: mat4 ( 1.0 ) glm :: mat4 ModelViewMatrix = ViewMatrix * ModelMatrix glm :: mat3 ModelView3x3Matrix = glm :: mat3 ( ModelViewMatrix ) // Take the upper-left part of ModelViewMatrix GlClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) // Use our shader Here is the algorithm : if we note deltaPos1 and deltaPos2 two edges of our triangle, and deltaUV1 and deltaUV2 the corresponding differences in UVs, we can express our problem with the following equation : Since we need 3 vectors to define a basis, we must also compute the bitangent B (which is any other tangent vector, but if everything is perpendicular, math is simpler) : The standard method is to orient the tangent in the same direction that our texture coordinates : Which one should we choose ? In theory, any, but we have to be consistent with the neighbors to avoid introducing ugly edges. Next we need a tangent, T : a vector parallel to the surface. It’s represented in blue, just like the overall color of the normal map : We already have our UP vector : it’s the normal, given by Blender or computed from the triangle by a simple cross product. You are now so familiar with matrices that you know that in order to define a space (in our case, the tangent space), we need 3 vectors.
AFFINITY PHOTO. BUMP MAP HOW TO
This texture is mapped just like the diffuse one the big problem is how to convert our normal, which is expressed in the space each individual triangle (tangent space, also called image space), in model space (since this is what is used in our shading equation).

As usual, X is right in the plane of the texture, Y is up (again in the plane of the texture), thus given the right hand rule Z point to the “outside” of the plane of the texture. The texture has a general blue tone because overall, the normal is towards the “outside of the surface”. Normal = ( 2 * color ) - 1 // on each component In each RGB texel is encoded a XYZ vector : each colour component is between 0 and 1, and each vector component is between -1 and 1, so this simple mapping goes from the texel to the normal :

The basic idea of normal mapping is to give normals similar variations.

One caveat is that until now, we only had one normal per vertex : inside each triangle, they vary smoothly, on the opposite to the colour, which samples a texture. Since Tutorial 8 : Basic shading, you know how to get decent shading using triangle normals. Welcome for our 13th tutorial ! Today we will talk about normal mapping.
