Repository navigation
Expand file tree
/
Copy pathGLMatrix.cpp
More file actions
178 lines (138 loc) · 3.88 KB
/
Copy pathGLMatrix.cpp
File metadata and controls
178 lines (138 loc) · 3.88 KB
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
170
171
172
173
174
175
176
177
178
#include "GLMatrix.hpp"
GLMatrix::GLMatrix(const std::initializer_list<GLfloat> &values)
{
data_ = new GLfloat [16];
auto iterator = values.begin();
for (int i = 0; i < 16; ++i)
data_[i] = *iterator++;
}
std::ostream &operator<<(std::ostream &stream, const GLMatrix &matrix)
{
for (int r = 0; r < 4; ++r) {
for (int c = 0; c < 4; ++c)
stream << std::setw(8) << std::setprecision(4) << std::fixed << matrix.data_[GLMatrix::index_(r, c)] << " ";
stream << "\n";
}
return stream;
}
GLMatrix operator*(const GLfloat &k, const GLMatrix &a)
{
GLMatrix result(a);
for (int i = 0; i < 16; ++i)
result.data_[i] *= k;
return result;
}
GLMatrix GLMatrix::operator*(const GLfloat k) const
{
GLMatrix result(*this);
for (int i = 0; i < 16; ++i)
result.data_[i] *= k;
return result;
}
GLMatrix GLMatrix::operator*(const GLMatrix& other) const
{
GLMatrix result;
for (int r = 0; r < 4; ++r) {
for (int c = 0; c < 4; ++c) {
result.data_[index_(r, c)] = data_[index_(r, 0)] * other.data_[index_(0, c)]
+ data_[index_(r, 1)] * other.data_[index_(1, c)]
+ data_[index_(r, 2)] * other.data_[index_(2, c)]
+ data_[index_(r, 3)] * other.data_[index_(3, c)];
}
}
return result;
}
void GLMatrix::operator*=(const GLfloat k)
{
for (int i = 0; i < 16; ++i)
data_[i] *= k;
}
void GLMatrix::operator*=(const GLMatrix& other)
{
GLMatrix tmp = other * (*this);
data_ = tmp.data_;
tmp.data_ = nullptr;
}
GLMatrix GLMatrix::Ones()
{
GLMatrix result;
for (int i = 0; i < 16; ++i)
result.data_[i] = 1;
return result;
}
GLMatrix GLMatrix::Identity()
{
GLMatrix result;
for (int i = 0; i < 4; ++i)
result.data_[index_(i, i)] = 1;
return result;
}
GLMatrix GLMatrix::Scale(GLfloat kx, GLfloat ky, GLfloat kz, GLfloat kw)
{
GLMatrix result;
result.data_[0] = kx;
result.data_[5] = ky;
result.data_[10] = kz;
result.data_[15] = kw;
return result;
}
GLMatrix GLMatrix::Zeros()
{
return GLMatrix();
}
GLMatrix GLMatrix::Translation(GLfloat rx, GLfloat ry, GLfloat rz)
{
GLMatrix result = Identity();
result.data_[12] = rx;
result.data_[13] = ry;
result.data_[14] = rz;
return result;
}
GLMatrix GLMatrix::Rotation(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
{
const GLfloat s = std::sin(angle);
const GLfloat c = std::cos(angle);
const GLfloat cp = 1.0 - c;
return GLMatrix({
c + x * x * cp, x * y * cp - z * s, x * z * cp + y * s, 0,
y * x * cp + z * s, c + y * y * cp, y * z * cp - x * s, 0,
x * z * cp - y * s, z * y * cp + x * s, c + z * z * cp, 0,
0, 0, 0, 1
});
}
GLMatrix GLMatrix::Projection(GLfloat width, GLfloat height, GLfloat near, GLfloat far)
{
GLMatrix projection = Zeros();
projection.data_[index_(0, 0)] = 2 * near / width;
projection.data_[index_(1, 1)] = 2 * near / height;
projection.data_[index_(2, 2)] = (far + near) / (near - far);
projection.data_[index_(2, 3)] = 2 * far * near / (near - far);
projection.data_[index_(3, 2)] = -1;
return projection;
}
GLMatrix::RowProxy GLMatrix::operator[](int index)
{
return RowProxy(this, index);
}
GLMatrix::RowProxyConst GLMatrix::operator[](int index) const
{
return RowProxyConst(this, index);
}
GLMatrix::RowProxy::RowProxy(GLMatrix* parent, int row)
{
parent_ = parent;
row_ = row;
}
GLfloat& GLMatrix::RowProxy::operator[](int index)
{
return parent_->data_[GLMatrix::index_(row_, index)];
}
GLMatrix::RowProxyConst::RowProxyConst(const GLMatrix* parent, int row)
{
parent_ = parent;
row_ = row;
}
GLfloat GLMatrix::RowProxyConst::operator[](int index) const
{
return parent_->data_[GLMatrix::index_(row_, index)];
}