| 1 |
// Copyright (C) 2018 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 |
#ifndef FP |
| 5 |
#define FP highp |
| 6 |
#endif |
| 7 |
|
| 8 |
FP mat3 calcWorldSpaceToTangentSpaceMatrix(const in FP vec3 wNormal, const in FP vec4 wTangent) |
| 9 |
{ |
| 10 |
// Make the tangent truly orthogonal to the normal by using Gram-Schmidt. |
| 11 |
// This allows to build the tangentMatrix below by simply transposing the |
| 12 |
// tangent -> eyespace matrix (which would now be orthogonal) |
| 13 |
FP vec3 wFixedTangent = normalize(wTangent.xyz - dot(wTangent.xyz, wNormal) * wNormal); |
| 14 |
|
| 15 |
// Calculate binormal vector. No "real" need to renormalize it, |
| 16 |
// as built by crossing two normal vectors. |
| 17 |
// To orient the binormal correctly, use the fourth coordinate of the tangent, |
| 18 |
// which is +1 for a right hand system, and -1 for a left hand system. |
| 19 |
FP vec3 wBinormal = cross(wNormal, wFixedTangent.xyz) * wTangent.w; |
| 20 |
|
| 21 |
// Construct matrix to transform from world space to tangent space |
| 22 |
// This is the transpose of the tangentToWorld transformation matrix |
| 23 |
FP mat3 tangentToWorldMatrix = mat3(wFixedTangent, wBinormal, wNormal); |
| 24 |
FP mat3 worldToTangentMatrix = transpose(tangentToWorldMatrix); |
| 25 |
return worldToTangentMatrix; |
| 26 |
} |