From 4705d0e5ef5621c120901150cd5c351c0467da81 Mon Sep 17 00:00:00 2001 From: cignoni Date: Tue, 4 May 2004 23:19:41 +0000 Subject: [PATCH] Clarified initial comment, removed vector*matrix operator (confusing!) Corrected translate and Rotate, removed gl stuff. --- vcg/math/matrix44.h | 95 +++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 51 deletions(-) diff --git a/vcg/math/matrix44.h b/vcg/math/matrix44.h index 8dc93f77..fa3ac273 100644 --- a/vcg/math/matrix44.h +++ b/vcg/math/matrix44.h @@ -24,11 +24,17 @@ History $Log: not supported by cvs2svn $ +Revision 1.14 2004/05/04 02:34:03 ganovelli +wrong use of operator [] corrected + Revision 1.13 2004/04/07 10:45:54 cignoni Added: [i][j] access, V() for the raw float values, constructor from T[16] Revision 1.12 2004/03/25 14:57:49 ponchio Microerror. ($LOG$ -> $Log: not supported by cvs2svn $ +Microerror. ($LOG$ -> Revision 1.14 2004/05/04 02:34:03 ganovelli +Microerror. ($LOG$ -> wrong use of operator [] corrected +Microerror. ($LOG$ -> Microerror. ($LOG$ -> Revision 1.13 2004/04/07 10:45:54 cignoni Microerror. ($LOG$ -> Added: [i][j] access, V() for the raw float values, constructor from T[16] Microerror. ($LOG$ -> @@ -39,6 +45,7 @@ Microerror. ($LOG$ -> #ifndef __VCGLIB_MATRIX44 #define __VCGLIB_MATRIX44 +#include #include #include #include @@ -55,7 +62,10 @@ Opengl stores matrix in column-major order. That is, the matrix is stored as: a2 a6 a10 a14 a3 a7 a11 a15 -e.g. glTranslate generate a matrix that is + Usually in opengl (see opengl specs) vectors are 'column' vectors + so usually matrix are PRE-multiplied for a vector. + So the command glTranslate generate a matrix that + is ready to be premultipled for a vector: 1 0 0 tx 0 1 0 ty @@ -69,12 +79,11 @@ Matrix44 stores matrix in row-major order i.e. a8 a9 a10 a11 a12 a13 a14 a15 -and the Translate Function generate: - - 1 0 0 0 - 0 1 0 0 - 0 0 1 0 - tx ty tz 1 +So for the use of that matrix in opengl with their supposed meaning you have to transpose them before feeding to glMultMatrix. +This mechanism is hidden by the templated function defined in wrap/gl/math.h; +If your machine has the ARB_transpose_matrix extension it will use the appropriate; +The various gl-like command SetRotate, SetTranslate assume that you are making matrix +for 'column' vectors. */ @@ -155,15 +164,10 @@ protected: bool Decompose(); }; -/// Apply POST moltiplica la matrice al vettore (e.g. la traslazione deve stare nell'ultima riga) -/// Project PRE moltiplica la matrice al vettore (e.g. la traslazione deve stare nell'ultima colonna) +/*** Postmultiply */ +//template Point3 operator*(const Point3 &p, const Matrix44 &m); -/*** Postmultiply (old Apply in the old interface). - * SetTranslate, SetScale, SetRotate prepare the matrix for this. - */ -template Point3 operator*(const Point3 &p, const Matrix44 &m); - -///Premultiply (old Project in the old interface) +///Premultiply template Point3 operator*(const Matrix44 &m, const Point3 &p); template Matrix44 &Transpose(Matrix44 &m); @@ -343,9 +347,9 @@ template Matrix44 &Matrix44::SetTranslate(const Point3 &t) { } template Matrix44 &Matrix44::SetTranslate(const T sx, const T sy, const T sz) { SetIdentity(); - element(3, 0) = sx; - element(3, 1) = sy; - element(3, 2) = sz; + element(0, 3) = sx; + element(1, 3) = sy; + element(2, 3) = sz; return *this; } template Matrix44 &Matrix44::SetRotate(T angle, const Point3 & axis) { @@ -356,20 +360,20 @@ template Matrix44 &Matrix44::SetRotate(T angle, const Point3 Point3 t = axis; t.Normalize(); element(0,0) = t[0]*t[0]*q + c; - element(1,0) = t[0]*t[1]*q - t[2]*s; - element(2,0) = t[0]*t[2]*q + t[1]*s; - element(3,0) = 0; - element(0,1) = t[1]*t[0]*q + t[2]*s; - element(1,1) = t[1]*t[1]*q + c; - element(2,1) = t[1]*t[2]*q - t[0]*s; - element(3,1) = 0; - element(0,2) = t[2]*t[0]*q -t[1]*s; - element(1,2) = t[2]*t[1]*q +t[0]*s; - element(2,2) = t[2]*t[2]*q +c; - element(3,2) = 0; + element(0,1) = t[0]*t[1]*q - t[2]*s; + element(0,2) = t[0]*t[2]*q + t[1]*s; element(0,3) = 0; - element(1,3) = 0; + element(1,0) = t[1]*t[0]*q + t[2]*s; + element(1,1) = t[1]*t[1]*q + c; + element(1,2) = t[1]*t[2]*q - t[0]*s; + element(1,3) = 0; + element(2,0) = t[2]*t[0]*q -t[1]*s; + element(2,1) = t[2]*t[1]*q +t[0]*s; + element(2,2) = t[2]*t[2]*q +c; element(2,3) = 0; + element(3,0) = 0; + element(3,1) = 0; + element(3,2) = 0; element(3,3) = 1; return *this; } @@ -392,16 +396,16 @@ template Point3 operator*(const Matrix44 &m, const Point3 &p) return s; } -template Point3 operator*(const Point3 &p, const Matrix44 &m) { - T w; - Point3 s; - s[0] = m.element(0, 0)*p[0] + m.element(1, 0)*p[1] + m.element(2, 0)*p[2] + m.element(3, 0); - s[1] = m.element(0, 1)*p[0] + m.element(1, 1)*p[1] + m.element(2, 1)*p[2] + m.element(3, 1); - s[2] = m.element(0, 2)*p[0] + m.element(1, 2)*p[1] + m.element(2, 2)*p[2] + m.element(3, 2); - w = m.element(0, 3)*p[0] + m.element(1, 3)*p[1] + m.element(2, 3)*p[2] + m.element(3, 3); - if(w != 0) s /= w; - return s; -} +//template Point3 operator*(const Point3 &p, const Matrix44 &m) { +// T w; +// Point3 s; +// s[0] = m.element(0, 0)*p[0] + m.element(1, 0)*p[1] + m.element(2, 0)*p[2] + m.element(3, 0); +// s[1] = m.element(0, 1)*p[0] + m.element(1, 1)*p[1] + m.element(2, 1)*p[2] + m.element(3, 1); +// s[2] = m.element(0, 2)*p[0] + m.element(1, 2)*p[1] + m.element(2, 2)*p[2] + m.element(3, 2); +// w = m.element(0, 3)*p[0] + m.element(1, 3)*p[1] + m.element(2, 3)*p[2] + m.element(3, 3); +// if(w != 0) s /= w; +// return s; +//} template Matrix44 &Transpose(Matrix44 &m) { for(int i = 1; i < 4; i++) @@ -607,14 +611,3 @@ template Point4 LinearSolve::Solve(const Point4 &b) { #endif - - -/*#ifdef __GL_H__ -// Applicano la trasformazione intesa secondo la Apply -void glMultMatrix(Matrix44 const & M) { glMultMatrixd(&(M.a[0][0]));} -void glMultMatrix(Matrix44 const & M) { glMultMatrixf(&(M.a[0][0]));} -void glLoadMatrix(Matrix44 const & M) { glLoadMatrixd(&(M.a[0][0]));} -void glLoadMatrix(Matrix44 const & M) { glLoadMatrixf(&(M.a[0][0]));} -#endif*/ - -