Skip to content

Commit

Permalink
UPBGE: Add full support of inverse before GLSL 140 in shaders.
Browse files Browse the repository at this point in the history
Previously only inverse(mat4) was implemented if the GLSL version was under 140.
Now as this implementation is also used by custom shaders, the other inverse
overloads shoudl be implemented.

So the full version from https://github.com/stackgl/glsl-inverse/blob/master/index.glsl
is copied.
  • Loading branch information
panzergame committed Jan 21, 2017
1 parent e169a26 commit 81d0d04
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions source/blender/gpu/shaders/gpu_shader_lib.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
* copied from https://github.com/stackgl/glsl-inverse/blob/master/index.glsl
*/

float inverse(float m) {
return 1.0 / m;
}

mat2 inverse(mat2 m) {
return mat2(m[1][1],-m[0][1],
-m[1][0], m[0][0]) / (m[0][0]*m[1][1] - m[0][1]*m[1][0]);
}

mat3 inverse(mat3 m) {
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];

float b01 = a22 * a11 - a12 * a21;
float b11 = -a22 * a10 + a12 * a20;
float b21 = a21 * a10 - a11 * a20;

float det = a00 * b01 + a01 * b11 + a02 * b21;

return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
}

mat4 inverse(mat4 m) {
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3],
a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3],
Expand Down

1 comment on commit 81d0d04

@youle31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome

Please sign in to comment.