| 1 |
// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 |
|
| 4 |
mat3 calcWorldSpaceToTangentSpaceMatrix(const in vec3 wNormal, const in vec4 wTangent) |
| 5 |
{ |
| 6 |
// Make the tangent truly orthogonal to the normal by using Gram-Schmidt. |
| 7 |
// This allows to build the tangentMatrix below by simply transposing the |
| 8 |
// tangent -> eyespace matrix (which would now be orthogonal) |
| 9 |
vec3 wFixedTangent = normalize(wTangent.xyz - dot(wTangent.xyz, wNormal) * wNormal); |
| 10 |
|
| 11 |
// Calculate binormal vector. No "real" need to renormalize it, |
| 12 |
// as built by crossing two normal vectors. |
| 13 |
// To orient the binormal correctly, use the fourth coordinate of the tangent, |
| 14 |
// which is +1 for a right hand system, and -1 for a left hand system. |
| 15 |
vec3 wBinormal = cross(wNormal, wFixedTangent.xyz) * wTangent.w; |
| 16 |
|
| 17 |
// Construct matrix to transform from world space to tangent space |
| 18 |
// This is the transpose of the tangentToWorld transformation matrix |
| 19 |
mat3 tangentToWorldMatrix = mat3(wFixedTangent, wBinormal, wNormal); |
| 20 |
mat3 worldToTangentMatrix = transpose(tangentToWorldMatrix); |
| 21 |
return worldToTangentMatrix; |
| 22 |
} |
| 23 |
|