-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathglmatrix.hpp
462 lines (390 loc) · 16.3 KB
/
glmatrix.hpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#pragma once
//=====================================================================//
/*! @file
@brief OpenGL マトリックス・エミュレーター
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2017, 2021 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include "common/vtx.hpp"
#include "common/mtx.hpp"
#include "common/fixed_stack.hpp"
#include "common/format.hpp"
namespace gl {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief OpenGL matrix エミュレータークラス
@param[in] T 基本型(float、又は double)
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <typename T>
struct matrix {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief マトリックス・モード
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
enum class mode {
modelview,
projection,
num_
};
typedef T value_type;
typedef mtx::matrix4<T> matrix_type;
private:
static constexpr uint32_t STACK_SIZE = 4;
mode mode_;
matrix_type acc_[static_cast<int>(mode::num_)];
typedef utils::fixed_stack<matrix_type, STACK_SIZE> STACK;
STACK stack_;
T near_;
T far_;
int vp_x_;
int vp_y_;
int vp_w_;
int vp_h_;
public:
//-----------------------------------------------------------------//
/*!
@brief OpenGL 系マトリックス操作コンストラクター
*/
//-----------------------------------------------------------------//
matrix() : mode_(mode::modelview),
near_(0.0f), far_(1.0f),
vp_x_(0), vp_y_(0), vp_w_(0), vp_h_(0)
{ }
//-----------------------------------------------------------------//
/*!
@brief OpenGL マトリックスモードを設定
@param[in] md マトリックス・モード
*/
//-----------------------------------------------------------------//
void set_mode(mode md) { mode_ = md; }
//-----------------------------------------------------------------//
/*!
@brief OpenGL マトリックスモードを取得
@return マトリックスモード
*/
//-----------------------------------------------------------------//
mode get_mode() const { return mode_; }
//-----------------------------------------------------------------//
/*!
@brief OpenGL ビューポートを取り出す。
@param[in] x X 軸の位置
@param[in] y Y 軸の位置
@param[in] w X 軸の幅
@param[in] h Y 軸の高さ
*/
//-----------------------------------------------------------------//
void get_viewport(int& x, int& y, int& w, int& h) const {
x = vp_x_;
y = vp_y_;
w = vp_w_;
h = vp_h_;
}
//-----------------------------------------------------------------//
/*!
@brief OpenGL ビューポートの設定
@param[in] x X 軸の位置
@param[in] y Y 軸の位置
@param[in] w X 軸の幅
@param[in] h Y 軸の高さ
*/
//-----------------------------------------------------------------//
void set_viewport(int x, int y, int w, int h) {
vp_x_ = x;
vp_y_ = y;
vp_w_ = w;
vp_h_ = h;
}
//-----------------------------------------------------------------//
/*!
@brief OpenGL カレント・マトリックスに単位行列をセット
*/
//-----------------------------------------------------------------//
void identity() noexcept { acc_[static_cast<int>(mode_)].identity(); }
//-----------------------------------------------------------------//
/*!
@brief カレント・マトリックスにロード
@param[in] m マトリックスのポインター先頭(fmat4)
*/
//-----------------------------------------------------------------//
void load(const value_type& m) { acc_[static_cast<int>(mode_)] = m; }
//-----------------------------------------------------------------//
/*!
@brief カレント・マトリックスにロード
@param[in] m マトリックスのポインター先頭(float)
*/
//-----------------------------------------------------------------//
void load(const T* m) { acc_[static_cast<int>(mode_)] = m; }
//-----------------------------------------------------------------//
/*!
@brief カレント・マトリックスにロード
@param[in] m マトリックスのポインター先頭(double)
*/
//-----------------------------------------------------------------//
void load(const double* m) { acc_[static_cast<int>(mode_)] = m; }
//-----------------------------------------------------------------//
/*!
@brief OpenGL 4 X 4 行列をカレント・マトリックスと積算
@param[in] m 4 X 4 マトリックス
*/
//-----------------------------------------------------------------//
void mult(const mtx::matrix4<T>& m) {
mtx::matmul4<T>(acc_[static_cast<int>(mode_)].m, acc_[static_cast<int>(mode_)].m, m.m);
}
//-----------------------------------------------------------------//
/*!
@brief OpenGL 4 X 4 行列をカレント・マトリックスと積算
@param[in] m マトリックス列(float)
*/
//-----------------------------------------------------------------//
void mult(const float* m) {
mtx::matrix4<T> tm = m;
mtx::matmul4<T>(acc_[static_cast<int>(mode_)].m, acc_[static_cast<int>(mode_)].m, tm.m);
}
//-----------------------------------------------------------------//
/*!
@brief OpenGL 4 X 4 行列をカレント・マトリックスと積算
@param[in] m マトリックス列(double)
*/
//-----------------------------------------------------------------//
void mult(const double* m) {
mtx::matrix4<T> tm = m;
mtx::matmul4<T>(acc_[static_cast<int>(mode_)].m, acc_[static_cast<int>(mode_)].m, tm.m);
}
//-----------------------------------------------------------------//
/*!
@brief OpenGL カレント・マトリックスをスタックに退避
*/
//-----------------------------------------------------------------//
void push() { stack_[static_cast<int>(mode_)].push(acc_[static_cast<int>(mode_)]); }
//-----------------------------------------------------------------//
/*!
@brief OpenGL カレント・マトリックスをスタックから復帰
*/
//-----------------------------------------------------------------//
void pop() { stack_[static_cast<int>(mode_)].pop(); acc_[static_cast<int>(mode_)] = stack_[static_cast<int>(mode_)].top(); }
//-----------------------------------------------------------------//
/*!
@brief OpenGL 視体積行列をカレント・マトリックスに合成する
@param[in] left クリップ平面上の位置(左)
@param[in] right クリップ平面上の位置(右)
@param[in] bottom クリップ平面上の位置(下)
@param[in] top クリップ平面上の位置(上)
@param[in] nearval クリップ平面上の位置(手前)
@param[in] farval クリップ平面上の位置(奥)
*/
//-----------------------------------------------------------------//
void frustum(T left, T right, T bottom, T top, T nearval, T farval) {
near_ = nearval;
far_ = farval;
acc_[static_cast<int>(mode_)].frustum(left, right, bottom, top, nearval, farval);
}
//-----------------------------------------------------------------//
/*!
@brief OpenGL 正射影行列をカレント・マトリックスに合成する
@param[in] left クリップ平面上の位置(左)
@param[in] right クリップ平面上の位置(右)
@param[in] bottom クリップ平面上の位置(下)
@param[in] top クリップ平面上の位置(上)
@param[in] nearval クリップ平面上の位置(手前)
@param[in] farval クリップ平面上の位置(奥)
*/
//-----------------------------------------------------------------//
void ortho(T left, T right, T bottom, T top, T nearval, T farval) {
near_ = nearval;
far_ = farval;
acc_[static_cast<int>(mode_)].ortho(left, right, bottom, top, nearval, farval);
}
//-----------------------------------------------------------------//
/*!
@brief OpenGL/GLU gluPerspective と同等な行列をカレント・マトリックスに合成する
@param[in] fovy 視野角度
@param[in] aspect アスペクト比
@param[in] nearval クリップ平面上の位置(手前)
@param[in] farval クリップ平面上の位置(奥)
*/
//-----------------------------------------------------------------//
void perspective(T fovy, T aspect, T nearval, T farval) {
near_ = nearval;
far_ = farval;
acc_[static_cast<int>(mode_)].perspective(fovy, aspect, nearval, farval);
}
//-----------------------------------------------------------------//
/*!
@brief OpenGL/GLU gluLookAt と同等な行列をカレント・マトリックスに合成する
@param[in] eye カメラの位置
@param[in] center 視線方向
@param[in] up カメラの上向き方向ベクトル
*/
//-----------------------------------------------------------------//
void look_at(const vtx::vertex3<T>& eye, const vtx::vertex3<T>& center, const vtx::vertex3<T>& up) {
acc_[static_cast<int>(mode_)].look_at(eye, center, up);
}
//-----------------------------------------------------------------//
/*!
@brief OpenGL スケール
@param[in] v スケール
*/
//-----------------------------------------------------------------//
void scale(const vtx::vertex3<T>& v) { acc_[static_cast<int>(mode_)].scale(v); }
//-----------------------------------------------------------------//
/*!
@brief OpenGL スケール
@param[in] x X スケール
@param[in] y Y スケール
@param[in] z Z スケール
*/
//-----------------------------------------------------------------//
void scale(T x, T y, T z) { acc_[static_cast<int>(mode_)].scale(vtx::vertex3<T>(x, y, z)); }
//-----------------------------------------------------------------//
/*!
@brief OpenGL 移動行列をカレント・マトリックスに合成する
@param[in] x X 軸移動量
@param[in] y Y 軸移動量
@param[in] z Z 軸移動量
*/
//-----------------------------------------------------------------//
void translate(T x, T y, T z) {
acc_[static_cast<int>(mode_)].translate(vtx::vertex3<T>(x, y, z));
}
//-----------------------------------------------------------------//
/*!
@brief OpenGL 回転行列をカレント・マトリックスに合成する
@param[in] angle 0 〜 360 度の(DEG)角度
@param[in] x 回転中心の X 要素
@param[in] y 回転中心の Y 要素
@param[in] z 回転中心の Z 要素
*/
//-----------------------------------------------------------------//
void rotate(T angle, T x, T y, T z) {
acc_[static_cast<int>(mode_)].rotate(angle, vtx::vertex3<T>(x, y, z));
};
//-----------------------------------------------------------------//
/*!
@brief カレント・マトリックスを参照
@return OpenGL 並びの、ベースマトリックス
*/
//-----------------------------------------------------------------//
matrix_type& at_current_matrix() { return acc_[static_cast<int>(mode_)]; };
//-----------------------------------------------------------------//
/*!
@brief カレント・マトリックスを得る
@return OpenGL 並びの、ベースマトリックス
*/
//-----------------------------------------------------------------//
const matrix_type& get_current_matrix() const { return acc_[static_cast<int>(mode_)]; };
//-----------------------------------------------------------------//
/*!
@brief プロジェクション・マトリックスを得る
@return OpenGL 並びの、ベースマトリックス
*/
//-----------------------------------------------------------------//
const matrix_type& get_projection_matrix() const {
return acc_[mode::projection];
};
//-----------------------------------------------------------------//
/*!
@brief モデル・マトリックスを得る
@return OpenGL 並びの、ベースマトリックス
*/
//-----------------------------------------------------------------//
const matrix_type& get_modelview_matrix() const {
return acc_[mode::modelview];
};
//-----------------------------------------------------------------//
/*!
@brief ワールド・マトリックス(最終)を計算する
@return OpenGL 並びの、ワールド・マトリックス
*/
//-----------------------------------------------------------------//
void world_matrix(matrix_type& mat) const noexcept
{
const auto& pm = acc_[static_cast<int>(mode::projection)];
const auto& mm = acc_[static_cast<int>(mode::modelview)];
mtx::matmul4(mat(), pm(), mm());
}
//-----------------------------------------------------------------//
/*!
@brief 頂点から変換された座標を得る
@param[in] mat ベース・マトリックス
@param[in] inv 頂点
@param[out] out 変換された座標
@param[out] scr スクリーン座標
*/
//-----------------------------------------------------------------//
void vertex(const matrix_type& mat, const vtx::vertex3<T>& inv, vtx::vertex3<T>& out, vtx::vertex3<T>& scr) const noexcept
{
vtx::vertex4<T> in = inv;
T o[4];
mtx::matmul1<T>(o, mat(), in.getXYZW());
out.set(o[0], o[1], o[2]);
T invw = static_cast<T>(1) / o[3];
T w = (far_ * near_) / (far_ - near_) * invw;
scr.set(out.x * invw, out.y * invw, w);
}
//-----------------------------------------------------------------//
/*!
@brief 頂点から変換されたワールド座標を得る
@param[in] mat ベース・マトリックス
@param[in] inv 頂点
@param[out] out 結果を受け取るベクター
*/
//-----------------------------------------------------------------//
static void vertex_world(const matrix_type& mat, const vtx::vertex3<T>& inv, vtx::vertex4<T>& out) {
vtx::vertex4<T> in = inv;
T o[4];
mtx::matmul1<T>(o, mat(), in.getXYZW());
out.set(o[0], o[1], o[2], o[3]);
}
//-----------------------------------------------------------------//
/*!
@brief 頂点から正規化されたスクリーン座標を得る
@param[in] mat ベース・マトリックス
@param[in] inv 頂点
@param[out] outv 結果を受け取るベクター
*/
//-----------------------------------------------------------------//
void vertex_screen(const mtx::matrix4<T>& mat, const vtx::vertex3<T>& inv, vtx::vertex3<T>& outv) const noexcept
{
T out[4];
vtx::vertex4<T> in = inv;
mtx::matmul1<T>(out, mat(), in.getXYZW());
T invw = 1.0f / out[3];
T w = (far_ * near_) / (far_ - near_) * invw;
outv.set(out[0] * invw, out[1] * invw, w);
}
//-----------------------------------------------------------------//
/*!
@brief マウス座標を正規化する
@param[in] mspos マウス位置(左上が0,0)
@param[in] rpos 正規化された位置
*/
//-----------------------------------------------------------------//
void regularization_mouse_position(const vtx::spos& mspos, vtx::vertex2<T>& rpos) const {
T fw = static_cast<T>(vp_w_) / static_cast<T>(2);
T fh = static_cast<T>(vp_h_) / static_cast<T>(2);
rpos.set((static_cast<float>(mspos.x) - fw) / fw, (fh - static_cast<float>(mspos.y)) / fh);
}
//-----------------------------------------------------------------//
/*!
@brief カレント・マトリックスの表示
*/
//-----------------------------------------------------------------//
void print_matrix() {
for(int i = 0; i < 4; ++i) {
utils::format("(%d) %-6.5f, %-6.5f, %-6.5f, %-6.5f\n")
% i
% acc_[static_cast<int>(mode_)].m[0 * 4 + i]
% acc_[static_cast<int>(mode_)].m[1 * 4 + i]
% acc_[static_cast<int>(mode_)].m[2 * 4 + i]
% acc_[static_cast<int>(mode_)].m[3 * 4 + i];
}
}
};
typedef matrix<float> matrixf;
typedef matrix<double> matrixd;
}