From ee7c89c5a0c49dae8905f85e6c670f44148073ed Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Mon, 1 Nov 2004 15:10:36 +0000
Subject: [PATCH] trying to fix problem reported by T. Schumacher: use
 double[16] instead of double[4][4] for opengl matrices

---
 Common/Context.cpp   | 31 ++++++++++++++++++------------
 Common/Context.h     |  6 +++---
 Common/Trackball.cpp | 45 +++++++++++++++++++++++---------------------
 Common/Trackball.h   |  2 +-
 Graphics/Axes.cpp    | 14 +++++++-------
 Graphics/Draw.cpp    | 14 ++++++--------
 Graphics/Post.cpp    |  8 ++++----
 7 files changed, 64 insertions(+), 56 deletions(-)

diff --git a/Common/Context.cpp b/Common/Context.cpp
index c2760c336b..ae37277810 100644
--- a/Common/Context.cpp
+++ b/Common/Context.cpp
@@ -1,4 +1,4 @@
-// $Id: Context.cpp,v 1.52 2004-10-28 03:44:36 geuzaine Exp $
+// $Id: Context.cpp,v 1.53 2004-11-01 15:10:36 geuzaine Exp $
 //
 // Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle
 //
@@ -29,27 +29,34 @@
 #include "DefaultOptions.h"
 #include "Trackball.h"
 
+/*
+00 01 02 03    0  1  2  3
+10 11 12 13    4  5  6  7
+20 21 22 23    8  9  10 11
+30 31 32 33    12 13 14 15
+*/
+
 void Context_T::buildRotmatrix(void)
 {
   if(useTrackball) {
     build_rotmatrix(rot, quaternion);
 
     // get Euler angles from rotation matrix
-    r[1] = asin(rot[2][0]); // Calculate Y-axis angle
+    r[1] = asin(rot[8]); // Calculate Y-axis angle
     double C =  cos(r[1]);
     r[1] *=  180. / Pi;
     if(fabs(C) > 0.005){ // Gimball lock?
-      double tmpx =  rot[2][2] / C; // No, so get X-axis angle
-      double tmpy = -rot[2][1] / C;
+      double tmpx =  rot[10] / C; // No, so get X-axis angle
+      double tmpy = -rot[9] / C;
       r[0] = atan2(tmpy, tmpx) * 180. / Pi;
-      tmpx =  rot[0][0] / C; // Get Z-axis angle
-      tmpy = -rot[1][0] / C;
+      tmpx =  rot[0] / C; // Get Z-axis angle
+      tmpy = -rot[4] / C;
       r[2] = atan2(tmpy, tmpx) * 180. / Pi;
     }
     else{ // Gimball lock has occurred
       r[0] = 0.; // Set X-axis angle to zero
-      double tmpx = rot[1][1]; // And calculate Z-axis angle
-      double tmpy = rot[0][1];
+      double tmpx = rot[5]; // And calculate Z-axis angle
+      double tmpy = rot[1];
       r[2] = atan2(tmpy, tmpx) * 180. / Pi;
     }
     // return only positive angles in [0,360]
@@ -69,10 +76,10 @@ void Context_T::buildRotmatrix(void)
     double F = sin(z);
     double AD = A * D;
     double BD = B * D;
-    rot[0][0] = C*E; rot[0][1] = BD*E+A*F; rot[0][2] =-AD*E+B*F; rot[0][3] = 0.;
-    rot[1][0] =-C*F; rot[1][1] =-BD*F+A*E; rot[1][2] = AD*F+B*E; rot[1][3] = 0.;
-    rot[2][0] = D;   rot[2][1] =-B*C;	   rot[2][2] = A*C;      rot[2][3] = 0.;
-    rot[3][0] = 0.;  rot[3][1] = 0.;       rot[3][2] = 0.;       rot[3][3] = 1.;
+    rot[0] = C*E; rot[1] = BD*E+A*F; rot[2] =-AD*E+B*F; rot[3] = 0.;
+    rot[4] =-C*F; rot[5] =-BD*F+A*E; rot[6] = AD*F+B*E; rot[7] = 0.;
+    rot[8] = D;   rot[9] =-B*C;	     rot[10] = A*C;     rot[11] = 0.;
+    rot[12] = 0.; rot[13] = 0.;      rot[14] = 0.;      rot[15] = 1.;
 
     // get the quaternion from the Euler angles
     // todo
diff --git a/Common/Context.h b/Common/Context.h
index 673da93afe..4f32a536b9 100644
--- a/Common/Context.h
+++ b/Common/Context.h
@@ -88,9 +88,9 @@ public :
   int initial_context;        // 0=automatic; 1=geom; 2=mesh; 3=solver; 4=post 
   int verbosity;              // 0=silent -> 3=debug 
 
-  double rot[4][4];           // current rotation matrix 
-  double mod[4][4];           // current modelview matrix 
-  double proj[4][4];          // current projection matrix
+  double rot[16];             // current rotation matrix 
+  double mod[16];             // current modelview matrix 
+  double proj[16];            // current projection matrix
   double r[3];                // position angles (if succ. rot. along x, y and z) 
   double t[3], s[3];          // current translation and scale 
   int rlock[3], tlock[3], slock[3];
diff --git a/Common/Trackball.cpp b/Common/Trackball.cpp
index 9254c59326..32af3b2d63 100644
--- a/Common/Trackball.cpp
+++ b/Common/Trackball.cpp
@@ -1,4 +1,4 @@
-/* $Id: Trackball.cpp,v 1.3 2004-10-28 03:44:36 geuzaine Exp $ */
+/* $Id: Trackball.cpp,v 1.4 2004-11-01 15:10:36 geuzaine Exp $ */
 /*
  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  * ALL RIGHTS RESERVED
@@ -50,6 +50,9 @@
  * Much mucking with by:
  * Gavin Bell
  */
+/*
+ * Modified for inclusion in Gmsh (rotmatrix as a vector + float->double
+ */
 #include <math.h>
 #include "Trackball.h"
 
@@ -300,26 +303,26 @@ normalize_quat(double q[4])
  *
  */
 void
-build_rotmatrix(double m[4][4], double q[4])
+build_rotmatrix(double m[16], double q[4])
 {
-    m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
-    m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
-    m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
-    m[0][3] = 0.0;
-
-    m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
-    m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
-    m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
-    m[1][3] = 0.0;
-
-    m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
-    m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
-    m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
-    m[2][3] = 0.0;
-
-    m[3][0] = 0.0;
-    m[3][1] = 0.0;
-    m[3][2] = 0.0;
-    m[3][3] = 1.0;
+    m[0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
+    m[1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
+    m[2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
+    m[3] = 0.0;
+
+    m[4] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
+    m[5]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
+    m[6] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
+    m[7] = 0.0;
+
+    m[8] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
+    m[9] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
+    m[10] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
+    m[11] = 0.0;
+
+    m[12] = 0.0;
+    m[13] = 0.0;
+    m[14] = 0.0;
+    m[15] = 1.0;
 }
 
diff --git a/Common/Trackball.h b/Common/Trackball.h
index af27549051..d6f4f144ca 100644
--- a/Common/Trackball.h
+++ b/Common/Trackball.h
@@ -66,7 +66,7 @@ add_quats(double *q1, double *q2, double *dest);
  * given quaternion.
  */
 void
-build_rotmatrix(double m[4][4], double q[4]);
+build_rotmatrix(double m[16], double q[4]);
 
 /*
  * This function computes a quaternion based on an axis (defined by
diff --git a/Graphics/Axes.cpp b/Graphics/Axes.cpp
index 45739bbb8e..4deb224a8e 100644
--- a/Graphics/Axes.cpp
+++ b/Graphics/Axes.cpp
@@ -1,4 +1,4 @@
-// $Id: Axes.cpp,v 1.21 2004-04-24 16:24:34 geuzaine Exp $
+// $Id: Axes.cpp,v 1.22 2004-11-01 15:10:36 geuzaine Exp $
 //
 // Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle
 //
@@ -135,12 +135,12 @@ void Draw_SmallAxes(void)
   else
     cy = CTX.viewport[1] - CTX.small_axes_pos[1];
 
-  xx = l * CTX.rot[0][0];
-  xy = l * CTX.rot[0][1];
-  yx = l * CTX.rot[1][0];
-  yy = l * CTX.rot[1][1];
-  zx = l * CTX.rot[2][0];
-  zy = l * CTX.rot[2][1];
+  xx = l * CTX.rot[0];
+  xy = l * CTX.rot[1];
+  yx = l * CTX.rot[4];
+  yy = l * CTX.rot[5];
+  zx = l * CTX.rot[8];
+  zy = l * CTX.rot[9];
 
   glLineWidth(CTX.line_width);
   gl2psLineWidth(CTX.line_width * CTX.print.eps_line_width_factor);
diff --git a/Graphics/Draw.cpp b/Graphics/Draw.cpp
index a4bfde0a85..915a8ba32a 100644
--- a/Graphics/Draw.cpp
+++ b/Graphics/Draw.cpp
@@ -1,4 +1,4 @@
-// $Id: Draw.cpp,v 1.63 2004-10-28 03:44:37 geuzaine Exp $
+// $Id: Draw.cpp,v 1.64 2004-11-01 15:10:36 geuzaine Exp $
 //
 // Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle
 //
@@ -217,7 +217,7 @@ void InitPosition(void)
 		 CTX.rotation_center[2]);
   
   CTX.buildRotmatrix();
-  glMultMatrixd(&(CTX.rot[0][0]));
+  glMultMatrixd(CTX.rot);
 
   if(CTX.rotation_center_cg)
     glTranslated(-CTX.cg[0], -CTX.cg[1], -CTX.cg[2]);
@@ -227,8 +227,8 @@ void InitPosition(void)
 		 -CTX.rotation_center[2]);
 
   // store the modelview and projection matrices
-  glGetDoublev(GL_MODELVIEW_MATRIX, &(CTX.mod[0][0]));
-  glGetDoublev(GL_PROJECTION_MATRIX, &(CTX.proj[0][0]));
+  glGetDoublev(GL_MODELVIEW_MATRIX, CTX.mod);
+  glGetDoublev(GL_PROJECTION_MATRIX, CTX.proj);
 }
 
 // Entity selection
@@ -334,11 +334,9 @@ void unproject(double x, double y, double p[3], double d[3])
 
   GLdouble x0, y0, z0, x1, y1, z1;
   
-  if(!gluUnProject(x, y, 0.0, &(CTX.mod[0][0]), &(CTX.proj[0][0]),
-		   viewport, &x0, &y0, &z0))
+  if(!gluUnProject(x, y, 0.0, CTX.mod, CTX.proj, viewport, &x0, &y0, &z0))
     Msg(WARNING, "unproject1 failed");
-  if(!gluUnProject(x, y, 1.0, &(CTX.mod[0][0]), &(CTX.proj[0][0]),
-		   viewport, &x1, &y1, &z1))
+  if(!gluUnProject(x, y, 1.0, CTX.mod, CTX.proj, viewport, &x1, &y1, &z1))
     Msg(WARNING, "unproject2 failed");
   
   p[0] = x0;
diff --git a/Graphics/Post.cpp b/Graphics/Post.cpp
index 5d61a27fb7..6068384869 100644
--- a/Graphics/Post.cpp
+++ b/Graphics/Post.cpp
@@ -1,4 +1,4 @@
-// $Id: Post.cpp,v 1.82 2004-10-27 17:27:40 geuzaine Exp $
+// $Id: Post.cpp,v 1.83 2004-11-01 15:10:36 geuzaine Exp $
 //
 // Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle
 //
@@ -166,9 +166,9 @@ static double storedEye[3] = { 0., 0., 0. };
 int changedEye()
 {
   double zeye = 100 * CTX.lc, tmp[3];
-  tmp[0] = CTX.rot[0][2] * zeye;
-  tmp[1] = CTX.rot[1][2] * zeye;
-  tmp[2] = CTX.rot[2][2] * zeye;
+  tmp[0] = CTX.rot[2] * zeye;
+  tmp[1] = CTX.rot[6] * zeye;
+  tmp[2] = CTX.rot[10] * zeye;
   if(fabs(tmp[0] - storedEye[0]) > 1.e-3 ||
      fabs(tmp[1] - storedEye[1]) > 1.e-3 ||
      fabs(tmp[2] - storedEye[2]) > 1.e-3) {
-- 
GitLab