-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.h
1485 lines (1215 loc) · 50.9 KB
/
Window.h
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
/*
** ゲームグラフィックス特論の宿題用補助プログラム GLFW3 版
**
Copyright (c) 2011-2020 Kohe Tokoi. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
KOHE TOKOI BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
*/
// 補助プログラム
#include "gg.h"
using namespace gg;
// Oculus Rift を使うなら
//#define USE_OCULUS_RIFT
// Oculus Rift SDK ライブラリ (LibOVR) の組み込み
#ifdef USE_OCULUS_RIFT
# if defined(_MSC_VER)
# define GLFW_EXPOSE_NATIVE_WIN32
# define GLFW_EXPOSE_NATIVE_WGL
# include <GLFW/glfw3native.h>
# define OVR_OS_WIN32
# undef APIENTRY
# pragma comment(lib, "LibOVR.lib")
# endif
# include <OVR_CAPI_GL.h>
# include <Extras/OVR_Math.h>
# if OVR_PRODUCT_VERSION > 0
# include <dxgi.h> // GetDefaultAdapterLuid のため
# pragma comment(lib, "dxgi.lib")
# endif
#endif
// Dear ImGui を使うなら
//#define USE_IMGUI
// ImGui の組み込み
#ifdef USE_IMGUI
# include "imgui.h"
# include "imgui_impl_glfw.h"
# include "imgui_impl_opengl3.h"
#endif
// 標準ライブラリ
#include <cmath>
#include <cstdlib>
#include <stdexcept>
#include <iostream>
/*!
** \brief ウィンドウ関連の処理.
**
** GLFW を使って OpenGL のウィンドウを操作するラッパークラス.
*/
class Window
{
// ウィンドウの識別子
GLFWwindow *window;
// ビューポートの横幅と高さ
GLsizei size[2];
// ビューポートのアスペクト比
GLfloat aspect;
// 矢印キー
int arrow[4][2];
// マウスの現在位置
GLfloat mouse_position[2];
// マウスホイールの回転量
GLfloat wheel_rotation[2];
// 平行移動量量[ボタン][直前/更新][X/Y/Z]
GLfloat translation[3][2][3];
// トラックボール
GgTrackball trackball[3];
#ifdef USE_OCULUS_RIFT
//
// Oculus Rift
//
// Oculus Rift のセッション
ovrSession session;
// Oculus Rift の状態
ovrHmdDesc hmdDesc;
// Oculus Rift のスクリーンのサイズ
GLfloat screen[ovrEye_Count][4];
// Oculus Rift 表示用の FBO
GLuint oculusFbo[ovrEye_Count];
// ミラー表示用の FBO
GLuint mirrorFbo;
# if OVR_PRODUCT_VERSION > 0
// Oculus Rift に送る描画データ
ovrLayerEyeFov layerData;
// Oculus Rift にレンダリングするフレームの番号
long long frameIndex;
// Oculus Rift 表示用の FBO のデプステクスチャ
GLuint oculusDepth[ovrEye_Count];
// ミラー表示用の FBO のサイズ
int mirrorWidth, mirrorHeight;
// ミラー表示用の FBO のカラーテクスチャ
ovrMirrorTexture mirrorTexture;
// グラフィックスカードのデフォルトの LUID を得る
inline ovrGraphicsLuid GetDefaultAdapterLuid()
{
ovrGraphicsLuid luid = ovrGraphicsLuid();
# if defined(_MSC_VER)
IDXGIFactory *factory(nullptr);
if (SUCCEEDED(CreateDXGIFactory(IID_PPV_ARGS(&factory))))
{
IDXGIAdapter *adapter(nullptr);
if (SUCCEEDED(factory->EnumAdapters(0, &adapter)))
{
DXGI_ADAPTER_DESC desc;
adapter->GetDesc(&desc);
memcpy(&luid, &desc.AdapterLuid, sizeof luid);
adapter->Release();
}
factory->Release();
}
# endif
return luid;
}
// グラフィックスカードの LUID の比較
inline int Compare(const ovrGraphicsLuid &lhs, const ovrGraphicsLuid &rhs)
{
return memcmp(&lhs, &rhs, sizeof(ovrGraphicsLuid));
}
# else
// Oculus Rift に送る描画データ
ovrLayer_Union layerData;
// Oculus Rift のレンダリング情報
ovrEyeRenderDesc eyeRenderDesc[ovrEye_Count];
// Oculus Rift の視点情報
ovrPosef eyePose[ovrEye_Count];
// ミラー表示用の FBO のカラーテクスチャ
ovrGLTexture *mirrorTexture;
# endif
#endif
//
// ユーザー定義のコールバック関数へのポインタ
//
void *userPointer;
void (*resizeFunc)(const Window *window, int width, int height);
void (*keyboardFunc)(const Window *window, int key, int scancode, int action, int mods);
void (*mouseFunc)(const Window *window, int button, int action, int mods);
void (*wheelFunc)(const Window *window, double x, double y);
//
// ウィンドウのサイズ変更時の処理
//
static void resize(GLFWwindow *window, int width, int height)
{
// このインスタンスの this ポインタを得る
Window *const instance(static_cast<Window *>(glfwGetWindowUserPointer(window)));
if (instance)
{
// ウィンドウのサイズを保存する
instance->size[0] = width;
instance->size[1] = height;
// トラックボール処理の範囲を設定する
instance->trackball[GLFW_MOUSE_BUTTON_1].region(width, height);
instance->trackball[GLFW_MOUSE_BUTTON_2].region(width, height);
instance->trackball[GLFW_MOUSE_BUTTON_3].region(width, height);
#ifndef USE_OCULUS_RIFT
// ウィンドウのアスペクト比を保存する
instance->aspect = static_cast<GLfloat>(width) / static_cast<GLfloat>(height);
// ウィンドウ全体に描画する
glViewport(0, 0, width, height);
#endif
// ユーザー定義のコールバック関数の呼び出し
if (instance->resizeFunc) (*instance->resizeFunc)(instance, width, height);
}
}
//
// キーボードをタイプした時の処理
//
static void keyboard(GLFWwindow *window, int key, int scancode, int action, int mods)
{
#ifdef USE_IMGUI
// ImGui のウィンドウが選択されていたらキーボードの処理を行わない
if (ImGui::IsAnyWindowFocused()) return;
#endif
// このインスタンスの this ポインタを得る
Window *const instance(static_cast<Window *>(glfwGetWindowUserPointer(window)));
if (instance && action)
{
// ユーザー定義のコールバック関数の呼び出し
if (instance->keyboardFunc) (*instance->keyboardFunc)(instance, key, scancode, action, mods);
switch (key)
{
case GLFW_KEY_HOME:
// トラックボールをリセットする
instance->trackball[GLFW_MOUSE_BUTTON_1].reset();
instance->trackball[GLFW_MOUSE_BUTTON_2].reset();
instance->trackball[GLFW_MOUSE_BUTTON_3].reset();
break;
case GLFW_KEY_END:
// 矢印キーの設定値とマウスホイールの回転量をリセットする
for (auto a : instance->arrow) a[0] = a[1] = 0;
std::fill(*(*instance->translation), *(*(instance->translation + 3)), 0.0f);
instance->wheel_rotation[0] = instance->wheel_rotation[1] = 0.0f;
break;
case GLFW_KEY_UP:
if (mods & GLFW_MOD_SHIFT)
instance->arrow[1][1]++;
else if (mods & GLFW_MOD_CONTROL)
instance->arrow[2][1]++;
else if (mods & GLFW_MOD_ALT)
instance->arrow[3][1]++;
else
instance->arrow[0][1]++;
break;
case GLFW_KEY_DOWN:
if (mods & GLFW_MOD_SHIFT)
instance->arrow[1][1]--;
else if (mods & GLFW_MOD_CONTROL)
instance->arrow[2][1]--;
else if (mods & GLFW_MOD_ALT)
instance->arrow[3][1]--;
else
instance->arrow[0][1]--;
break;
case GLFW_KEY_RIGHT:
if (mods & GLFW_MOD_SHIFT)
instance->arrow[1][0]++;
else if (mods & GLFW_MOD_CONTROL)
instance->arrow[2][0]++;
else if (mods & GLFW_MOD_ALT)
instance->arrow[3][0]++;
else
instance->arrow[0][0]++;
break;
case GLFW_KEY_LEFT:
if (mods & GLFW_MOD_SHIFT)
instance->arrow[1][0]--;
else if (mods & GLFW_MOD_CONTROL)
instance->arrow[2][0]--;
else if (mods & GLFW_MOD_ALT)
instance->arrow[3][0]--;
else
instance->arrow[0][0]--;
break;
default:
break;
}
}
}
//
// マウスボタンを操作したときの処理
//
static void mouse(GLFWwindow *window, int button, int action, int mods)
{
#ifdef USE_IMGUI
// マウスカーソルが ImGui のウィンドウ上にあったら Window クラスのマウス位置を更新しない
if (ImGui::IsAnyWindowHovered()) return;
#endif
// このインスタンスの this ポインタを得る
Window *const instance(static_cast<Window *>(glfwGetWindowUserPointer(window)));
if (instance)
{
// ユーザー定義のコールバック関数の呼び出し
if (instance->mouseFunc) (*instance->mouseFunc)(instance, button, action, mods);
// マウスの現在位置を得る
const GLfloat x(instance->mouse_position[0]);
const GLfloat y(instance->mouse_position[1]);
switch (button)
{
case GLFW_MOUSE_BUTTON_1:
case GLFW_MOUSE_BUTTON_2:
case GLFW_MOUSE_BUTTON_3:
if (action)
{
// 左ドラッグ開始
instance->trackball[button].begin(x, y);
}
else
{
// 左ドラッグ終了
instance->translation[button][0][0] = instance->translation[button][1][0];
instance->translation[button][0][1] = instance->translation[button][1][1];
instance->translation[button][0][2] = instance->translation[button][1][2];
instance->trackball[button].end(x, y);
}
break;
default:
break;
}
}
}
//
// マウスホイールを操作した時の処理
//
static void wheel(GLFWwindow *window, double x, double y)
{
#ifdef USE_IMGUI
// マウスカーソルが ImGui のウィンドウ上にあったら Window クラスのマウスホイールの回転量を更新しない
if (ImGui::IsAnyWindowHovered()) return;
#endif
// このインスタンスの this ポインタを得る
Window *const instance(static_cast<Window *>(glfwGetWindowUserPointer(window)));
if (instance)
{
// ユーザー定義のコールバック関数の呼び出し
if (instance->wheelFunc) (*instance->wheelFunc)(instance, x, y);
// マウスホイールの回転量の保存
instance->wheel_rotation[0] += static_cast<GLfloat>(x);
instance->wheel_rotation[1] += static_cast<GLfloat>(y);
// マウスによる平行移動量の z 値の更新
const GLfloat z(instance->getWheelY() * 0.05f);
instance->translation[GLFW_MOUSE_BUTTON_1][1][2] = z;
instance->translation[GLFW_MOUSE_BUTTON_2][1][2] = z;
instance->translation[GLFW_MOUSE_BUTTON_3][1][2] = z;
}
}
//
// トラックボール処理を考慮した平行移動量を計算する (X, Y のみ, Z は wheel() で計算する)
//
void calcTranslation(GLfloat *t, int button) const
{
const GLfloat d(fabs(translation[button][0][2]) + 1.0f);
t[0] = (mouse_position[0] - trackball[button].getStart(0)) * trackball[button].getScale(0) * d + translation[button][0][0];
t[1] = (trackball[button].getStart(1) - mouse_position[1]) * trackball[button].getScale(1) * d + translation[button][0][1];
}
#ifdef USE_OCULUS_RIFT
//
// Oculus Rift の使用終了
//
void terminateLibOVR()
{
// ミラー表示用の FBO を削除する
if (mirrorFbo) glDeleteFramebuffers(1, &mirrorFbo);
// ミラー表示に使ったテクスチャを開放する
if (mirrorTexture)
{
# if OVR_PRODUCT_VERSION > 0
ovr_DestroyMirrorTexture(session, mirrorTexture);
# else
glDeleteTextures(1, &mirrorTexture->OGL.TexId);
ovr_DestroyMirrorTexture(session, reinterpret_cast<ovrTexture *>(mirrorTexture));
# endif
}
// Oculus Rift のレンダリング用の FBO を削除する
glDeleteFramebuffers(ovrEye_Count, oculusFbo);
// Oculus Rift 表示用の FBO を削除する
for (int eye = 0; eye < ovrEye_Count; ++eye)
{
# if OVR_PRODUCT_VERSION > 0
// レンダリングターゲットに使ったテクスチャを開放する
if (layerData.ColorTexture[eye])
{
ovr_DestroyTextureSwapChain(session, layerData.ColorTexture[eye]);
layerData.ColorTexture[eye] = nullptr;
}
// デプスバッファとして使ったテクスチャを開放する
glDeleteTextures(1, &oculusDepth[eye]);
oculusDepth[eye] = 0;
# else
// レンダリングターゲットに使ったテクスチャを開放する
auto *const colorTexture(layerData.EyeFov.ColorTexture[eye]);
for (int i = 0; i < colorTexture->TextureCount; ++i)
{
const auto *const ctex(reinterpret_cast<ovrGLTexture *>(&colorTexture->Textures[i]));
glDeleteTextures(1, &ctex->OGL.TexId);
}
ovr_DestroySwapTextureSet(session, colorTexture);
// デプスバッファとして使ったテクスチャを開放する
auto *const depthTexture(layerData.EyeFovDepth.DepthTexture[eye]);
for (int i = 0; i < depthTexture->TextureCount; ++i)
{
const auto *const dtex(reinterpret_cast<ovrGLTexture *>(&depthTexture->Textures[i]));
glDeleteTextures(1, &dtex->OGL.TexId);
}
ovr_DestroySwapTextureSet(session, depthTexture);
# endif
}
// Oculus Rift のセッションを破棄する
ovr_Destroy(session);
session = nullptr;
// LibOVR を終了する
ovr_Shutdown();
}
#endif
//
// GLFW のエラー表示
//
static void glfwErrorCallback(int error, const char *description)
{
throw std::runtime_error(description);
}
public:
//! \brief 初期化, 最初に一度だけ実行する.
//! \param major 使用する OpenGL の major 番号, 0 なら無指定.
//! \param minor 使用する OpenGL の minor 番号, major 番号が 0 なら無視.
static void init(int major = 0, int minor = 1)
{
// 最初に実行するときだけ true
static bool firstTime(true);
// 既に実行されていたら何もしない
if (!firstTime) return;
// 初期化済みの印をつける
firstTime = false;
// GLFW を初期化する
glfwSetErrorCallback(glfwErrorCallback);
if (glfwInit() == GL_FALSE) throw std::runtime_error("Can't initialize GLFW");
// 後始末を登録する
atexit(glfwTerminate);
// OpenGL の major 番号が指定されていれば
if (major > 0)
{
// OpenGL のバージョンを指定する
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
if (major * 10 + minor >= 32)
{
// Core Profile を選択する
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
}
#ifdef USE_OCULUS_RIFT
// Oculus Rift (LibOVR) を初期化する
ovrInitParams initParams = { ovrInit_RequestVersion, OVR_MINOR_VERSION, NULL, 0, 0 };
if (OVR_FAILURE(ovr_Initialize(&initParams))) throw std::runtime_error("Can't initialize LibOVR");
// プログラム終了時に LibOVR を終了する
atexit(terminateLibOVR);
// Oculus Rift のセッションを作成する
ovrGraphicsLuid luid;
session = nullptr;
if (OVR_FAILURE(ovr_Create(&session, &luid))) throw std::runtime_error("Can't create Oculus Rift session");
// Oculus Rift へのレンダリングに使う FBO の初期値を設定する
for (int eye = 0; eye < ovrEye_Count; ++eye) oculusFbo[eye] = 0;
// ミラー表示に使う FBO の初期値を設定する
mirrorFbo = 0;
mirrorTexture = nullptr;
# if OVR_PRODUCT_VERSION > 0
// デフォルトのグラフィックスアダプタが使われているか確かめる
if (Compare(luid, GetDefaultAdapterLuid())) throw std::runtime_error("Graphics adapter is not default");
// Asynchronous TimeWarp 処理に使うフレーム番号の初期値を設定する
frameIndex = 0LL;
// Oculus Rift へのレンダリングに使う FBO のデプステクスチャの初期値を設定する
for (int eye = 0; eye < ovrEye_Count; ++eye) oculusDepth[eye] = 0;
# endif
// Oculus Rift ではダブルバッファリングしない
glfwWindowHint(GLFW_DOUBLEBUFFER, GL_FALSE);
// Oculus Rift では SRGB でレンダリングする
glfwWindowHint(GLFW_SRGB_CAPABLE, GL_TRUE);
#endif
#ifdef USE_IMGUI
// ImGui のバージョンをチェックする
IMGUI_CHECKVERSION();
// ImGui のコンテキストを作成する
ImGui::CreateContext();
// プログラム終了時には ImGui のコンテキストを破棄する
atexit([] { ImGui::DestroyContext(); });
#endif
}
//! \brief コンストラクタ.
//! \param title ウィンドウタイトルの文字列.
//! \param width 開くウィンドウの幅.
//! \param height 開くウィンドウの高さ.
//! \param fullscreen フルスクリーン表示を行うディスプレイ番号, 0 ならフルスクリーン表示を行わない.
//! \param share 共有するコンテキスト, nullptr ならコンテキストを共有しない.
Window(const char *title = "GLFW Window", int width = 640, int height = 480,
int fullscreen = 0, GLFWwindow *share = nullptr)
: window(nullptr), size{ width, height }, aspect(1.0f)
, arrow{ { 0 }, { 0 }, { 0 }, { 0 } }, mouse_position{ 0.0f }, wheel_rotation{ 0.0f }
, translation{ { { 0.0f }, { 0.0f } }, { { 0.0f }, { 0.0f } }, { { 0.0f }, { 0.0f } } }
, userPointer(nullptr), resizeFunc(nullptr), keyboardFunc(nullptr), mouseFunc(nullptr), wheelFunc(nullptr)
{
// ディスプレイの情報
GLFWmonitor *monitor(nullptr);
// フルスクリーン表示
if (fullscreen > 0)
{
// 接続されているモニタの数を数える
int mcount;
GLFWmonitor **const monitors(glfwGetMonitors(&mcount));
// セカンダリモニタがあればそれを使う
if (fullscreen > mcount) fullscreen = mcount;
monitor = monitors[fullscreen - 1];
// モニタのモードを調べる
const GLFWvidmode *mode(glfwGetVideoMode(monitor));
// ウィンドウのサイズをディスプレイのサイズにする
width = mode->width;
height = mode->height;
}
// GLFW のウィンドウを作成する
window = glfwCreateWindow(width, height, title, monitor, share);
// ウィンドウが作成できなければエラー
if (!window) throw std::runtime_error("Unable to open the GLFW window.");
// 現在のウィンドウを処理対象にする
glfwMakeContextCurrent(window);
// ゲームグラフィックス特論の都合による初期化を行う
ggInit();
// このインスタンスの this ポインタを記録しておく
glfwSetWindowUserPointer(window, this);
// キーボードを操作した時の処理を登録する
glfwSetKeyCallback(window, keyboard);
// マウスボタンを操作したときの処理を登録する
glfwSetMouseButtonCallback(window, mouse);
// マウスホイール操作時に呼び出す処理を登録する
glfwSetScrollCallback(window, wheel);
// ウィンドウのサイズ変更時に呼び出す処理を登録する
glfwSetFramebufferSizeCallback(window, resize);
#ifdef USE_OCULUS_RIFT
// Oculus Rift の情報を取り出す
hmdDesc = ovr_GetHmdDesc(session);
# if defined(_DEBUG)
// Oculus Rift の情報を表示する
std::cerr
<< "\nProduct name: " << hmdDesc.ProductName
<< "\nResolution: " << hmdDesc.Resolution.w << " x " << hmdDesc.Resolution.h
<< "\nDefault Fov: (" << hmdDesc.DefaultEyeFov[ovrEye_Left].LeftTan
<< "," << hmdDesc.DefaultEyeFov[ovrEye_Left].DownTan
<< ") - (" << hmdDesc.DefaultEyeFov[ovrEye_Left].RightTan
<< "," << hmdDesc.DefaultEyeFov[ovrEye_Left].UpTan
<< ")\n (" << hmdDesc.DefaultEyeFov[ovrEye_Right].LeftTan
<< "," << hmdDesc.DefaultEyeFov[ovrEye_Right].DownTan
<< ") - (" << hmdDesc.DefaultEyeFov[ovrEye_Right].RightTan
<< "," << hmdDesc.DefaultEyeFov[ovrEye_Right].UpTan
<< ")\nMaximum Fov: (" << hmdDesc.MaxEyeFov[ovrEye_Left].LeftTan
<< "," << hmdDesc.MaxEyeFov[ovrEye_Left].DownTan
<< ") - (" << hmdDesc.MaxEyeFov[ovrEye_Left].RightTan
<< "," << hmdDesc.MaxEyeFov[ovrEye_Left].UpTan
<< ")\n (" << hmdDesc.MaxEyeFov[ovrEye_Right].LeftTan
<< "," << hmdDesc.MaxEyeFov[ovrEye_Right].DownTan
<< ") - (" << hmdDesc.MaxEyeFov[ovrEye_Right].RightTan
<< "," << hmdDesc.MaxEyeFov[ovrEye_Right].UpTan
<< ")\n" << std::endl;
# endif
// Oculus Rift に転送する描画データを作成する
# if OVR_PRODUCT_VERSION > 0
layerData.Header.Type = ovrLayerType_EyeFov;
# else
layerData.Header.Type = ovrLayerType_EyeFovDepth;
# endif
layerData.Header.Flags = ovrLayerFlag_TextureOriginAtBottomLeft; // OpenGL なので左下が原点
// Oculus Rift 表示用の FBO を作成する
for (int eye = 0; eye < ovrEye_Count; ++eye)
{
// Oculus Rift の視野を取得する
const auto &fov(hmdDesc.DefaultEyeFov[ovrEyeType(eye)]);
// Oculus Rift 表示用の FBO のサイズを求める
const auto textureSize(ovr_GetFovTextureSize(session, ovrEyeType(eye), fov, 1.0f));
// Oculus Rift 表示用の FBO のアスペクト比を求める
aspect = static_cast<GLfloat>(textureSize.w) / static_cast<GLfloat>(textureSize.h);
// Oculus Rift のスクリーンのサイズを保存する
screen[eye][0] = -fov.LeftTan;
screen[eye][1] = fov.RightTan;
screen[eye][2] = -fov.DownTan;
screen[eye][3] = fov.UpTan;
# if OVR_PRODUCT_VERSION > 0
// 描画データに視野を設定する
layerData.Fov[eye] = fov;
// 描画データにビューポートを設定する
layerData.Viewport[eye].Pos = OVR::Vector2i(0, 0);
layerData.Viewport[eye].Size = textureSize;
// Oculus Rift 表示用の FBO のカラーバッファとして使うテクスチャセットの特性
const ovrTextureSwapChainDesc colorDesc =
{
ovrTexture_2D, // Type
OVR_FORMAT_R8G8B8A8_UNORM_SRGB, // Format
1, // ArraySize
textureSize.w, // Width
textureSize.h, // Height
1, // MipLevels
1, // SampleCount
ovrFalse, // StaticImage
0, 0
};
// Oculus Rift 表示用の FBO のレンダーターゲットとして使うテクスチャチェインを作成する
layerData.ColorTexture[eye] = nullptr;
if (OVR_SUCCESS(ovr_CreateTextureSwapChainGL(session, &colorDesc, &layerData.ColorTexture[eye])))
{
// 作成したテクスチャチェインの長さを取得する
int length(0);
if (OVR_SUCCESS(ovr_GetTextureSwapChainLength(session, layerData.ColorTexture[eye], &length)))
{
// テクスチャチェインの個々の要素について
for (int i = 0; i < length; ++i)
{
// テクスチャのパラメータを設定する
GLuint texId;
ovr_GetTextureSwapChainBufferGL(session, layerData.ColorTexture[eye], i, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
}
// Oculus Rift 表示用の FBO のデプスバッファとして使うテクスチャを作成する
glGenTextures(1, &oculusDepth[eye]);
glBindTexture(GL_TEXTURE_2D, oculusDepth[eye]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, textureSize.w, textureSize.h, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
# else
// 描画データに視野を設定する
layerData.EyeFov.Fov[eye] = fov;
// 描画データにビューポートを設定する
layerData.EyeFov.Viewport[eye].Pos = OVR::Vector2i(0, 0);
layerData.EyeFov.Viewport[eye].Size = textureSize;
// Oculus Rift 表示用の FBO のカラーバッファとして使うテクスチャセットを作成する
ovrSwapTextureSet *colorTexture;
ovr_CreateSwapTextureSetGL(session, GL_SRGB8_ALPHA8, textureSize.w, textureSize.h, &colorTexture);
layerData.EyeFov.ColorTexture[eye] = colorTexture;
// Oculus Rift 表示用の FBO のデプスバッファとして使うテクスチャセットを作成する
ovrSwapTextureSet *depthTexture;
ovr_CreateSwapTextureSetGL(session, GL_DEPTH_COMPONENT32F, textureSize.w, textureSize.h, &depthTexture);
layerData.EyeFovDepth.DepthTexture[eye] = depthTexture;
// Oculus Rift のレンズ補正等の設定値を取得する
eyeRenderDesc[eye] = ovr_GetRenderDesc(session, ovrEyeType(eye), fov);
# endif
}
# if OVR_PRODUCT_VERSION > 0
// 姿勢のトラッキングにおける床の高さを 0 に設定する
ovr_SetTrackingOriginType(session, ovrTrackingOrigin_FloorLevel);
// ミラー表示用の FBO を作成する
const ovrMirrorTextureDesc mirrorDesc =
{
OVR_FORMAT_R8G8B8A8_UNORM_SRGB, // Format
mirrorWidth = width, // Width
mirrorHeight = height, // Height
0 // Flags
};
// ミラー表示用の FBO のカラーバッファとして使うテクスチャを作成する
if (OVR_SUCCESS(ovr_CreateMirrorTextureGL(session, &mirrorDesc, &mirrorTexture)))
{
// 作成したテクスチャのテクスチャ名を得る
GLuint texId;
if (OVR_SUCCESS(ovr_GetMirrorTextureBufferGL(session, mirrorTexture, &texId)))
{
// 作成したテクスチャをミラー表示用の FBO にカラーバッファとして組み込む
glGenFramebuffers(1, &mirrorFbo);
glBindFramebuffer(GL_READ_FRAMEBUFFER, mirrorFbo);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0);
glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
}
}
# else
// ミラー表示用の FBO を作成する
if (OVR_SUCCESS(ovr_CreateMirrorTextureGL(session, GL_SRGB8_ALPHA8, width, height, reinterpret_cast<ovrTexture **>(&mirrorTexture))))
{
glGenFramebuffers(1, &mirrorFbo);
glBindFramebuffer(GL_READ_FRAMEBUFFER, mirrorFbo);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mirrorTexture->OGL.TexId, 0);
glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
}
# endif
// Oculus Rift のレンダリング用の FBO を作成する
glGenFramebuffers(ovrEye_Count, oculusFbo);
// Oculus Rift にレンダリングするときは sRGB カラースペースを使う
glEnable(GL_FRAMEBUFFER_SRGB);
// Oculus Rift への表示では垂直同期タイミングに合わせない
glfwSwapInterval(0);
#else
// 垂直同期タイミングに合わせる
glfwSwapInterval(1);
#endif
// ビューポートと投影変換行列を初期化する
resize(window, width, height);
#ifdef USE_IMGUI
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(nullptr);
#endif
}
//! \brief コピーコンストラクタは使用禁止.
Window(const Window &w) = delete;
//! \brief 代入演算子は使用禁止.
Window &operator=(const Window &w) = delete;
//! \brief デストラクタ.
virtual ~Window()
{
// ウィンドウが作成されていなければ戻る
if (!window) return;
#ifdef USE_IMGUI
// Shutdown Platform/Renderer bindings
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
#endif
// ウィンドウを破棄する
glfwDestroyWindow(window);
}
#ifdef USE_OCULUS_RIFT
//! \brief Oculus Rift による描画開始.
//! \return 描画可能なら true.
bool begin()
{
# if OVR_PRODUCT_VERSION > 0
// セッションの状態を取得する
ovrSessionStatus sessionStatus;
ovr_GetSessionStatus(session, &sessionStatus);
// アプリケーションが終了を要求しているときはウィンドウのクローズフラグを立てる
if (sessionStatus.ShouldQuit) setClose(GLFW_TRUE);
// Oculus Rift に表示されていないときは戻る
if (!sessionStatus.IsVisible) return false;
// 現在の状態をトラッキングの原点にする
if (sessionStatus.ShouldRecenter) ovr_RecenterTrackingOrigin(session);
// HmdToEyeOffset などは実行時に変化するので毎フレーム ovr_GetRenderDesc() で ovrEyeRenderDesc を取得する
const ovrEyeRenderDesc eyeRenderDesc[] =
{
ovr_GetRenderDesc(session, ovrEyeType(0), hmdDesc.DefaultEyeFov[0]),
ovr_GetRenderDesc(session, ovrEyeType(1), hmdDesc.DefaultEyeFov[1])
};
// Oculus Rift のスクリーンのヘッドトラッキング位置からの変位を取得する
const ovrPosef hmdToEyePose[] =
{
eyeRenderDesc[0].HmdToEyePose,
eyeRenderDesc[1].HmdToEyePose
};
// 視点の姿勢情報を取得する
ovr_GetEyePoses(session, frameIndex, ovrTrue, hmdToEyePose, layerData.RenderPose, &layerData.SensorSampleTime);
# else
// フレームのタイミング計測開始
const auto ftiming(ovr_GetPredictedDisplayTime(session, 0));
// sensorSampleTime の取得は可能な限り ovr_GetTrackingState() の近くで行う
layerData.EyeFov.SensorSampleTime = ovr_GetTimeInSeconds();
// ヘッドトラッキングの状態を取得する
const auto hmdState(ovr_GetTrackingState(session, ftiming, ovrTrue));
// Oculus Rift のスクリーンのヘッドトラッキング位置からの変位を取得する
const ovrVector3f hmdToEyeViewOffset[] =
{
eyeRenderDesc[0].HmdToEyeViewOffset,
eyeRenderDesc[1].HmdToEyeViewOffset
};
// 視点の姿勢情報を求める
ovr_CalcEyePoses(hmdState.HeadPose.ThePose, hmdToEyeViewOffset, eyePose);
# endif
return true;
}
//! \brief Oculus Rift の描画する目の指定.
//! \param eye 表示する目.
//! \param screen HMD の視野の視錐台.
//! \param position HMD の位置.
//! \param orientation HMD の方法の四元数.
void select(int eye, GLfloat *screen, GLfloat *position, GLfloat *orientation)
{
# if OVR_PRODUCT_VERSION > 0
// Oculus Rift にレンダリングする FBO に切り替える
if (layerData.ColorTexture[eye])
{
// FBO のカラーバッファに使う現在のテクスチャのインデックスを取得する
int curIndex;
ovr_GetTextureSwapChainCurrentIndex(session, layerData.ColorTexture[eye], &curIndex);
// FBO のカラーバッファに使うテクスチャを取得する
GLuint curTexId;
ovr_GetTextureSwapChainBufferGL(session, layerData.ColorTexture[eye], curIndex, &curTexId);
// FBO を設定する
glBindFramebuffer(GL_FRAMEBUFFER, oculusFbo[eye]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, curTexId, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, oculusDepth[eye], 0);
// ビューポートを設定する
const auto &vp(layerData.Viewport[eye]);
glViewport(vp.Pos.x, vp.Pos.y, vp.Size.w, vp.Size.h);
}
// Oculus Rift の片目の位置と回転を取得する
const auto &p(layerData.RenderPose[eye].Position);
const auto &o(layerData.RenderPose[eye].Orientation);
# else
// レンダーターゲットに描画する前にレンダーターゲットのインデックスをインクリメントする
auto *const colorTexture(layerData.EyeFov.ColorTexture[eye]);
colorTexture->CurrentIndex = (colorTexture->CurrentIndex + 1) % colorTexture->TextureCount;
auto *const depthTexture(layerData.EyeFovDepth.DepthTexture[eye]);
depthTexture->CurrentIndex = (depthTexture->CurrentIndex + 1) % depthTexture->TextureCount;
// レンダーターゲットを切り替える
glBindFramebuffer(GL_FRAMEBUFFER, oculusFbo[eye]);
const auto &ctex(reinterpret_cast<ovrGLTexture *>(&colorTexture->Textures[colorTexture->CurrentIndex]));
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ctex->OGL.TexId, 0);
const auto &dtex(reinterpret_cast<ovrGLTexture *>(&depthTexture->Textures[depthTexture->CurrentIndex]));
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, dtex->OGL.TexId, 0);
// ビューポートを設定する
const auto &vp(layerData.EyeFov.Viewport[eye]);
glViewport(vp.Pos.x, vp.Pos.y, vp.Size.w, vp.Size.h);
// Oculus Rift の片目の位置と回転を取得する
const auto &p(eyePose[eye].Position);
const auto &o(eyePose[eye].Orientation);
# endif
// Oculus Rift のスクリーンの大きさを返す
screen[0] = this->screen[eye][0];
screen[1] = this->screen[eye][1];
screen[2] = this->screen[eye][2];
screen[3] = this->screen[eye][3];
// Oculus Rift の位置を返す
position[0] = p.x;
position[1] = p.y;
position[2] = p.z;
// Oculus Rift の方向を返す
orientation[0] = o.x;
orientation[1] = o.y;
orientation[2] = o.z;
orientation[3] = o.w;
}
//! \brief Time Warp 処理に使う投影変換行列の成分の設定 (DK1, DK2).
//! \param projection 投影変換行列.
void timewarp(const GgMatrix &projection)
{