| 97 | } |
| 98 | |
| 99 | Matrix4f Matrix4f::Inverse() const |
| 100 | { |
| 101 | float a0 = (*this)[0] * (*this)[5] - (*this)[1] * (*this)[4]; |
| 102 | float a1 = (*this)[0] * (*this)[6] - (*this)[2] * (*this)[4]; |
| 103 | float a2 = (*this)[0] * (*this)[7] - (*this)[3] * (*this)[4]; |
| 104 | float a3 = (*this)[1] * (*this)[6] - (*this)[2] * (*this)[5]; |
| 105 | float a4 = (*this)[1] * (*this)[7] - (*this)[3] * (*this)[5]; |
| 106 | float a5 = (*this)[2] * (*this)[7] - (*this)[3] * (*this)[6]; |
| 107 | float b0 = (*this)[8] * (*this)[13] - (*this)[9] * (*this)[12]; |
| 108 | float b1 = (*this)[8] * (*this)[14] - (*this)[10] * (*this)[12]; |
| 109 | float b2 = (*this)[8] * (*this)[15] - (*this)[11] * (*this)[12]; |
| 110 | float b3 = (*this)[9] * (*this)[14] - (*this)[10] * (*this)[13]; |
| 111 | float b4 = (*this)[9] * (*this)[15] - (*this)[11] * (*this)[13]; |
| 112 | float b5 = (*this)[10] * (*this)[15] - (*this)[11] * (*this)[14]; |
| 113 | |
| 114 | float det = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0; |
| 115 | if(fabsf(det) > FLT_EPSILON) |
| 116 | { |
| 117 | Matrix4f inverse; |
| 118 | inverse[0] = +(*this)[5] * b5 - (*this)[6] * b4 + (*this)[7] * b3; |
| 119 | inverse[4] = -(*this)[4] * b5 + (*this)[6] * b2 - (*this)[7] * b1; |
| 120 | inverse[8] = +(*this)[4] * b4 - (*this)[5] * b2 + (*this)[7] * b0; |
| 121 | inverse[12] = -(*this)[4] * b3 + (*this)[5] * b1 - (*this)[6] * b0; |
| 122 | inverse[1] = -(*this)[1] * b5 + (*this)[2] * b4 - (*this)[3] * b3; |
| 123 | inverse[5] = +(*this)[0] * b5 - (*this)[2] * b2 + (*this)[3] * b1; |
| 124 | inverse[9] = -(*this)[0] * b4 + (*this)[1] * b2 - (*this)[3] * b0; |
| 125 | inverse[13] = +(*this)[0] * b3 - (*this)[1] * b1 + (*this)[2] * b0; |
| 126 | inverse[2] = +(*this)[13] * a5 - (*this)[14] * a4 + (*this)[15] * a3; |
| 127 | inverse[6] = -(*this)[12] * a5 + (*this)[14] * a2 - (*this)[15] * a1; |
| 128 | inverse[10] = +(*this)[12] * a4 - (*this)[13] * a2 + (*this)[15] * a0; |
| 129 | inverse[14] = -(*this)[12] * a3 + (*this)[13] * a1 - (*this)[14] * a0; |
| 130 | inverse[3] = -(*this)[9] * a5 + (*this)[10] * a4 - (*this)[11] * a3; |
| 131 | inverse[7] = +(*this)[8] * a5 - (*this)[10] * a2 + (*this)[11] * a1; |
| 132 | inverse[11] = -(*this)[8] * a4 + (*this)[9] * a2 - (*this)[11] * a0; |
| 133 | inverse[15] = +(*this)[8] * a3 - (*this)[9] * a1 + (*this)[10] * a0; |
| 134 | |
| 135 | float invDet = 1.0f / det; |
| 136 | inverse[0] *= invDet; |
| 137 | inverse[1] *= invDet; |
| 138 | inverse[2] *= invDet; |
| 139 | inverse[3] *= invDet; |
| 140 | inverse[4] *= invDet; |
| 141 | inverse[5] *= invDet; |
| 142 | inverse[6] *= invDet; |
| 143 | inverse[7] *= invDet; |
| 144 | inverse[8] *= invDet; |
| 145 | inverse[9] *= invDet; |
| 146 | inverse[10] *= invDet; |
| 147 | inverse[11] *= invDet; |
| 148 | inverse[12] *= invDet; |
| 149 | inverse[13] *= invDet; |
| 150 | inverse[14] *= invDet; |
| 151 | inverse[15] *= invDet; |
| 152 | |
| 153 | return inverse; |
| 154 | } |
| 155 | |
| 156 | // no inverse |
no test coverage detected