Home | History | Annotate | Download | only in glsl
      1 /* Copyright (c) 2005 - 2012 G-Truc Creation (www.g-truc.net)
      2  * Copyright  2012 Intel Corporation
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a copy
      5  * of this software and associated documentation files (the "Software"), to deal
      6  * in the Software without restriction, including without limitation the rights
      7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8  * copies of the Software, and to permit persons to whom the Software is
      9  * furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice shall be included in
     12  * all copies or substantial portions of the Software.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     20  * THE SOFTWARE.
     21  */
     22 
     23 #version 120
     24 mat2 inverse(mat2 m)
     25 {
     26    mat2 adj;
     27    adj[0][0] = m[1][1];
     28    adj[0][1] = -m[0][1];
     29    adj[1][0] = -m[1][0];
     30    adj[1][1] = m[0][0];
     31    float det = m[0][0] * m[1][1] - m[1][0] * m[0][1];
     32    return adj / det;
     33 }
     34 
     35 mat3 inverse(mat3 m)
     36 {
     37    mat3 adj;
     38    adj[0][0] = + (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
     39    adj[1][0] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
     40    adj[2][0] = + (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
     41    adj[0][1] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
     42    adj[1][1] = + (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
     43    adj[2][1] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
     44    adj[0][2] = + (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
     45    adj[1][2] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
     46    adj[2][2] = + (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
     47 
     48    float det = (+ m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
     49 		- m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
     50 		+ m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]));
     51 
     52    return adj / det;
     53 }
     54 
     55 mat4 inverse(mat4 m)
     56 {
     57    float SubFactor00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
     58    float SubFactor01 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
     59    float SubFactor02 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
     60    float SubFactor03 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
     61    float SubFactor04 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
     62    float SubFactor05 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
     63    float SubFactor06 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
     64    float SubFactor07 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
     65    float SubFactor08 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
     66    float SubFactor09 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
     67    float SubFactor10 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
     68    float SubFactor11 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
     69    float SubFactor12 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
     70    float SubFactor13 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
     71    float SubFactor14 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
     72    float SubFactor15 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
     73    float SubFactor16 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
     74    float SubFactor17 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
     75    float SubFactor18 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
     76 
     77    mat4 adj;
     78 
     79    adj[0][0] = + (m[1][1] * SubFactor00 - m[1][2] * SubFactor01 + m[1][3] * SubFactor02);
     80    adj[1][0] = - (m[1][0] * SubFactor00 - m[1][2] * SubFactor03 + m[1][3] * SubFactor04);
     81    adj[2][0] = + (m[1][0] * SubFactor01 - m[1][1] * SubFactor03 + m[1][3] * SubFactor05);
     82    adj[3][0] = - (m[1][0] * SubFactor02 - m[1][1] * SubFactor04 + m[1][2] * SubFactor05);
     83 
     84    adj[0][1] = - (m[0][1] * SubFactor00 - m[0][2] * SubFactor01 + m[0][3] * SubFactor02);
     85    adj[1][1] = + (m[0][0] * SubFactor00 - m[0][2] * SubFactor03 + m[0][3] * SubFactor04);
     86    adj[2][1] = - (m[0][0] * SubFactor01 - m[0][1] * SubFactor03 + m[0][3] * SubFactor05);
     87    adj[3][1] = + (m[0][0] * SubFactor02 - m[0][1] * SubFactor04 + m[0][2] * SubFactor05);
     88 
     89    adj[0][2] = + (m[0][1] * SubFactor06 - m[0][2] * SubFactor07 + m[0][3] * SubFactor08);
     90    adj[1][2] = - (m[0][0] * SubFactor06 - m[0][2] * SubFactor09 + m[0][3] * SubFactor10);
     91    adj[2][2] = + (m[0][0] * SubFactor11 - m[0][1] * SubFactor09 + m[0][3] * SubFactor12);
     92    adj[3][2] = - (m[0][0] * SubFactor08 - m[0][1] * SubFactor10 + m[0][2] * SubFactor12);
     93 
     94    adj[0][3] = - (m[0][1] * SubFactor13 - m[0][2] * SubFactor14 + m[0][3] * SubFactor15);
     95    adj[1][3] = + (m[0][0] * SubFactor13 - m[0][2] * SubFactor16 + m[0][3] * SubFactor17);
     96    adj[2][3] = - (m[0][0] * SubFactor14 - m[0][1] * SubFactor16 + m[0][3] * SubFactor18);
     97    adj[3][3] = + (m[0][0] * SubFactor15 - m[0][1] * SubFactor17 + m[0][2] * SubFactor18);
     98 
     99    float det = (+ m[0][0] * adj[0][0]
    100 		+ m[0][1] * adj[1][0]
    101 		+ m[0][2] * adj[2][0]
    102 		+ m[0][3] * adj[3][0]);
    103 
    104    return adj / det;
    105 }
    106 
    107