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