From 652cbac63d051d702595779c7e3bbc63aab533ff Mon Sep 17 00:00:00 2001 From: attilaz Date: Fri, 20 Sep 2019 11:29:40 +0200 Subject: [PATCH] added cofactor function to shaderlib.sh It is useful to transform normals. --- examples/common/shaderlib.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/common/shaderlib.sh b/examples/common/shaderlib.sh index 8289abb00..f0aee1924 100644 --- a/examples/common/shaderlib.sh +++ b/examples/common/shaderlib.sh @@ -392,4 +392,22 @@ vec2 texture2DBc5(sampler2D _sampler, vec2 _uv) #endif } +mat3 cofactor( mat4 _m ) +{ + // Reference: + // Cofactor of matrix. Use to transform normals. The code assumes the last column of _m is [0,0,0,1]. + // https://www.shadertoy.com/view/3s33zj + // https://github.com/graphitemaster/normals_revisited + return mat3( + _m[1][1]*_m[2][2]-_m[1][2]*_m[2][1], + _m[1][2]*_m[2][0]-_m[1][0]*_m[2][2], + _m[1][0]*_m[2][1]-_m[1][1]*_m[2][0], + _m[0][2]*_m[2][1]-_m[0][1]*_m[2][2], + _m[0][0]*_m[2][2]-_m[0][2]*_m[2][0], + _m[0][1]*_m[2][0]-_m[0][0]*_m[2][1], + _m[0][1]*_m[1][2]-_m[0][2]*_m[1][1], + _m[0][2]*_m[1][0]-_m[0][0]*_m[1][2], + _m[0][0]*_m[1][1]-_m[0][1]*_m[1][0] ); +} + #endif // __SHADERLIB_SH__