optimizations to _math_matrix_rotate() (Rudolf Opalla)
[mesa.git] / src / mesa / math / m_matrix.c
1 /* $Id: m_matrix.c,v 1.13 2002/09/12 16:26:04 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /*
29 * Matrix operations
30 *
31 * NOTES:
32 * 1. 4x4 transformation matrices are stored in memory in column major order.
33 * 2. Points/vertices are to be thought of as column vectors.
34 * 3. Transformation of a point p by a matrix M is: p' = M * p
35 */
36
37 #include <math.h>
38
39 #include "glheader.h"
40 #include "imports.h"
41 #include "macros.h"
42 #include "mem.h"
43 #include "mmath.h"
44
45 #include "m_matrix.h"
46
47
48 static const char *types[] = {
49 "MATRIX_GENERAL",
50 "MATRIX_IDENTITY",
51 "MATRIX_3D_NO_ROT",
52 "MATRIX_PERSPECTIVE",
53 "MATRIX_2D",
54 "MATRIX_2D_NO_ROT",
55 "MATRIX_3D"
56 };
57
58
59 static GLfloat Identity[16] = {
60 1.0, 0.0, 0.0, 0.0,
61 0.0, 1.0, 0.0, 0.0,
62 0.0, 0.0, 1.0, 0.0,
63 0.0, 0.0, 0.0, 1.0
64 };
65
66
67
68
69 /*
70 * This matmul was contributed by Thomas Malik
71 *
72 * Perform a 4x4 matrix multiplication (product = a x b).
73 * Input: a, b - matrices to multiply
74 * Output: product - product of a and b
75 * WARNING: (product != b) assumed
76 * NOTE: (product == a) allowed
77 *
78 * KW: 4*16 = 64 muls
79 */
80 #define A(row,col) a[(col<<2)+row]
81 #define B(row,col) b[(col<<2)+row]
82 #define P(row,col) product[(col<<2)+row]
83
84 static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
85 {
86 GLint i;
87 for (i = 0; i < 4; i++) {
88 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
89 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
90 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
91 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
92 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
93 }
94 }
95
96
97 /* Multiply two matrices known to occupy only the top three rows, such
98 * as typical model matrices, and ortho matrices.
99 */
100 static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
101 {
102 GLint i;
103 for (i = 0; i < 3; i++) {
104 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
105 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
106 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
107 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
108 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
109 }
110 P(3,0) = 0;
111 P(3,1) = 0;
112 P(3,2) = 0;
113 P(3,3) = 1;
114 }
115
116
117 #undef A
118 #undef B
119 #undef P
120
121
122 /*
123 * Multiply a matrix by an array of floats with known properties.
124 */
125 static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags )
126 {
127 mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
128
129 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
130 matmul34( mat->m, mat->m, m );
131 else
132 matmul4( mat->m, mat->m, m );
133 }
134
135
136 static void print_matrix_floats( const GLfloat m[16] )
137 {
138 int i;
139 for (i=0;i<4;i++) {
140 _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
141 }
142 }
143
144 void
145 _math_matrix_print( const GLmatrix *m )
146 {
147 _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
148 print_matrix_floats(m->m);
149 _mesa_debug(NULL, "Inverse: \n");
150 if (m->inv) {
151 GLfloat prod[16];
152 print_matrix_floats(m->inv);
153 matmul4(prod, m->m, m->inv);
154 _mesa_debug(NULL, "Mat * Inverse:\n");
155 print_matrix_floats(prod);
156 }
157 else {
158 _mesa_debug(NULL, " - not available\n");
159 }
160 }
161
162
163
164
165 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
166 #define MAT(m,r,c) (m)[(c)*4+(r)]
167
168 /*
169 * Compute inverse of 4x4 transformation matrix.
170 * Code contributed by Jacques Leroy jle@star.be
171 * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
172 */
173 static GLboolean invert_matrix_general( GLmatrix *mat )
174 {
175 const GLfloat *m = mat->m;
176 GLfloat *out = mat->inv;
177 GLfloat wtmp[4][8];
178 GLfloat m0, m1, m2, m3, s;
179 GLfloat *r0, *r1, *r2, *r3;
180
181 r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
182
183 r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
184 r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
185 r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
186
187 r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
188 r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
189 r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
190
191 r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
192 r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
193 r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
194
195 r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
196 r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
197 r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
198
199 /* choose pivot - or die */
200 if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
201 if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
202 if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
203 if (0.0 == r0[0]) return GL_FALSE;
204
205 /* eliminate first variable */
206 m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
207 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
208 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
209 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
210 s = r0[4];
211 if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
212 s = r0[5];
213 if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
214 s = r0[6];
215 if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
216 s = r0[7];
217 if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
218
219 /* choose pivot - or die */
220 if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
221 if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
222 if (0.0 == r1[1]) return GL_FALSE;
223
224 /* eliminate second variable */
225 m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
226 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
227 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
228 s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
229 s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
230 s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
231 s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
232
233 /* choose pivot - or die */
234 if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
235 if (0.0 == r2[2]) return GL_FALSE;
236
237 /* eliminate third variable */
238 m3 = r3[2]/r2[2];
239 r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
240 r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
241 r3[7] -= m3 * r2[7];
242
243 /* last check */
244 if (0.0 == r3[3]) return GL_FALSE;
245
246 s = 1.0F/r3[3]; /* now back substitute row 3 */
247 r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
248
249 m2 = r2[3]; /* now back substitute row 2 */
250 s = 1.0F/r2[2];
251 r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
252 r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
253 m1 = r1[3];
254 r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
255 r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
256 m0 = r0[3];
257 r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
258 r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
259
260 m1 = r1[2]; /* now back substitute row 1 */
261 s = 1.0F/r1[1];
262 r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
263 r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
264 m0 = r0[2];
265 r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
266 r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
267
268 m0 = r0[1]; /* now back substitute row 0 */
269 s = 1.0F/r0[0];
270 r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
271 r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
272
273 MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
274 MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
275 MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
276 MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],
277 MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
278 MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
279 MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
280 MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7];
281
282 return GL_TRUE;
283 }
284 #undef SWAP_ROWS
285
286
287 /* Adapted from graphics gems II.
288 */
289 static GLboolean invert_matrix_3d_general( GLmatrix *mat )
290 {
291 const GLfloat *in = mat->m;
292 GLfloat *out = mat->inv;
293 GLfloat pos, neg, t;
294 GLfloat det;
295
296 /* Calculate the determinant of upper left 3x3 submatrix and
297 * determine if the matrix is singular.
298 */
299 pos = neg = 0.0;
300 t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
301 if (t >= 0.0) pos += t; else neg += t;
302
303 t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
304 if (t >= 0.0) pos += t; else neg += t;
305
306 t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
307 if (t >= 0.0) pos += t; else neg += t;
308
309 t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
310 if (t >= 0.0) pos += t; else neg += t;
311
312 t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
313 if (t >= 0.0) pos += t; else neg += t;
314
315 t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
316 if (t >= 0.0) pos += t; else neg += t;
317
318 det = pos + neg;
319
320 if (det*det < 1e-25)
321 return GL_FALSE;
322
323 det = 1.0F / det;
324 MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
325 MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
326 MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
327 MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);
328 MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);
329 MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);
330 MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);
331 MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);
332 MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);
333
334 /* Do the translation part */
335 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
336 MAT(in,1,3) * MAT(out,0,1) +
337 MAT(in,2,3) * MAT(out,0,2) );
338 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
339 MAT(in,1,3) * MAT(out,1,1) +
340 MAT(in,2,3) * MAT(out,1,2) );
341 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
342 MAT(in,1,3) * MAT(out,2,1) +
343 MAT(in,2,3) * MAT(out,2,2) );
344
345 return GL_TRUE;
346 }
347
348
349 static GLboolean invert_matrix_3d( GLmatrix *mat )
350 {
351 const GLfloat *in = mat->m;
352 GLfloat *out = mat->inv;
353
354 if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
355 return invert_matrix_3d_general( mat );
356 }
357
358 if (mat->flags & MAT_FLAG_UNIFORM_SCALE) {
359 GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
360 MAT(in,0,1) * MAT(in,0,1) +
361 MAT(in,0,2) * MAT(in,0,2));
362
363 if (scale == 0.0)
364 return GL_FALSE;
365
366 scale = 1.0F / scale;
367
368 /* Transpose and scale the 3 by 3 upper-left submatrix. */
369 MAT(out,0,0) = scale * MAT(in,0,0);
370 MAT(out,1,0) = scale * MAT(in,0,1);
371 MAT(out,2,0) = scale * MAT(in,0,2);
372 MAT(out,0,1) = scale * MAT(in,1,0);
373 MAT(out,1,1) = scale * MAT(in,1,1);
374 MAT(out,2,1) = scale * MAT(in,1,2);
375 MAT(out,0,2) = scale * MAT(in,2,0);
376 MAT(out,1,2) = scale * MAT(in,2,1);
377 MAT(out,2,2) = scale * MAT(in,2,2);
378 }
379 else if (mat->flags & MAT_FLAG_ROTATION) {
380 /* Transpose the 3 by 3 upper-left submatrix. */
381 MAT(out,0,0) = MAT(in,0,0);
382 MAT(out,1,0) = MAT(in,0,1);
383 MAT(out,2,0) = MAT(in,0,2);
384 MAT(out,0,1) = MAT(in,1,0);
385 MAT(out,1,1) = MAT(in,1,1);
386 MAT(out,2,1) = MAT(in,1,2);
387 MAT(out,0,2) = MAT(in,2,0);
388 MAT(out,1,2) = MAT(in,2,1);
389 MAT(out,2,2) = MAT(in,2,2);
390 }
391 else {
392 /* pure translation */
393 MEMCPY( out, Identity, sizeof(Identity) );
394 MAT(out,0,3) = - MAT(in,0,3);
395 MAT(out,1,3) = - MAT(in,1,3);
396 MAT(out,2,3) = - MAT(in,2,3);
397 return GL_TRUE;
398 }
399
400 if (mat->flags & MAT_FLAG_TRANSLATION) {
401 /* Do the translation part */
402 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
403 MAT(in,1,3) * MAT(out,0,1) +
404 MAT(in,2,3) * MAT(out,0,2) );
405 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
406 MAT(in,1,3) * MAT(out,1,1) +
407 MAT(in,2,3) * MAT(out,1,2) );
408 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
409 MAT(in,1,3) * MAT(out,2,1) +
410 MAT(in,2,3) * MAT(out,2,2) );
411 }
412 else {
413 MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
414 }
415
416 return GL_TRUE;
417 }
418
419
420
421 static GLboolean invert_matrix_identity( GLmatrix *mat )
422 {
423 MEMCPY( mat->inv, Identity, sizeof(Identity) );
424 return GL_TRUE;
425 }
426
427
428 static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
429 {
430 const GLfloat *in = mat->m;
431 GLfloat *out = mat->inv;
432
433 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
434 return GL_FALSE;
435
436 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
437 MAT(out,0,0) = 1.0F / MAT(in,0,0);
438 MAT(out,1,1) = 1.0F / MAT(in,1,1);
439 MAT(out,2,2) = 1.0F / MAT(in,2,2);
440
441 if (mat->flags & MAT_FLAG_TRANSLATION) {
442 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
443 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
444 MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
445 }
446
447 return GL_TRUE;
448 }
449
450
451 static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
452 {
453 const GLfloat *in = mat->m;
454 GLfloat *out = mat->inv;
455
456 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
457 return GL_FALSE;
458
459 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
460 MAT(out,0,0) = 1.0F / MAT(in,0,0);
461 MAT(out,1,1) = 1.0F / MAT(in,1,1);
462
463 if (mat->flags & MAT_FLAG_TRANSLATION) {
464 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
465 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
466 }
467
468 return GL_TRUE;
469 }
470
471
472 #if 0
473 /* broken */
474 static GLboolean invert_matrix_perspective( GLmatrix *mat )
475 {
476 const GLfloat *in = mat->m;
477 GLfloat *out = mat->inv;
478
479 if (MAT(in,2,3) == 0)
480 return GL_FALSE;
481
482 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
483
484 MAT(out,0,0) = 1.0F / MAT(in,0,0);
485 MAT(out,1,1) = 1.0F / MAT(in,1,1);
486
487 MAT(out,0,3) = MAT(in,0,2);
488 MAT(out,1,3) = MAT(in,1,2);
489
490 MAT(out,2,2) = 0;
491 MAT(out,2,3) = -1;
492
493 MAT(out,3,2) = 1.0F / MAT(in,2,3);
494 MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
495
496 return GL_TRUE;
497 }
498 #endif
499
500
501 typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
502
503
504 static inv_mat_func inv_mat_tab[7] = {
505 invert_matrix_general,
506 invert_matrix_identity,
507 invert_matrix_3d_no_rot,
508 #if 0
509 /* Don't use this function for now - it fails when the projection matrix
510 * is premultiplied by a translation (ala Chromium's tilesort SPU).
511 */
512 invert_matrix_perspective,
513 #else
514 invert_matrix_general,
515 #endif
516 invert_matrix_3d, /* lazy! */
517 invert_matrix_2d_no_rot,
518 invert_matrix_3d
519 };
520
521
522 static GLboolean matrix_invert( GLmatrix *mat )
523 {
524 if (inv_mat_tab[mat->type](mat)) {
525 mat->flags &= ~MAT_FLAG_SINGULAR;
526 return GL_TRUE;
527 } else {
528 mat->flags |= MAT_FLAG_SINGULAR;
529 MEMCPY( mat->inv, Identity, sizeof(Identity) );
530 return GL_FALSE;
531 }
532 }
533
534
535
536
537
538
539 /*
540 * Generate a 4x4 transformation matrix from glRotate parameters, and
541 * postmultiply the input matrix by it.
542 * This function contributed by Erich Boleyn (erich@uruk.org).
543 * Optimizatios contributed by Rudolf Opalla (rudi@khm.de).
544 */
545 void
546 _math_matrix_rotate( GLmatrix *mat,
547 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
548 {
549 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
550 GLfloat m[16];
551 GLboolean optimized;
552
553 s = (GLfloat) sin( angle * DEG2RAD );
554 c = (GLfloat) cos( angle * DEG2RAD );
555
556 MEMCPY(m, Identity, sizeof(GLfloat)*16);
557 optimized = GL_FALSE;
558
559 #define M(row,col) m[col*4+row]
560
561 if (x == 0.0F) {
562 if (y == 0.0F) {
563 if (z != 0.0F) {
564 optimized = GL_TRUE;
565 /* rotate only around z-axis */
566 M(0,0) = c;
567 M(1,1) = c;
568 if (z < 0.0F) {
569 M(0,1) = s;
570 M(1,0) = -s;
571 }
572 else {
573 M(0,1) = -s;
574 M(1,0) = s;
575 }
576 }
577 }
578 else if (z == 0.0F) {
579 optimized = GL_TRUE;
580 /* rotate only around y-axis */
581 M(0,0) = c;
582 M(2,2) = c;
583 if (y < 0.0F) {
584 M(0,2) = -s;
585 M(2,0) = s;
586 }
587 else {
588 M(0,2) = s;
589 M(2,0) = -s;
590 }
591 }
592 }
593 else if (y == 0.0F) {
594 if (z == 0.0F) {
595 optimized = GL_TRUE;
596 /* rotate only around x-axis */
597 M(1,1) = c;
598 M(2,2) = c;
599 if (y < 0.0F) {
600 M(1,2) = s;
601 M(2,1) = -s;
602 }
603 else {
604 M(1,2) = -s;
605 M(2,1) = s;
606 }
607 }
608 }
609
610 if (!optimized) {
611 const GLfloat mag = (GLfloat) GL_SQRT(x * x + y * y + z * z);
612
613 if (mag <= 1.0e-4) {
614 /* no rotation, leave mat as-is */
615 return;
616 }
617
618 x /= mag;
619 y /= mag;
620 z /= mag;
621
622
623 /*
624 * Arbitrary axis rotation matrix.
625 *
626 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
627 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
628 * (which is about the X-axis), and the two composite transforms
629 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
630 * from the arbitrary axis to the X-axis then back. They are
631 * all elementary rotations.
632 *
633 * Rz' is a rotation about the Z-axis, to bring the axis vector
634 * into the x-z plane. Then Ry' is applied, rotating about the
635 * Y-axis to bring the axis vector parallel with the X-axis. The
636 * rotation about the X-axis is then performed. Ry and Rz are
637 * simply the respective inverse transforms to bring the arbitrary
638 * axis back to it's original orientation. The first transforms
639 * Rz' and Ry' are considered inverses, since the data from the
640 * arbitrary axis gives you info on how to get to it, not how
641 * to get away from it, and an inverse must be applied.
642 *
643 * The basic calculation used is to recognize that the arbitrary
644 * axis vector (x, y, z), since it is of unit length, actually
645 * represents the sines and cosines of the angles to rotate the
646 * X-axis to the same orientation, with theta being the angle about
647 * Z and phi the angle about Y (in the order described above)
648 * as follows:
649 *
650 * cos ( theta ) = x / sqrt ( 1 - z^2 )
651 * sin ( theta ) = y / sqrt ( 1 - z^2 )
652 *
653 * cos ( phi ) = sqrt ( 1 - z^2 )
654 * sin ( phi ) = z
655 *
656 * Note that cos ( phi ) can further be inserted to the above
657 * formulas:
658 *
659 * cos ( theta ) = x / cos ( phi )
660 * sin ( theta ) = y / sin ( phi )
661 *
662 * ...etc. Because of those relations and the standard trigonometric
663 * relations, it is pssible to reduce the transforms down to what
664 * is used below. It may be that any primary axis chosen will give the
665 * same results (modulo a sign convention) using thie method.
666 *
667 * Particularly nice is to notice that all divisions that might
668 * have caused trouble when parallel to certain planes or
669 * axis go away with care paid to reducing the expressions.
670 * After checking, it does perform correctly under all cases, since
671 * in all the cases of division where the denominator would have
672 * been zero, the numerator would have been zero as well, giving
673 * the expected result.
674 */
675
676 xx = x * x;
677 yy = y * y;
678 zz = z * z;
679 xy = x * y;
680 yz = y * z;
681 zx = z * x;
682 xs = x * s;
683 ys = y * s;
684 zs = z * s;
685 one_c = 1.0F - c;
686
687 /* We already hold the identity-matrix so we can skip some statements */
688 M(0,0) = (one_c * xx) + c;
689 M(0,1) = (one_c * xy) - zs;
690 M(0,2) = (one_c * zx) + ys;
691 /* M(0,3) = 0.0F; */
692
693 M(1,0) = (one_c * xy) + zs;
694 M(1,1) = (one_c * yy) + c;
695 M(1,2) = (one_c * yz) - xs;
696 /* M(1,3) = 0.0F; */
697
698 M(2,0) = (one_c * zx) - ys;
699 M(2,1) = (one_c * yz) + xs;
700 M(2,2) = (one_c * zz) + c;
701 /* M(2,3) = 0.0F; */
702
703 /*
704 M(3,0) = 0.0F;
705 M(3,1) = 0.0F;
706 M(3,2) = 0.0F;
707 M(3,3) = 1.0F;
708 */
709 }
710 #undef M
711
712 matrix_multf( mat, m, MAT_FLAG_ROTATION );
713 }
714
715
716
717 void
718 _math_matrix_frustum( GLmatrix *mat,
719 GLfloat left, GLfloat right,
720 GLfloat bottom, GLfloat top,
721 GLfloat nearval, GLfloat farval )
722 {
723 GLfloat x, y, a, b, c, d;
724 GLfloat m[16];
725
726 x = (2.0F*nearval) / (right-left);
727 y = (2.0F*nearval) / (top-bottom);
728 a = (right+left) / (right-left);
729 b = (top+bottom) / (top-bottom);
730 c = -(farval+nearval) / ( farval-nearval);
731 d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */
732
733 #define M(row,col) m[col*4+row]
734 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
735 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
736 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
737 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
738 #undef M
739
740 matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE );
741 }
742
743 void
744 _math_matrix_ortho( GLmatrix *mat,
745 GLfloat left, GLfloat right,
746 GLfloat bottom, GLfloat top,
747 GLfloat nearval, GLfloat farval )
748 {
749 GLfloat x, y, z;
750 GLfloat tx, ty, tz;
751 GLfloat m[16];
752
753 x = 2.0F / (right-left);
754 y = 2.0F / (top-bottom);
755 z = -2.0F / (farval-nearval);
756 tx = -(right+left) / (right-left);
757 ty = -(top+bottom) / (top-bottom);
758 tz = -(farval+nearval) / (farval-nearval);
759
760 #define M(row,col) m[col*4+row]
761 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = 0.0F; M(0,3) = tx;
762 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = 0.0F; M(1,3) = ty;
763 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = z; M(2,3) = tz;
764 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = 0.0F; M(3,3) = 1.0F;
765 #undef M
766
767 matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
768 }
769
770
771 #define ZERO(x) (1<<x)
772 #define ONE(x) (1<<(x+16))
773
774 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
775 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
776
777 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
778 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
779 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
780 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
781
782 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
783 ZERO(1) | ZERO(9) | \
784 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
785 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
786
787 #define MASK_2D ( ZERO(8) | \
788 ZERO(9) | \
789 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
790 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
791
792
793 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
794 ZERO(1) | ZERO(9) | \
795 ZERO(2) | ZERO(6) | \
796 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
797
798 #define MASK_3D ( \
799 \
800 \
801 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
802
803
804 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
805 ZERO(1) | ZERO(13) |\
806 ZERO(2) | ZERO(6) | \
807 ZERO(3) | ZERO(7) | ZERO(15) )
808
809 #define SQ(x) ((x)*(x))
810
811 /* Determine type and flags from scratch. This is expensive enough to
812 * only want to do it once.
813 */
814 static void analyse_from_scratch( GLmatrix *mat )
815 {
816 const GLfloat *m = mat->m;
817 GLuint mask = 0;
818 GLuint i;
819
820 for (i = 0 ; i < 16 ; i++) {
821 if (m[i] == 0.0) mask |= (1<<i);
822 }
823
824 if (m[0] == 1.0F) mask |= (1<<16);
825 if (m[5] == 1.0F) mask |= (1<<21);
826 if (m[10] == 1.0F) mask |= (1<<26);
827 if (m[15] == 1.0F) mask |= (1<<31);
828
829 mat->flags &= ~MAT_FLAGS_GEOMETRY;
830
831 /* Check for translation - no-one really cares
832 */
833 if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
834 mat->flags |= MAT_FLAG_TRANSLATION;
835
836 /* Do the real work
837 */
838 if (mask == (GLuint) MASK_IDENTITY) {
839 mat->type = MATRIX_IDENTITY;
840 }
841 else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
842 mat->type = MATRIX_2D_NO_ROT;
843
844 if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
845 mat->flags = MAT_FLAG_GENERAL_SCALE;
846 }
847 else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
848 GLfloat mm = DOT2(m, m);
849 GLfloat m4m4 = DOT2(m+4,m+4);
850 GLfloat mm4 = DOT2(m,m+4);
851
852 mat->type = MATRIX_2D;
853
854 /* Check for scale */
855 if (SQ(mm-1) > SQ(1e-6) ||
856 SQ(m4m4-1) > SQ(1e-6))
857 mat->flags |= MAT_FLAG_GENERAL_SCALE;
858
859 /* Check for rotation */
860 if (SQ(mm4) > SQ(1e-6))
861 mat->flags |= MAT_FLAG_GENERAL_3D;
862 else
863 mat->flags |= MAT_FLAG_ROTATION;
864
865 }
866 else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
867 mat->type = MATRIX_3D_NO_ROT;
868
869 /* Check for scale */
870 if (SQ(m[0]-m[5]) < SQ(1e-6) &&
871 SQ(m[0]-m[10]) < SQ(1e-6)) {
872 if (SQ(m[0]-1.0) > SQ(1e-6)) {
873 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
874 }
875 }
876 else {
877 mat->flags |= MAT_FLAG_GENERAL_SCALE;
878 }
879 }
880 else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
881 GLfloat c1 = DOT3(m,m);
882 GLfloat c2 = DOT3(m+4,m+4);
883 GLfloat c3 = DOT3(m+8,m+8);
884 GLfloat d1 = DOT3(m, m+4);
885 GLfloat cp[3];
886
887 mat->type = MATRIX_3D;
888
889 /* Check for scale */
890 if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) {
891 if (SQ(c1-1.0) > SQ(1e-6))
892 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
893 /* else no scale at all */
894 }
895 else {
896 mat->flags |= MAT_FLAG_GENERAL_SCALE;
897 }
898
899 /* Check for rotation */
900 if (SQ(d1) < SQ(1e-6)) {
901 CROSS3( cp, m, m+4 );
902 SUB_3V( cp, cp, (m+8) );
903 if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
904 mat->flags |= MAT_FLAG_ROTATION;
905 else
906 mat->flags |= MAT_FLAG_GENERAL_3D;
907 }
908 else {
909 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
910 }
911 }
912 else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
913 mat->type = MATRIX_PERSPECTIVE;
914 mat->flags |= MAT_FLAG_GENERAL;
915 }
916 else {
917 mat->type = MATRIX_GENERAL;
918 mat->flags |= MAT_FLAG_GENERAL;
919 }
920 }
921
922
923 /* Analyse a matrix given that its flags are accurate - this is the
924 * more common operation, hopefully.
925 */
926 static void analyse_from_flags( GLmatrix *mat )
927 {
928 const GLfloat *m = mat->m;
929
930 if (TEST_MAT_FLAGS(mat, 0)) {
931 mat->type = MATRIX_IDENTITY;
932 }
933 else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
934 MAT_FLAG_UNIFORM_SCALE |
935 MAT_FLAG_GENERAL_SCALE))) {
936 if ( m[10]==1.0F && m[14]==0.0F ) {
937 mat->type = MATRIX_2D_NO_ROT;
938 }
939 else {
940 mat->type = MATRIX_3D_NO_ROT;
941 }
942 }
943 else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
944 if ( m[ 8]==0.0F
945 && m[ 9]==0.0F
946 && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
947 mat->type = MATRIX_2D;
948 }
949 else {
950 mat->type = MATRIX_3D;
951 }
952 }
953 else if ( m[4]==0.0F && m[12]==0.0F
954 && m[1]==0.0F && m[13]==0.0F
955 && m[2]==0.0F && m[6]==0.0F
956 && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
957 mat->type = MATRIX_PERSPECTIVE;
958 }
959 else {
960 mat->type = MATRIX_GENERAL;
961 }
962 }
963
964
965 void
966 _math_matrix_analyse( GLmatrix *mat )
967 {
968 if (mat->flags & MAT_DIRTY_TYPE) {
969 if (mat->flags & MAT_DIRTY_FLAGS)
970 analyse_from_scratch( mat );
971 else
972 analyse_from_flags( mat );
973 }
974
975 if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
976 matrix_invert( mat );
977 }
978
979 mat->flags &= ~(MAT_DIRTY_FLAGS|
980 MAT_DIRTY_TYPE|
981 MAT_DIRTY_INVERSE);
982 }
983
984
985 void
986 _math_matrix_copy( GLmatrix *to, const GLmatrix *from )
987 {
988 MEMCPY( to->m, from->m, sizeof(Identity) );
989 to->flags = from->flags;
990 to->type = from->type;
991
992 if (to->inv != 0) {
993 if (from->inv == 0) {
994 matrix_invert( to );
995 }
996 else {
997 MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
998 }
999 }
1000 }
1001
1002
1003 void
1004 _math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1005 {
1006 GLfloat *m = mat->m;
1007 m[0] *= x; m[4] *= y; m[8] *= z;
1008 m[1] *= x; m[5] *= y; m[9] *= z;
1009 m[2] *= x; m[6] *= y; m[10] *= z;
1010 m[3] *= x; m[7] *= y; m[11] *= z;
1011
1012 if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
1013 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1014 else
1015 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1016
1017 mat->flags |= (MAT_DIRTY_TYPE |
1018 MAT_DIRTY_INVERSE);
1019 }
1020
1021
1022 void
1023 _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1024 {
1025 GLfloat *m = mat->m;
1026 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1027 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1028 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1029 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1030
1031 mat->flags |= (MAT_FLAG_TRANSLATION |
1032 MAT_DIRTY_TYPE |
1033 MAT_DIRTY_INVERSE);
1034 }
1035
1036
1037 void
1038 _math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
1039 {
1040 MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
1041 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
1042 }
1043
1044 void
1045 _math_matrix_ctr( GLmatrix *m )
1046 {
1047 m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1048 if (m->m)
1049 MEMCPY( m->m, Identity, sizeof(Identity) );
1050 m->inv = NULL;
1051 m->type = MATRIX_IDENTITY;
1052 m->flags = 0;
1053 }
1054
1055 void
1056 _math_matrix_dtr( GLmatrix *m )
1057 {
1058 if (m->m) {
1059 ALIGN_FREE( m->m );
1060 m->m = NULL;
1061 }
1062 if (m->inv) {
1063 ALIGN_FREE( m->inv );
1064 m->inv = NULL;
1065 }
1066 }
1067
1068
1069 void
1070 _math_matrix_alloc_inv( GLmatrix *m )
1071 {
1072 if (!m->inv) {
1073 m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1074 if (m->inv)
1075 MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
1076 }
1077 }
1078
1079
1080 void
1081 _math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
1082 {
1083 dest->flags = (a->flags |
1084 b->flags |
1085 MAT_DIRTY_TYPE |
1086 MAT_DIRTY_INVERSE);
1087
1088 if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
1089 matmul34( dest->m, a->m, b->m );
1090 else
1091 matmul4( dest->m, a->m, b->m );
1092 }
1093
1094
1095 void
1096 _math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
1097 {
1098 dest->flags |= (MAT_FLAG_GENERAL |
1099 MAT_DIRTY_TYPE |
1100 MAT_DIRTY_INVERSE);
1101
1102 matmul4( dest->m, dest->m, m );
1103 }
1104
1105 void
1106 _math_matrix_set_identity( GLmatrix *mat )
1107 {
1108 MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
1109
1110 if (mat->inv)
1111 MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
1112
1113 mat->type = MATRIX_IDENTITY;
1114 mat->flags &= ~(MAT_DIRTY_FLAGS|
1115 MAT_DIRTY_TYPE|
1116 MAT_DIRTY_INVERSE);
1117 }
1118
1119
1120
1121 void
1122 _math_transposef( GLfloat to[16], const GLfloat from[16] )
1123 {
1124 to[0] = from[0];
1125 to[1] = from[4];
1126 to[2] = from[8];
1127 to[3] = from[12];
1128 to[4] = from[1];
1129 to[5] = from[5];
1130 to[6] = from[9];
1131 to[7] = from[13];
1132 to[8] = from[2];
1133 to[9] = from[6];
1134 to[10] = from[10];
1135 to[11] = from[14];
1136 to[12] = from[3];
1137 to[13] = from[7];
1138 to[14] = from[11];
1139 to[15] = from[15];
1140 }
1141
1142
1143 void
1144 _math_transposed( GLdouble to[16], const GLdouble from[16] )
1145 {
1146 to[0] = from[0];
1147 to[1] = from[4];
1148 to[2] = from[8];
1149 to[3] = from[12];
1150 to[4] = from[1];
1151 to[5] = from[5];
1152 to[6] = from[9];
1153 to[7] = from[13];
1154 to[8] = from[2];
1155 to[9] = from[6];
1156 to[10] = from[10];
1157 to[11] = from[14];
1158 to[12] = from[3];
1159 to[13] = from[7];
1160 to[14] = from[11];
1161 to[15] = from[15];
1162 }
1163
1164 void
1165 _math_transposefd( GLfloat to[16], const GLdouble from[16] )
1166 {
1167 to[0] = (GLfloat) from[0];
1168 to[1] = (GLfloat) from[4];
1169 to[2] = (GLfloat) from[8];
1170 to[3] = (GLfloat) from[12];
1171 to[4] = (GLfloat) from[1];
1172 to[5] = (GLfloat) from[5];
1173 to[6] = (GLfloat) from[9];
1174 to[7] = (GLfloat) from[13];
1175 to[8] = (GLfloat) from[2];
1176 to[9] = (GLfloat) from[6];
1177 to[10] = (GLfloat) from[10];
1178 to[11] = (GLfloat) from[14];
1179 to[12] = (GLfloat) from[3];
1180 to[13] = (GLfloat) from[7];
1181 to[14] = (GLfloat) from[11];
1182 to[15] = (GLfloat) from[15];
1183 }