File: GLMatrix.cpp

package info (click to toggle)
blockout2 2.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,260 kB
  • ctags: 939
  • sloc: cpp: 8,725; ansic: 143; makefile: 105
file content (169 lines) | stat: -rw-r--r-- 5,368 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// -------------------------------------
// 3x3 Matrix class
// -------------------------------------
#include "GLMatrix.h"
#include <math.h>

// -------------------------------------

GLMatrix::GLMatrix() {
  Identity();
}

// -------------------------------------

void GLMatrix::Init33(float _11,float _12,float _13,float _21,float _22,float _23,float _31,float _32,float _33) {

  this->_11 = _11; this->_12 = _12; this->_13 = _13;
  this->_21 = _21; this->_22 = _22; this->_23 = _23;
  this->_31 = _31; this->_32 = _32; this->_33 = _33;

}

// -------------------------------------
  
void GLMatrix::Identity() {

  _11=1.0f;  _12=0.0f;  _13=0.0f; _14=0.0f;
  _21=0.0f;  _22=1.0f;  _23=0.0f; _24=0.0f;
  _31=0.0f;  _32=0.0f;  _33=1.0f; _34=0.0f;
  _41=0.0f;  _42=0.0f;  _43=0.0f; _44=1.0f;

}

// -------------------------------------

void GLMatrix::Translate(float x,float y,float z) {

	Identity();
  _14 = x;
  _24 = y;
  _34 = z;

}
    
// -------------------------------------

void GLMatrix::Multiply(GLMatrix *m2) {

  GLMatrix m1 = *this;
  
  this->_11 = m1._11*m2->_11 + m1._12*m2->_21 + m1._13*m2->_31 + m1._14*m2->_41;
  this->_12 = m1._11*m2->_12 + m1._12*m2->_22 + m1._13*m2->_32 + m1._14*m2->_42;
  this->_13 = m1._11*m2->_13 + m1._12*m2->_23 + m1._13*m2->_33 + m1._14*m2->_43;
  this->_14 = m1._11*m2->_14 + m1._12*m2->_24 + m1._13*m2->_34 + m1._14*m2->_44;

  this->_21 = m1._21*m2->_11 + m1._22*m2->_21 + m1._23*m2->_31 + m1._24*m2->_41;
  this->_22 = m1._21*m2->_12 + m1._22*m2->_22 + m1._23*m2->_32 + m1._24*m2->_42;
  this->_23 = m1._21*m2->_13 + m1._22*m2->_23 + m1._23*m2->_33 + m1._24*m2->_43;
  this->_24 = m1._21*m2->_14 + m1._22*m2->_24 + m1._23*m2->_34 + m1._24*m2->_44;

  this->_31 = m1._31*m2->_11 + m1._32*m2->_21 + m1._33*m2->_31 + m1._34*m2->_41;
  this->_32 = m1._31*m2->_12 + m1._32*m2->_22 + m1._33*m2->_32 + m1._34*m2->_42;
  this->_33 = m1._31*m2->_13 + m1._32*m2->_23 + m1._33*m2->_33 + m1._34*m2->_43;
  this->_34 = m1._31*m2->_14 + m1._32*m2->_24 + m1._33*m2->_34 + m1._34*m2->_44;

	this->_41 = m1._41*m2->_11 + m1._42*m2->_21 + m1._43*m2->_31 + m1._44*m2->_41;
  this->_42 = m1._41*m2->_12 + m1._42*m2->_22 + m1._43*m2->_32 + m1._44*m2->_42;
  this->_43 = m1._41*m2->_13 + m1._42*m2->_23 + m1._43*m2->_33 + m1._44*m2->_43;
  this->_44 = m1._41*m2->_14 + m1._42*m2->_24 + m1._43*m2->_34 + m1._44*m2->_44;

}

void GLMatrix::Multiply(GLMatrix *m2,GLMatrix *m1) {

  this->_11 = m1->_11*m2->_11 + m1->_12*m2->_21 + m1->_13*m2->_31 + m1->_14*m2->_41;
  this->_12 = m1->_11*m2->_12 + m1->_12*m2->_22 + m1->_13*m2->_32 + m1->_14*m2->_42;
  this->_13 = m1->_11*m2->_13 + m1->_12*m2->_23 + m1->_13*m2->_33 + m1->_14*m2->_43;
  this->_14 = m1->_11*m2->_14 + m1->_12*m2->_24 + m1->_13*m2->_34 + m1->_14*m2->_44;

  this->_21 = m1->_21*m2->_11 + m1->_22*m2->_21 + m1->_23*m2->_31 + m1->_24*m2->_41;
  this->_22 = m1->_21*m2->_12 + m1->_22*m2->_22 + m1->_23*m2->_32 + m1->_24*m2->_42;
  this->_23 = m1->_21*m2->_13 + m1->_22*m2->_23 + m1->_23*m2->_33 + m1->_24*m2->_43;
  this->_24 = m1->_21*m2->_14 + m1->_22*m2->_24 + m1->_23*m2->_34 + m1->_24*m2->_44;

  this->_31 = m1->_31*m2->_11 + m1->_32*m2->_21 + m1->_33*m2->_31 + m1->_34*m2->_41;
  this->_32 = m1->_31*m2->_12 + m1->_32*m2->_22 + m1->_33*m2->_32 + m1->_34*m2->_42;
  this->_33 = m1->_31*m2->_13 + m1->_32*m2->_23 + m1->_33*m2->_33 + m1->_34*m2->_43;
  this->_34 = m1->_31*m2->_14 + m1->_32*m2->_24 + m1->_33*m2->_34 + m1->_34*m2->_44;

  this->_41 = m1->_41*m2->_11 + m1->_42*m2->_21 + m1->_43*m2->_31 + m1->_44*m2->_41;
  this->_42 = m1->_41*m2->_12 + m1->_42*m2->_22 + m1->_43*m2->_32 + m1->_44*m2->_42;
  this->_43 = m1->_41*m2->_13 + m1->_42*m2->_23 + m1->_43*m2->_33 + m1->_44*m2->_43;
  this->_44 = m1->_41*m2->_14 + m1->_42*m2->_24 + m1->_43*m2->_34 + m1->_44*m2->_44;
  
}

// -------------------------------------

void GLMatrix::TransfomVec(float x,float y,float z,float w,float *rx,float *ry,float *rz,float *rw) {

  *rx = x * _11 + y * _12 + z * _13 + w * _14;
  *ry = x * _21 + y * _22 + z * _23 + w * _24;
  *rz = x * _31 + y * _32 + z * _33 + w * _34;
  *rw = x * _41 + y * _42 + z * _43 + w * _44;

}

// -------------------------------------

GLfloat *GLMatrix::GetGL() {
  
  static GLfloat ret[16];
  ret[0] =  _11; ret[4] =  _12; ret[8]  = _13;  ret[12] = _14; 
  ret[1] =  _21; ret[5] =  _22; ret[9]  = _23;  ret[13] = _24; 
  ret[2] =  _31; ret[6] =  _32; ret[10] = _33;  ret[14] = _34; 
  ret[3] =  _41; ret[7] =  _42; ret[11] = _43;  ret[15] = _44; 
  return ret;

}

// -------------------------------------

void GLMatrix::FromGL(GLfloat *m) {
  
  _11 = m[0]; _12 = m[4]; _13 = m[8];  _14 = m[12]; 
  _21 = m[1]; _22 = m[5]; _23 = m[9];  _24 = m[13]; 
  _31 = m[2]; _32 = m[6]; _33 = m[10]; _34 = m[14]; 
  _41 = m[3]; _42 = m[7]; _43 = m[11]; _44 = m[15]; 

}

// -------------------------------------

void GLMatrix::RotateX(float angle) {

  float cs = cosf(angle);
  float sn = sinf(angle);

	Identity();
  _22=cs;   _23=-sn;
  _32=sn;   _33=cs;
  
}

// -------------------------------------

void GLMatrix::RotateY(float angle) {

  float cs = cosf(angle);
  float sn = sinf(angle);

	Identity();
  _11=cs;  _13=sn;
  _31=-sn; _33=cs;
  
}

// -------------------------------------

void GLMatrix::RotateZ(float angle) {

  float cs = cosf(angle);
  float sn = sinf(angle);

	Identity();
  _11=cs;   _12=-sn;
  _21=sn;   _22=cs;
  
}