-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
137 lines (108 loc) · 3.28 KB
/
Matrix.cpp
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
/**
* File Matrix.cpp
* Description: implementation of Matrix class
*
*/
#include "Matrix.hpp"
#include "Vector.hpp"
// construct an identity matrix as mat
Matrix::Matrix() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i == j)
this->mat[i][j] = 1;
else
this->mat[i][j] = 0;
}
}
}
// multiplication mat <- m * mat
void Matrix::matrix_pre_multiply(Matrix* m) {
Matrix* temp = new Matrix();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
GLfloat sum = 0;
for (int k = 0; k < 4; k++) {
sum += m->mat[i][k] * this->mat[k][j];
}
temp->mat[i][j] = sum;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
this->mat[i][j] = temp->mat[i][j];
}
}
delete temp;
}
// translate the origin of MC
void Matrix::translate(GLfloat tx, GLfloat ty, GLfloat tz) {
this->mat[0][3] += tx;
this->mat[1][3] += ty;
this->mat[2][3] += tz;
this->mat[3][3] = 1;
}
void Matrix::transpose() {
}
// normalize MC
void Matrix::normalize() {
GLfloat norm;
norm = sqrt(this->mat[0][0]*this->mat[0][0] + this->mat[1][0]*this->mat[1][0]+this->mat[2][0]*this->mat[2][0]);
this->mat[0][0] = this->mat[0][0]/norm;
this->mat[1][0] = this->mat[1][0]/norm;
this->mat[2][0] = this->mat[2][0]/norm;
this->mat[0][2] = this->mat[1][0]*this->mat[2][1]-this->mat[2][0]*this->mat[1][1];
this->mat[1][2] = this->mat[2][0]*this->mat[0][1]-this->mat[0][0]*this->mat[2][1];
this->mat[2][2] = this->mat[0][0]*this->mat[1][1]-this->mat[1][0]*this->mat[0][1];
norm = sqrt(this->mat[0][2]*this->mat[0][2] + this->mat[1][2]*this->mat[1][2]+this->mat[2][2]*this->mat[2][2]);
this->mat[0][2] = this->mat[0][2]/norm;
this->mat[1][2] = this->mat[1][2]/norm;
this->mat[2][2] = this->mat[2][2]/norm;
this->mat[0][1] = this->mat[1][2]*this->mat[2][0]-this->mat[1][0]*this->mat[2][2];
this->mat[1][1] = this->mat[2][2]*this->mat[0][0]-this->mat[2][0]*this->mat[0][2];
this->mat[2][1] = this->mat[0][2]*this->mat[1][0]-this->mat[0][0]*this->mat[1][2];
this->mat[3][0] = 0;
this->mat[3][1] = 0;
this->mat[3][2] = 0;
this->mat[3][3] = 1;
}
// v <- mat * v
void Matrix::multiply_vector(GLfloat* v) {
GLfloat sum, temp[4];
for( int i = 0; i < 4; i++ ) {
sum = 0;
for( int j = 0; j < 4; j++ ) {
sum += v[j] * this->mat[i][j];
}
temp[i] = sum;
}
for( int i = 0; i < 4; i++ ) {
v[i] = temp[i];
}
}
// MC <= rotation matrix * MC, i.e., rotate cordinate vectors and the origin
void Matrix::rotate(GLfloat x, GLfloat y, GLfloat z, GLfloat angle) {
angle = angle * 3.1415926/180;
float oneC = 1 - cos(angle);
float COS = cos(angle);
float SIN = sin(angle);
Matrix* m = new Matrix();
m->mat[0][0] = x * x * oneC + cos( angle );
m->mat[0][1] = y * x * oneC + z * sin( angle );
m->mat[0][2] = x * z * oneC - y * SIN;
m->mat[0][3] = 0;
m->mat[1][0] = x * y * oneC - z * SIN;
m->mat[1][1] = y * y * oneC + COS;
m->mat[1][2] = y * z * oneC + x * SIN;
m->mat[1][3] = 0;
m->mat[2][0] = x * z * oneC + y * SIN;
m->mat[2][1] = y * z * oneC - x * SIN;
m->mat[2][2] = z * z * oneC + COS;
m->mat[2][3] = 0;
m->mat[3][0] = 0;
m->mat[3][1] = 0;
m->mat[3][2] = 0;
m->mat[3][3] = 1;
this->matrix_pre_multiply(m);
delete m;
}