0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / Shaders / TangentSpaceNormal.glsl
1 //! Calculates transformation from tangent space and apply it to value from normal map to get normal in object space
2 vec3 TangentSpaceNormal (in mat2 theDeltaUVMatrix,
3                          in mat2x3 theDeltaVectorMatrix,
4                          in vec3 theNormalMapValue,
5                          in vec3 theNormal,
6                          in bool theIsInverse)
7 {
8   theNormalMapValue = normalize(theNormalMapValue * 2.0 - vec3(1.0));
9   // Inverse matrix
10   theDeltaUVMatrix = mat2 (theDeltaUVMatrix[1][1], -theDeltaUVMatrix[0][1], -theDeltaUVMatrix[1][0], theDeltaUVMatrix[0][0]);
11   theDeltaVectorMatrix = theDeltaVectorMatrix * theDeltaUVMatrix;
12   // Gram-Schmidt orthogonalization
13   theDeltaVectorMatrix[1] = normalize(theDeltaVectorMatrix[1] - dot(theNormal, theDeltaVectorMatrix[1]) * theNormal);
14   theDeltaVectorMatrix[0] = cross(theDeltaVectorMatrix[1], theNormal);
15   float aDirection = theIsInverse ? -1.0 : 1.0;
16   return mat3 (aDirection * theDeltaVectorMatrix[0], aDirection * theDeltaVectorMatrix[1], theNormal) * theNormalMapValue;
17 }