1 // -------------------------------------
2 // 3x3 Matrix class
3 // -------------------------------------
4 #include "GLMatrix.h"
5 #include <math.h>
6 
7 // -------------------------------------
8 
GLMatrix()9 GLMatrix::GLMatrix() {
10   Identity();
11 }
12 
13 // -------------------------------------
14 
Init33(float _11,float _12,float _13,float _21,float _22,float _23,float _31,float _32,float _33)15 void GLMatrix::Init33(float _11,float _12,float _13,float _21,float _22,float _23,float _31,float _32,float _33) {
16 
17   this->_11 = _11; this->_12 = _12; this->_13 = _13;
18   this->_21 = _21; this->_22 = _22; this->_23 = _23;
19   this->_31 = _31; this->_32 = _32; this->_33 = _33;
20 
21 }
22 
23 // -------------------------------------
24 
Identity()25 void GLMatrix::Identity() {
26 
27   _11=1.0f;  _12=0.0f;  _13=0.0f; _14=0.0f;
28   _21=0.0f;  _22=1.0f;  _23=0.0f; _24=0.0f;
29   _31=0.0f;  _32=0.0f;  _33=1.0f; _34=0.0f;
30   _41=0.0f;  _42=0.0f;  _43=0.0f; _44=1.0f;
31 
32 }
33 
34 // -------------------------------------
35 
Translate(float x,float y,float z)36 void GLMatrix::Translate(float x,float y,float z) {
37 
38   Identity();
39   _14 = x;
40   _24 = y;
41   _34 = z;
42 
43 }
44 
45 // -------------------------------------
46 
Transpose()47 void GLMatrix::Transpose() {
48 
49   GLMatrix m = *this;
50 
51   _11=m._11;  _12=m._21;  _13=m._31; _14=m._41;
52   _21=m._12;  _22=m._22;  _23=m._32; _24=m._42;
53   _31=m._13;  _32=m._23;  _33=m._33; _34=m._43;
54   _41=m._14;  _42=m._24;  _43=m._34; _44=m._44;
55 
56 }
57 
58 // -------------------------------------
59 
Multiply(GLMatrix * m2)60 void GLMatrix::Multiply(GLMatrix *m2) {
61 
62   GLMatrix m1 = *this;
63 
64   this->_11 = m1._11*m2->_11 + m1._12*m2->_21 + m1._13*m2->_31 + m1._14*m2->_41;
65   this->_12 = m1._11*m2->_12 + m1._12*m2->_22 + m1._13*m2->_32 + m1._14*m2->_42;
66   this->_13 = m1._11*m2->_13 + m1._12*m2->_23 + m1._13*m2->_33 + m1._14*m2->_43;
67   this->_14 = m1._11*m2->_14 + m1._12*m2->_24 + m1._13*m2->_34 + m1._14*m2->_44;
68 
69   this->_21 = m1._21*m2->_11 + m1._22*m2->_21 + m1._23*m2->_31 + m1._24*m2->_41;
70   this->_22 = m1._21*m2->_12 + m1._22*m2->_22 + m1._23*m2->_32 + m1._24*m2->_42;
71   this->_23 = m1._21*m2->_13 + m1._22*m2->_23 + m1._23*m2->_33 + m1._24*m2->_43;
72   this->_24 = m1._21*m2->_14 + m1._22*m2->_24 + m1._23*m2->_34 + m1._24*m2->_44;
73 
74   this->_31 = m1._31*m2->_11 + m1._32*m2->_21 + m1._33*m2->_31 + m1._34*m2->_41;
75   this->_32 = m1._31*m2->_12 + m1._32*m2->_22 + m1._33*m2->_32 + m1._34*m2->_42;
76   this->_33 = m1._31*m2->_13 + m1._32*m2->_23 + m1._33*m2->_33 + m1._34*m2->_43;
77   this->_34 = m1._31*m2->_14 + m1._32*m2->_24 + m1._33*m2->_34 + m1._34*m2->_44;
78 
79   this->_41 = m1._41*m2->_11 + m1._42*m2->_21 + m1._43*m2->_31 + m1._44*m2->_41;
80   this->_42 = m1._41*m2->_12 + m1._42*m2->_22 + m1._43*m2->_32 + m1._44*m2->_42;
81   this->_43 = m1._41*m2->_13 + m1._42*m2->_23 + m1._43*m2->_33 + m1._44*m2->_43;
82   this->_44 = m1._41*m2->_14 + m1._42*m2->_24 + m1._43*m2->_34 + m1._44*m2->_44;
83 
84 }
85 
Multiply(GLMatrix * m2,GLMatrix * m1)86 void GLMatrix::Multiply(GLMatrix *m2,GLMatrix *m1) {
87 
88   this->_11 = m1->_11*m2->_11 + m1->_12*m2->_21 + m1->_13*m2->_31 + m1->_14*m2->_41;
89   this->_12 = m1->_11*m2->_12 + m1->_12*m2->_22 + m1->_13*m2->_32 + m1->_14*m2->_42;
90   this->_13 = m1->_11*m2->_13 + m1->_12*m2->_23 + m1->_13*m2->_33 + m1->_14*m2->_43;
91   this->_14 = m1->_11*m2->_14 + m1->_12*m2->_24 + m1->_13*m2->_34 + m1->_14*m2->_44;
92 
93   this->_21 = m1->_21*m2->_11 + m1->_22*m2->_21 + m1->_23*m2->_31 + m1->_24*m2->_41;
94   this->_22 = m1->_21*m2->_12 + m1->_22*m2->_22 + m1->_23*m2->_32 + m1->_24*m2->_42;
95   this->_23 = m1->_21*m2->_13 + m1->_22*m2->_23 + m1->_23*m2->_33 + m1->_24*m2->_43;
96   this->_24 = m1->_21*m2->_14 + m1->_22*m2->_24 + m1->_23*m2->_34 + m1->_24*m2->_44;
97 
98   this->_31 = m1->_31*m2->_11 + m1->_32*m2->_21 + m1->_33*m2->_31 + m1->_34*m2->_41;
99   this->_32 = m1->_31*m2->_12 + m1->_32*m2->_22 + m1->_33*m2->_32 + m1->_34*m2->_42;
100   this->_33 = m1->_31*m2->_13 + m1->_32*m2->_23 + m1->_33*m2->_33 + m1->_34*m2->_43;
101   this->_34 = m1->_31*m2->_14 + m1->_32*m2->_24 + m1->_33*m2->_34 + m1->_34*m2->_44;
102 
103   this->_41 = m1->_41*m2->_11 + m1->_42*m2->_21 + m1->_43*m2->_31 + m1->_44*m2->_41;
104   this->_42 = m1->_41*m2->_12 + m1->_42*m2->_22 + m1->_43*m2->_32 + m1->_44*m2->_42;
105   this->_43 = m1->_41*m2->_13 + m1->_42*m2->_23 + m1->_43*m2->_33 + m1->_44*m2->_43;
106   this->_44 = m1->_41*m2->_14 + m1->_42*m2->_24 + m1->_43*m2->_34 + m1->_44*m2->_44;
107 
108 }
109 
110 // -------------------------------------
111 
TransfomVec(float x,float y,float z,float w,float * rx,float * ry,float * rz,float * rw)112 void GLMatrix::TransfomVec(float x,float y,float z,float w,float *rx,float *ry,float *rz,float *rw) {
113 
114   *rx = x * _11 + y * _12 + z * _13 + w * _14;
115   *ry = x * _21 + y * _22 + z * _23 + w * _24;
116   *rz = x * _31 + y * _32 + z * _33 + w * _34;
117   *rw = x * _41 + y * _42 + z * _43 + w * _44;
118 
119 }
120 
121 // -------------------------------------
122 
GetGL()123 GLfloat *GLMatrix::GetGL() {
124 
125   static GLfloat ret[16];
126   ret[0] =  _11; ret[4] =  _12; ret[8]  = _13;  ret[12] = _14;
127   ret[1] =  _21; ret[5] =  _22; ret[9]  = _23;  ret[13] = _24;
128   ret[2] =  _31; ret[6] =  _32; ret[10] = _33;  ret[14] = _34;
129   ret[3] =  _41; ret[7] =  _42; ret[11] = _43;  ret[15] = _44;
130   return ret;
131 
132 }
133 
134 // -------------------------------------
135 
FromGL(GLfloat * m)136 void GLMatrix::FromGL(GLfloat *m) {
137 
138   _11 = m[0]; _12 = m[4]; _13 = m[8];  _14 = m[12];
139   _21 = m[1]; _22 = m[5]; _23 = m[9];  _24 = m[13];
140   _31 = m[2]; _32 = m[6]; _33 = m[10]; _34 = m[14];
141   _41 = m[3]; _42 = m[7]; _43 = m[11]; _44 = m[15];
142 
143 }
144 
145 // -------------------------------------
146 
RotateX(float angle)147 void GLMatrix::RotateX(float angle) {
148 
149   float cs = cosf(angle);
150   float sn = sinf(angle);
151 
152   Identity();
153   _22=cs;   _23=-sn;
154   _32=sn;   _33=cs;
155 
156 }
157 
158 // -------------------------------------
159 
RotateY(float angle)160 void GLMatrix::RotateY(float angle) {
161 
162   float cs = cosf(angle);
163   float sn = sinf(angle);
164 
165   Identity();
166   _11=cs;  _13=sn;
167   _31=-sn; _33=cs;
168 
169 }
170 
171 // -------------------------------------
172 
RotateZ(float angle)173 void GLMatrix::RotateZ(float angle) {
174 
175   float cs = cosf(angle);
176   float sn = sinf(angle);
177 
178   Identity();
179   _11=cs;   _12=-sn;
180   _21=sn;   _22=cs;
181 
182 }
183