[cogl/wip/rib/cogl-1.12: 83/101] Initialize the inverse matrix in invert_matrix_3d()

• From: Robert Bragg <rbragg src gnome org>
• To: commits-list gnome org
• Cc:
• Subject: [cogl/wip/rib/cogl-1.12: 83/101] Initialize the inverse matrix in invert_matrix_3d()
• Date: Mon, 6 Aug 2012 13:08:02 +0000 (UTC)

```commit e6adf11334a236cb1e5a816937342a3fa96c2cda
Author: Damien Lespiau <damien lespiau intel com>
Date:   Wed Jun 6 17:26:10 2012 +0100

Initialize the inverse matrix in invert_matrix_3d()

Contrary to the other inversion functions, invert_matrix_3d() does not
initialize the inverse to the identity and then only touches the
elements it cares about. Problem is the ww component is left alone,
which makes everything go to a black hole when using an inverse matrix
as the transform matrix of a framebuffer.

This is how cameras are typically implemented, they have a transform
and the framebuffer matrix stack is initialized with the inverse
of that transform. A gdb session gives away what happens:

The camera model view matrix, slightly rotation around the X axis:
camera mv       1.000000 0.000000 0.000000 0.000000
camera mv       0.000000 0.984808 -0.173648 0.000000
camera mv       0.000000 0.173648 0.984808 10.000000
camera mv       0.000000 0.000000 0.000000 1.000000

Breakpoint 5, invert_matrix_3d (matrix=0x8056b58) at ./cogl-matrix.c:671
671       const float *in = (float *)matrix;
(gdb) p *matrix
\$1 = {xx = 1, yx = 0, zx = 0, wx = 0, xy = 0, yy = 0.98480773,
zy = 0.173648164, wy = 0, xz = 0, yz = -0.173648164, zz = 0.98480773,
wz = 0, xw = 0, yw = 0, zw = 10, ww = 1, inv = {0 <repeats 16 times>},
type = 6, flags = 1030, _padding3 = 0}
(gdb) finish
Run till exit from #0  invert_matrix_3d (matrix=0x8056b58)
at ./cogl-matrix.c:671
0x00141ced in _cogl_matrix_update_inverse (matrix=0x8056b58)
at ./cogl-matrix.c:1123
1123          if (inv_mat_tab[matrix->type](matrix))
Value returned is \$2 = 1
(gdb) p *matrix
\$3 = {xx = 1, yx = 0, zx = 0, wx = 0, xy = 0, yy = 0.98480773,
zy = 0.173648164, wy = 0, xz = 0, yz = -0.173648164, zz = 0.98480773,
wz = 0, xw = 0, yw = 0, zw = 10, ww = 1, inv = {1, 0, 0, 0, 0, 0.98480773,
-0.173648164, 0, 0, 0.173648164, 0.98480773, 0, -0, -1.73648167,
-9.84807777, 0}, type = 6, flags = 1030, _padding3 = 0}

Reviewed-by: Robert Bragg <robert linux intel com>

(cherry picked from commit 9a19ea0147eb316247c45cbba6bb70dec5b9be4c)

cogl/cogl-matrix.c |    2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
---
diff --git a/cogl/cogl-matrix.c b/cogl/cogl-matrix.c
index 376af5c..96a2ee9 100644
--- a/cogl/cogl-matrix.c
+++ b/cogl/cogl-matrix.c
@@ -665,6 +665,8 @@ invert_matrix_3d (CoglMatrix *matrix)
const float *in = (float *)matrix;
float *out = matrix->inv;

+  memcpy (out, identity, 16 * sizeof (float));
+
if (!TEST_MAT_FLAGS(matrix, MAT_FLAGS_ANGLE_PRESERVING))
return invert_matrix_3d_general (matrix);

```