我要计算新的法线向量,在GLSL 1.4中,您可以使用以下公式: 正常= mat3(transpose(inverse(modelview)))* in_Normal; 但是我的GLSL版本是1.3,并且该版本中没有函数inverse。 您是否知道是否有替代方法而无需将整个函数编码为逆矩阵?
答案 0 :(得分:0)
假设矩阵的最后一行是[ 0 0 0 1 ]
(如果只使用旋转,缩放和平移,则应该使用),则可以简化计算。
代替:
normal = mat3(transpose(inverse(modelview))) * in_Normal
使用:
normal = transpose(inverse(mat3(modelview))) * in_Normal
然后:
mat3 transpose(mat3 matrix) {
vec3 row0 = matrix[0];
vec3 row1 = matrix[1];
vec3 row2 = matrix[2];
mat3 result = mat3(
vec3(row0.x, row1.x, row2.x),
vec3(row0.y, row1.y, row2.y),
vec3(row0.z, row1.z, row2.z)
);
return result;
}
float det(mat2 matrix) {
return matrix[0].x * matrix[1].y - matrix[0].y * matrix[1].x;
}
mat3 inverse(mat3 matrix) {
vec3 row0 = matrix[0];
vec3 row1 = matrix[1];
vec3 row2 = matrix[2];
vec3 minors0 = vec3(
det(mat2(row1.y, row1.z, row2.y, row2.z)),
det(mat2(row1.z, row1.x, row2.z, row2.x)),
det(mat2(row1.x, row1.y, row2.x, row2.y))
);
vec3 minors1 = vec3(
det(mat2(row2.y, row2.z, row0.y, row0.z)),
det(mat2(row2.z, row2.x, row0.z, row0.x)),
det(mat2(row2.x, row2.y, row0.x, row0.y))
);
vec3 minors2 = vec3(
det(mat2(row0.y, row0.z, row1.y, row1.z)),
det(mat2(row0.z, row0.x, row1.z, row1.x)),
det(mat2(row0.x, row0.y, row1.x, row1.y))
);
mat3 adj = transpose(mat3(minors0, minors1, minors2));
return (1.0 / dot(row0, minors0)) * adj;
}