teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

追加

2019/02/08 13:16

投稿

miiichat
miiichat

スコア72

title CHANGED
File without changes
body CHANGED
@@ -17,4 +17,203 @@
17
17
  ```
18
18
 
19
19
  const int j(i & 3), k(i & ~3);
20
- の部分の & は何をしてますか?
20
+ の部分の & は何をしてますか?
21
+
22
+ ---追記---
23
+ GLFW による OpenGL 入門(130p)
24
+ http://marina.sys.wakayama-u.ac.jp/~tokoi/GLFWdraft.pdf
25
+
26
+ をやってます。実行できます
27
+ ```
28
+ #pragma once
29
+ #include <algorithm>
30
+ #include <GL/glew.h>
31
+ #include <cmath>
32
+
33
+ class Matrix
34
+ {
35
+ GLfloat matrix[16];
36
+
37
+ public:
38
+
39
+ Matrix() {}
40
+
41
+ Matrix(const GLfloat *a)
42
+ {
43
+ std::copy(a, a + 16, matrix);
44
+ }
45
+
46
+ const GLfloat *data() const
47
+ {
48
+ return matrix;
49
+ }
50
+
51
+ void loadIdentity()
52
+ {
53
+ std::fill(matrix, matrix + 16, 0.0f);
54
+ matrix[0] = matrix[5] = matrix[10] = matrix[15] = 1.0f;
55
+ }
56
+
57
+ static Matrix identity()
58
+ {
59
+ Matrix t;
60
+ t.loadIdentity();
61
+ return t;
62
+ }
63
+
64
+ static Matrix translate(GLfloat x, GLfloat y, GLfloat z)
65
+ {
66
+ Matrix t;
67
+ t.loadIdentity();
68
+ t.matrix[12] = x;
69
+ t.matrix[13] = y;
70
+ t.matrix[14] = z;
71
+
72
+ return t;
73
+ }
74
+
75
+ static Matrix scale(GLfloat x, GLfloat y, GLfloat z)
76
+ {
77
+ Matrix t;
78
+ t.loadIdentity();
79
+ t.matrix[0] = x;
80
+ t.matrix[5] = y;
81
+ t.matrix[10] = z;
82
+
83
+ return t;
84
+ }
85
+
86
+ static Matrix shearXY(GLfloat angle)
87
+ {
88
+ Matrix t;
89
+ t.loadIdentity();
90
+ t.matrix[4] = angle;
91
+
92
+ return t;
93
+ }
94
+
95
+ static Matrix shearYZ(GLfloat angle)
96
+ {
97
+ Matrix t;
98
+ t.loadIdentity();
99
+ t.matrix[9] = angle;
100
+
101
+ return t;
102
+ }
103
+
104
+ static Matrix shearZX(GLfloat angle)
105
+ {
106
+ Matrix t;
107
+ t.loadIdentity();
108
+ t.matrix[2] = angle;
109
+
110
+ return t;
111
+ }
112
+
113
+ static Matrix shearYX(GLfloat angle)
114
+ {
115
+ Matrix t;
116
+ t.loadIdentity();
117
+ t.matrix[1] = angle;
118
+
119
+ return t;
120
+ }
121
+
122
+ static Matrix shearZY(GLfloat angle)
123
+ {
124
+ Matrix t;
125
+ t.loadIdentity();
126
+ t.matrix[6] = angle;
127
+
128
+ return t;
129
+ }
130
+
131
+ static Matrix shearXZ(GLfloat angle)
132
+ {
133
+ Matrix t;
134
+ t.loadIdentity();
135
+ t.matrix[8] = angle;
136
+
137
+ return t;
138
+ }
139
+
140
+ static Matrix rotationX(GLfloat angle)
141
+ {
142
+ Matrix t;
143
+ t.loadIdentity();
144
+ t.matrix[5] = cos(angle);
145
+ t.matrix[6] = sin(angle);
146
+ t.matrix[9] = -sin(angle);
147
+ t.matrix[10] = cos(angle);
148
+
149
+ return t;
150
+ }
151
+
152
+ static Matrix rotationY(GLfloat angle)
153
+ {
154
+ Matrix t;
155
+ t.loadIdentity();
156
+ t.matrix[0] = cos(angle);
157
+ t.matrix[2] = sin(angle);
158
+ t.matrix[8] = -sin(angle);
159
+ t.matrix[10] = cos(angle);
160
+
161
+ return t;
162
+ }
163
+
164
+ static Matrix rotationZ(GLfloat angle)
165
+ {
166
+ Matrix t;
167
+ t.loadIdentity();
168
+ t.matrix[0] = cos(angle);
169
+ t.matrix[1] = sin(angle);
170
+ t.matrix[4] = -sin(angle);
171
+ t.matrix[5] = cos(angle);
172
+
173
+ return t;
174
+ }
175
+
176
+ static Matrix rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z)
177
+ {
178
+ Matrix t;
179
+ const GLfloat d(sqrt(x * x + y * y + z * z));
180
+
181
+ if (d > 0.0f)
182
+ {
183
+ const GLfloat l(x / d), m(y / d), n(z / d);
184
+ const GLfloat l2(l * l), m2(m * m), n2(n * n);
185
+ const GLfloat lm(l * m), mn(m * n), nl(n * l);
186
+ const GLfloat c(cos(a)), c1(1.0f - c), s(sin(a));
187
+
188
+ t.loadIdentity();
189
+ t.matrix[0] = (1.0f - l2) * c + l2;
190
+ t.matrix[1] = lm * c1 + n * s;
191
+ t.matrix[2] = nl * c1 - m * s;
192
+ t.matrix[4] = lm * c1 - n * s;
193
+ t.matrix[5] = (1.0f - m2) * c + m2;
194
+ t.matrix[6] = mn * c1 + l * s;
195
+ t.matrix[8] = nl * c1 + m * s;
196
+ t.matrix[9] = mn * c1 - l * s;
197
+ t.matrix[10] = (1.0f - n2) * c + n2;
198
+ }
199
+ return t;
200
+ }
201
+
202
+ Matrix operator*(const Matrix &m) const
203
+ {
204
+ Matrix t;
205
+
206
+ for (int i = 0; i < 16; ++i)
207
+ {
208
+ const int j(i & 3), k(i & ~3);
209
+
210
+ t.matrix[i] =
211
+ matrix[0 + j] * matrix[k + 0] +
212
+ matrix[4 + j] * matrix[k + 1] +
213
+ matrix[8 + j] * matrix[k + 2] +
214
+ matrix[12+j] * matrix[k + 3];
215
+ }
216
+ return t;
217
+ }
218
+ };
219
+ ```