|
@@ -34,10 +34,13 @@ bool Matrix::getInvert(float out[16]) {
|
34
|
34
|
getTransposeMatrix(m);
|
35
|
35
|
#endif
|
36
|
36
|
|
37
|
|
- /* Mongoose: This code was from a Jeff Lander tutorial which was based
|
38
|
|
- on MESA GL's InvertMatrix */
|
|
37
|
+ /*
|
|
38
|
+ * Mongoose: This code was from a Jeff Lander tutorial which was based
|
|
39
|
+ * on MESA GL's InvertMatrix.
|
|
40
|
+ *
|
|
41
|
+ * NB. OpenGL Matrices are COLUMN major
|
|
42
|
+ */
|
39
|
43
|
|
40
|
|
- /* NB. OpenGL Matrices are COLUMN major. */
|
41
|
44
|
#define SWAP_ROWS(a, b) { float *_tmp = a; (a)=(b); (b)=_tmp; }
|
42
|
45
|
#define MAT(m,r,c) (m)[(c)*4+(r)]
|
43
|
46
|
|
|
@@ -45,115 +48,221 @@ bool Matrix::getInvert(float out[16]) {
|
45
|
48
|
float m0, m1, m2, m3, s;
|
46
|
49
|
float* r0, *r1, *r2, *r3;
|
47
|
50
|
|
48
|
|
- r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
|
|
51
|
+ r0 = wtmp[0];
|
|
52
|
+ r1 = wtmp[1];
|
|
53
|
+ r2 = wtmp[2];
|
|
54
|
+ r3 = wtmp[3];
|
|
55
|
+
|
|
56
|
+ r0[0] = MAT(m, 0, 0);
|
|
57
|
+ r0[1] = MAT(m, 0, 1);
|
|
58
|
+ r0[2] = MAT(m, 0, 2);
|
|
59
|
+ r0[3] = MAT(m, 0, 3);
|
|
60
|
+ r0[4] = 1.0f;
|
|
61
|
+ r0[5] = r0[6] = r0[7] = 0.0f;
|
|
62
|
+
|
|
63
|
+ r1[0] = MAT(m, 1, 0);
|
|
64
|
+ r1[1] = MAT(m, 1, 1);
|
|
65
|
+ r1[2] = MAT(m, 1, 2);
|
|
66
|
+ r1[3] = MAT(m, 1, 3);
|
|
67
|
+ r1[5] = 1.0f;
|
|
68
|
+ r1[4] = r1[6] = r1[7] = 0.0f;
|
|
69
|
+
|
|
70
|
+ r2[0] = MAT(m, 2, 0);
|
|
71
|
+ r2[1] = MAT(m, 2, 1);
|
|
72
|
+ r2[2] = MAT(m, 2, 2);
|
|
73
|
+ r2[3] = MAT(m, 2, 3);
|
|
74
|
+ r2[6] = 1.0f;
|
|
75
|
+ r2[4] = r2[5] = r2[7] = 0.0f;
|
|
76
|
+
|
|
77
|
+ r3[0] = MAT(m, 3, 0);
|
|
78
|
+ r3[1] = MAT(m, 3, 1);
|
|
79
|
+ r3[2] = MAT(m, 3, 2);
|
|
80
|
+ r3[3] = MAT(m, 3, 3);
|
|
81
|
+ r3[7] = 1.0f;
|
|
82
|
+ r3[4] = r3[5] = r3[6] = 0.0f;
|
|
83
|
+
|
|
84
|
+ // choose pivot or die
|
|
85
|
+ if (fabs(r3[0]) > fabs(r2[0]))
|
|
86
|
+ SWAP_ROWS(r3, r2);
|
|
87
|
+
|
|
88
|
+ if (fabs(r2[0]) > fabs(r1[0]))
|
|
89
|
+ SWAP_ROWS(r2, r1);
|
|
90
|
+
|
|
91
|
+ if (fabs(r1[0]) > fabs(r0[0]))
|
|
92
|
+ SWAP_ROWS(r1, r0);
|
|
93
|
+
|
|
94
|
+ if (0.0f == r0[0])
|
|
95
|
+ return false;
|
|
96
|
+
|
|
97
|
+ // eliminate first variable
|
|
98
|
+ m1 = r1[0] / r0[0];
|
|
99
|
+ m2 = r2[0] / r0[0];
|
|
100
|
+ m3 = r3[0] / r0[0];
|
|
101
|
+
|
|
102
|
+ s = r0[1];
|
|
103
|
+ r1[1] -= m1 * s;
|
|
104
|
+ r2[1] -= m2 * s;
|
|
105
|
+ r3[1] -= m3 * s;
|
|
106
|
+
|
|
107
|
+ s = r0[2];
|
|
108
|
+ r1[2] -= m1 * s;
|
|
109
|
+ r2[2] -= m2 * s;
|
|
110
|
+ r3[2] -= m3 * s;
|
|
111
|
+
|
|
112
|
+ s = r0[3];
|
|
113
|
+ r1[3] -= m1 * s;
|
|
114
|
+ r2[3] -= m2 * s;
|
|
115
|
+ r3[3] -= m3 * s;
|
49
|
116
|
|
50
|
|
- r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1),
|
51
|
|
- r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3),
|
52
|
|
- r0[4] = 1.0f, r0[5] = r0[6] = r0[7] = 0.0f,
|
|
117
|
+ s = r0[4];
|
|
118
|
+ if (s != 0.0f) {
|
|
119
|
+ r1[4] -= m1 * s;
|
|
120
|
+ r2[4] -= m2 * s;
|
|
121
|
+ r3[4] -= m3 * s;
|
|
122
|
+ }
|
53
|
123
|
|
54
|
|
- r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1),
|
55
|
|
- r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3),
|
56
|
|
- r1[5] = 1.0f, r1[4] = r1[6] = r1[7] = 0.0f,
|
|
124
|
+ s = r0[5];
|
|
125
|
+ if (s != 0.0f) {
|
|
126
|
+ r1[5] -= m1 * s;
|
|
127
|
+ r2[5] -= m2 * s;
|
|
128
|
+ r3[5] -= m3 * s;
|
|
129
|
+ }
|
57
|
130
|
|
58
|
|
- r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1),
|
59
|
|
- r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3),
|
60
|
|
- r2[6] = 1.0f, r2[4] = r2[5] = r2[7] = 0.0f,
|
|
131
|
+ s = r0[6];
|
|
132
|
+ if (s != 0.0f) {
|
|
133
|
+ r1[6] -= m1 * s;
|
|
134
|
+ r2[6] -= m2 * s;
|
|
135
|
+ r3[6] -= m3 * s;
|
|
136
|
+ }
|
61
|
137
|
|
62
|
|
- r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1),
|
63
|
|
- r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3),
|
64
|
|
- r3[7] = 1.0f, r3[4] = r3[5] = r3[6] = 0.0f;
|
|
138
|
+ s = r0[7];
|
|
139
|
+ if (s != 0.0f) {
|
|
140
|
+ r1[7] -= m1 * s;
|
|
141
|
+ r2[7] -= m2 * s;
|
|
142
|
+ r3[7] -= m3 * s;
|
|
143
|
+ }
|
65
|
144
|
|
66
|
|
- /* choose pivot - or die */
|
67
|
|
- if (fabs(r3[0]) > fabs(r2[0])) SWAP_ROWS(r3, r2);
|
68
|
|
- if (fabs(r2[0]) > fabs(r1[0])) SWAP_ROWS(r2, r1);
|
69
|
|
- if (fabs(r1[0]) > fabs(r0[0])) SWAP_ROWS(r1, r0);
|
70
|
|
- if (0.0f == r0[0]) return false;
|
|
145
|
+ // choose pivot or die
|
|
146
|
+ if (fabs(r3[1]) > fabs(r2[1]))
|
|
147
|
+ SWAP_ROWS(r3, r2);
|
71
|
148
|
|
72
|
|
- /* eliminate first variable */
|
73
|
|
- m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
|
74
|
|
- s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
|
75
|
|
- s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
|
76
|
|
- s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
|
77
|
|
- s = r0[4];
|
78
|
|
- if (s != 0.0f) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
|
79
|
|
- s = r0[5];
|
80
|
|
- if (s != 0.0f) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
|
81
|
|
- s = r0[6];
|
82
|
|
- if (s != 0.0f) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
|
83
|
|
- s = r0[7];
|
84
|
|
- if (s != 0.0f) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
|
|
149
|
+ if (fabs(r2[1]) > fabs(r1[1]))
|
|
150
|
+ SWAP_ROWS(r2, r1);
|
85
|
151
|
|
86
|
|
- /* choose pivot - or die */
|
87
|
|
- if (fabs(r3[1]) > fabs(r2[1])) SWAP_ROWS(r3, r2);
|
88
|
|
- if (fabs(r2[1]) > fabs(r1[1])) SWAP_ROWS(r2, r1);
|
89
|
|
- if (0.0f == r1[1]) return false;
|
|
152
|
+ if (0.0f == r1[1])
|
|
153
|
+ return false;
|
|
154
|
+
|
|
155
|
+ // eliminate second variable
|
|
156
|
+ m2 = r2[1] / r1[1];
|
|
157
|
+ m3 = r3[1] / r1[1];
|
|
158
|
+ r2[2] -= m2 * r1[2];
|
|
159
|
+ r3[2] -= m3 * r1[2];
|
|
160
|
+ r2[3] -= m2 * r1[3];
|
|
161
|
+ r3[3] -= m3 * r1[3];
|
90
|
162
|
|
91
|
|
- /* eliminate second variable */
|
92
|
|
- m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
|
93
|
|
- r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
|
94
|
|
- r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
|
95
|
163
|
s = r1[4];
|
96
|
|
- if (0.0f != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
|
|
164
|
+ if (0.0f != s) {
|
|
165
|
+ r2[4] -= m2 * s;
|
|
166
|
+ r3[4] -= m3 * s;
|
|
167
|
+ }
|
|
168
|
+
|
97
|
169
|
s = r1[5];
|
98
|
|
- if (0.0f != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
|
|
170
|
+ if (0.0f != s) {
|
|
171
|
+ r2[5] -= m2 * s;
|
|
172
|
+ r3[5] -= m3 * s;
|
|
173
|
+ }
|
|
174
|
+
|
99
|
175
|
s = r1[6];
|
100
|
|
- if (0.0f != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
|
|
176
|
+ if (0.0f != s) {
|
|
177
|
+ r2[6] -= m2 * s;
|
|
178
|
+ r3[6] -= m3 * s;
|
|
179
|
+ }
|
|
180
|
+
|
101
|
181
|
s = r1[7];
|
102
|
|
- if (0.0f != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
|
|
182
|
+ if (0.0f != s) {
|
|
183
|
+ r2[7] -= m2 * s;
|
|
184
|
+ r3[7] -= m3 * s;
|
|
185
|
+ }
|
103
|
186
|
|
104
|
|
- /* choose pivot - or die */
|
|
187
|
+ // choose pivot or die
|
105
|
188
|
if (fabs(r3[2]) > fabs(r2[2])) SWAP_ROWS(r3, r2);
|
106
|
189
|
if (0.0f == r2[2]) return false;
|
107
|
190
|
|
108
|
|
- /* eliminate third variable */
|
|
191
|
+ // eliminate third variable
|
109
|
192
|
m3 = r3[2] / r2[2];
|
110
|
|
- r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
|
111
|
|
- r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
|
112
|
|
- r3[7] -= m3 * r2[7];
|
113
|
|
-
|
114
|
|
- /* last check */
|
115
|
|
- if (0.0f == r3[3]) return false;
|
|
193
|
+ r3[3] -= m3 * r2[3];
|
|
194
|
+ r3[4] -= m3 * r2[4];
|
|
195
|
+ r3[5] -= m3 * r2[5];
|
|
196
|
+ r3[6] -= m3 * r2[6];
|
|
197
|
+ r3[7] -= m3 * r2[7];
|
|
198
|
+
|
|
199
|
+ // last check
|
|
200
|
+ if (0.0f == r3[3])
|
|
201
|
+ return false;
|
|
202
|
+
|
|
203
|
+ s = 1.0f / r3[3]; // now back substitute row 3
|
|
204
|
+ r3[4] *= s;
|
|
205
|
+ r3[5] *= s;
|
|
206
|
+ r3[6] *= s;
|
|
207
|
+ r3[7] *= s;
|
|
208
|
+
|
|
209
|
+ m2 = r2[3]; // now back substitute row 2
|
|
210
|
+ s = 1.0f / r2[2];
|
|
211
|
+ r2[4] = s * (r2[4] - r3[4] * m2);
|
|
212
|
+ r2[5] = s * (r2[5] - r3[5] * m2);
|
|
213
|
+ r2[6] = s * (r2[6] - r3[6] * m2);
|
|
214
|
+ r2[7] = s * (r2[7] - r3[7] * m2);
|
116
|
215
|
|
117
|
|
- s = 1.0f / r3[3]; /* now back substitute row 3 */
|
118
|
|
- r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
|
119
|
|
-
|
120
|
|
- m2 = r2[3]; /* now back substitute row 2 */
|
121
|
|
- s = 1.0f / r2[2];
|
122
|
|
- r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
|
123
|
|
- r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
|
124
|
216
|
m1 = r1[3];
|
125
|
|
- r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
|
126
|
|
- r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
|
|
217
|
+ r1[4] -= r3[4] * m1;
|
|
218
|
+ r1[5] -= r3[5] * m1;
|
|
219
|
+ r1[6] -= r3[6] * m1;
|
|
220
|
+ r1[7] -= r3[7] * m1;
|
|
221
|
+
|
127
|
222
|
m0 = r0[3];
|
128
|
|
- r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
|
129
|
|
- r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
|
|
223
|
+ r0[4] -= r3[4] * m0;
|
|
224
|
+ r0[5] -= r3[5] * m0;
|
|
225
|
+ r0[6] -= r3[6] * m0;
|
|
226
|
+ r0[7] -= r3[7] * m0;
|
|
227
|
+
|
|
228
|
+ m1 = r1[2]; // now back substitute row 1
|
|
229
|
+ s = 1.0f / r1[1];
|
|
230
|
+ r1[4] = s * (r1[4] - r2[4] * m1);
|
|
231
|
+ r1[5] = s * (r1[5] - r2[5] * m1);
|
|
232
|
+ r1[6] = s * (r1[6] - r2[6] * m1);
|
|
233
|
+ r1[7] = s * (r1[7] - r2[7] * m1);
|
130
|
234
|
|
131
|
|
- m1 = r1[2]; /* now back substitute row 1 */
|
132
|
|
- s = 1.0f / r1[1];
|
133
|
|
- r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
|
134
|
|
- r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
|
135
|
235
|
m0 = r0[2];
|
136
|
|
- r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
|
137
|
|
- r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
|
138
|
|
-
|
139
|
|
- m0 = r0[1]; /* now back substitute row 0 */
|
140
|
|
- s = 1.0f / r0[0];
|
141
|
|
- r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
|
142
|
|
- r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
|
|
236
|
+ r0[4] -= r2[4] * m0;
|
|
237
|
+ r0[5] -= r2[5] * m0;
|
|
238
|
+ r0[6] -= r2[6] * m0;
|
|
239
|
+ r0[7] -= r2[7] * m0;
|
|
240
|
+
|
|
241
|
+ m0 = r0[1]; // now back substitute row 0
|
|
242
|
+ s = 1.0f / r0[0];
|
|
243
|
+ r0[4] = s * (r0[4] - r1[4] * m0);
|
|
244
|
+ r0[5] = s * (r0[5] - r1[5] * m0);
|
|
245
|
+ r0[6] = s * (r0[6] - r1[6] * m0);
|
|
246
|
+ r0[7] = s * (r0[7] - r1[7] * m0);
|
143
|
247
|
|
144
|
248
|
MAT(out, 0, 0) = r0[4];
|
145
|
|
- MAT(out, 0, 1) = r0[5], MAT(out, 0, 2) = r0[6];
|
146
|
|
- MAT(out, 0, 3) = r0[7], MAT(out, 1, 0) = r1[4];
|
147
|
|
- MAT(out, 1, 1) = r1[5], MAT(out, 1, 2) = r1[6];
|
148
|
|
- MAT(out, 1, 3) = r1[7], MAT(out, 2, 0) = r2[4];
|
149
|
|
- MAT(out, 2, 1) = r2[5], MAT(out, 2, 2) = r2[6];
|
150
|
|
- MAT(out, 2, 3) = r2[7], MAT(out, 3, 0) = r3[4];
|
151
|
|
- MAT(out, 3, 1) = r3[5], MAT(out, 3, 2) = r3[6];
|
|
249
|
+ MAT(out, 0, 1) = r0[5];
|
|
250
|
+ MAT(out, 0, 2) = r0[6];
|
|
251
|
+ MAT(out, 0, 3) = r0[7];
|
|
252
|
+ MAT(out, 1, 0) = r1[4];
|
|
253
|
+ MAT(out, 1, 1) = r1[5];
|
|
254
|
+ MAT(out, 1, 2) = r1[6];
|
|
255
|
+ MAT(out, 1, 3) = r1[7];
|
|
256
|
+ MAT(out, 2, 0) = r2[4];
|
|
257
|
+ MAT(out, 2, 1) = r2[5];
|
|
258
|
+ MAT(out, 2, 2) = r2[6];
|
|
259
|
+ MAT(out, 2, 3) = r2[7];
|
|
260
|
+ MAT(out, 3, 0) = r3[4];
|
|
261
|
+ MAT(out, 3, 1) = r3[5];
|
|
262
|
+ MAT(out, 3, 2) = r3[6];
|
152
|
263
|
MAT(out, 3, 3) = r3[7];
|
153
|
264
|
|
154
|
265
|
return true;
|
155
|
|
-#undef MAT
|
156
|
|
-#undef SWAP_ROWS
|
157
|
266
|
}
|
158
|
267
|
|
159
|
268
|
void Matrix::getMatrix(float mat[16]) {
|
|
@@ -224,16 +333,6 @@ void Matrix::print() {
|
224
|
333
|
}
|
225
|
334
|
|
226
|
335
|
bool Matrix::isIdentity() {
|
227
|
|
- // Hhhmm... floating point using direct comparisons
|
228
|
|
- /*
|
229
|
|
- if (mMatrix[ 0] == 1 && mMatrix[ 1] == 0 && mMatrix[ 2] == 0 &&
|
230
|
|
- mMatrix[ 3] == 0 && mMatrix[ 4] == 0 && mMatrix[ 5] == 1 &&
|
231
|
|
- mMatrix[ 6] == 0 && mMatrix[ 7] == 0 && mMatrix[ 8] == 0 &&
|
232
|
|
- mMatrix[ 9] == 0 && mMatrix[10] == 1 && mMatrix[11] == 0 &&
|
233
|
|
- mMatrix[12] == 0 && mMatrix[13] == 0 && mMatrix[14] == 0 &&
|
234
|
|
- mMatrix[15] == 1)
|
235
|
|
- return true;
|
236
|
|
- */
|
237
|
336
|
if (equalEpsilon(mMatrix[ 0], 1.0) && equalEpsilon(mMatrix[ 1], 0.0)
|
238
|
337
|
&& equalEpsilon(mMatrix[ 2], 0.0) &&
|
239
|
338
|
equalEpsilon(mMatrix[ 3], 0.0) && equalEpsilon(mMatrix[ 4], 0.0) && equalEpsilon(mMatrix[ 5], 1.0)
|