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