-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGgApp.cpp
More file actions
1126 lines (926 loc) · 36 KB
/
GgApp.cpp
File metadata and controls
1126 lines (926 loc) · 36 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
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
///
/// ゲームグラフィックス特論宿題アプリケーションクラスの実装
///
/// @file
/// @author Kohe Tokoi
/// @date November 22, 2022
///
#include "GgApp.h"
//
// GLFW のエラー表示
//
static void glfwErrorCallback(int error, const char* description)
{
#if defined(__aarch64__)
if (error == 65544) return;
#endif
throw std::runtime_error(description);
}
//
// GgApp クラスのコンストラクタ
//
GgApp::GgApp(int major, int minor)
{
// GLFW のエラー処理関数を登録する
glfwSetErrorCallback(glfwErrorCallback);
// GLFW を初期化する
if (glfwInit() == GL_FALSE) throw std::runtime_error("Can't initialize GLFW");
// OpenGL の major 番号が指定されていれば
if (major > 0)
{
// OpenGL のバージョンを指定する
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
#if defined(GL_GLES_PROTOTYPES)
// OpenGL ES 3 のコンテキストを指定する
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
#else
// OpenGL Version 3.2 以降なら
if (major * 10 + minor >= 32)
{
// Core Profile を選択する (macOS の都合)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
#endif
}
#if defined(GG_USE_OCULUS_RIFT)
// Oculus Rift では SRGB でレンダリングする
glfwWindowHint(GLFW_SRGB_CAPABLE, GL_TRUE);
#endif
#if defined(IMGUI_VERSION)
// ImGui のバージョンをチェックする
IMGUI_CHECKVERSION();
// ImGui のコンテキストを作成する
ImGui::CreateContext();
#endif
}
//
// デストラクタ
//
GgApp::~GgApp()
{
#if defined(IMGUI_VERSION)
// Shutdown Platform/Renderer bindings
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
#endif
// プログラム終了時に GLFW を終了する
glfwTerminate();
}
//
// マウスや矢印キーによる平行移動量を初期化する
//
void GgApp::Window::HumanInterface::resetTranslation()
{
// 平行移動量を初期化する
for (auto& t : translation)
{
std::fill(t.begin(), t.end(), GgVector{ 0.0f, 0.0f, 0.0f, 1.0f });
}
// 矢印キーの設定値を初期化する
std::fill(arrow.begin(), arrow.end(), std::array<int, 2>{ 0, 0 });
// マウスホイールの回転量を初期化する
std::fill(wheel.begin(), wheel.end(), 0.0f);
}
//
// 平行移動量と回転量を更新する (X, Y のみ, Z は wheel() で計算する)
//
void GgApp::Window::HumanInterface::calcTranslation(int button, const std::array<GLfloat, 3>& velocity)
{
// マウスの相対変位
assert(button >= GLFW_MOUSE_BUTTON_1 && button < GLFW_MOUSE_BUTTON_1 + GG_BUTTON_COUNT);
const auto dx{ (mouse[0] - rotation[button].getStart(0)) * rotation[button].getScale(0) };
const auto dy{ (rotation[button].getStart(1) - mouse[1]) * rotation[button].getScale(1) };
// 平行移動量
auto& t{ translation[button] };
// 平行移動量の更新
t[1][0] = dx * velocity[0] + t[0][0];
t[1][1] = dy * velocity[1] + t[0][1];
// 回転量の更新
rotation[button].motion(mouse[0], mouse[1]);
}
//
// ウィンドウのサイズ変更時の処理
//
void GgApp::Window::resize(GLFWwindow* window, int width, int height)
{
// このインスタンスの this ポインタを得る
auto* const instance{ static_cast<Window*>(glfwGetWindowUserPointer(window)) };
if (instance)
{
// ウィンドウのサイズを保存する
instance->size[0] = width;
instance->size[1] = height;
// トラックボール処理の範囲を設定する
for (auto& current_if : instance->interfaceData)
{
for (auto& t : current_if.rotation)
{
t.region(width, height);
}
}
// ビューポートを更新する
instance->updateViewport();
// ユーザー定義のコールバック関数の呼び出し
if (instance->resizeFunc) (*instance->resizeFunc)(instance, width, height);
}
}
//
// キーボードをタイプした時の処理
//
void GgApp::Window::keyboard(GLFWwindow* window, int key, int scancode, int action, int mods)
{
#if defined(IMGUI_VERSION)
// ImGui がキーボードを使うときはキーボードの処理を行わない
if (ImGui::GetIO().WantCaptureKeyboard) return;
#endif
// このインスタンスの this ポインタを得る
auto* const instance{ static_cast<Window*>(glfwGetWindowUserPointer(window)) };
if (instance && action)
{
// ユーザー定義のコールバック関数の呼び出し
if (instance->keyboardFunc) (*instance->keyboardFunc)(instance, key, scancode, action, mods);
// 対象のユーザインタフェース
auto& current_if{ instance->interfaceData[instance->interfaceNo] };
switch (key)
{
case GLFW_KEY_HOME:
// トラックボールを初期化する
instance->resetRotation();
[[fallthrough]];
case GLFW_KEY_END:
// 平行移動量を初期化する
instance->resetTranslation();
break;
case GLFW_KEY_UP:
if (mods & GLFW_MOD_SHIFT)
current_if.arrow[1][1]++;
else if (mods & GLFW_MOD_CONTROL)
current_if.arrow[2][1]++;
else if (mods & GLFW_MOD_ALT)
current_if.arrow[3][1]++;
else
current_if.arrow[0][1]++;
break;
case GLFW_KEY_DOWN:
if (mods & GLFW_MOD_SHIFT)
current_if.arrow[1][1]--;
else if (mods & GLFW_MOD_CONTROL)
current_if.arrow[2][1]--;
else if (mods & GLFW_MOD_ALT)
current_if.arrow[3][1]--;
else
current_if.arrow[0][1]--;
break;
case GLFW_KEY_RIGHT:
if (mods & GLFW_MOD_SHIFT)
current_if.arrow[1][0]++;
else if (mods & GLFW_MOD_CONTROL)
current_if.arrow[2][0]++;
else if (mods & GLFW_MOD_ALT)
current_if.arrow[3][0]++;
else
current_if.arrow[0][0]++;
break;
case GLFW_KEY_LEFT:
if (mods & GLFW_MOD_SHIFT)
current_if.arrow[1][0]--;
else if (mods & GLFW_MOD_CONTROL)
current_if.arrow[2][0]--;
else if (mods & GLFW_MOD_ALT)
current_if.arrow[3][0]--;
else
current_if.arrow[0][0]--;
break;
default:
break;
}
current_if.lastKey = key;
}
}
//
// マウスボタンを操作したときの処理
//
void GgApp::Window::mouse(GLFWwindow* window, int button, int action, int mods)
{
#if defined(IMGUI_VERSION)
// ImGui がマウスを使うときは Window クラスのマウス位置を更新しない
if (ImGui::GetIO().WantCaptureMouse) return;
#endif
// このインスタンスの this ポインタを得る
auto* const instance{ static_cast<Window*>(glfwGetWindowUserPointer(window)) };
// マウスボタンの状態を記録する
assert(button >= GLFW_MOUSE_BUTTON_1 && button < GLFW_MOUSE_BUTTON_1 + GG_BUTTON_COUNT);
instance->status[button] = action != GLFW_RELEASE;
if (instance)
{
// ユーザー定義のコールバック関数の呼び出し
if (instance->mouseFunc) (*instance->mouseFunc)(instance, button, action, mods);
// 対象のユーザインタフェース
auto& current_if{ instance->interfaceData[instance->interfaceNo] };
// マウスの現在位置を得る
const auto x{ current_if.mouse[0] };
const auto y{ current_if.mouse[1] };
if (x < 0 || x >= instance->size[0] || y < 0 || y >= instance->size[1]) return;
if (action)
{
// ドラッグ開始
current_if.rotation[button].begin(x, y);
}
else
{
// ドラッグ終了
current_if.translation[button][0] = current_if.translation[button][1];
current_if.rotation[button].end(x, y);
}
}
}
//
// マウスホイールを操作した時の処理
//
void GgApp::Window::wheel(GLFWwindow* window, double x, double y)
{
#if defined(IMGUI_VERSION)
// ImGui がマウスを使うときは Window クラスのマウス位置を更新しない
if (ImGui::GetIO().WantCaptureMouse) return;
#endif
// このインスタンスの this ポインタを得る
auto* const instance{ static_cast<Window*>(glfwGetWindowUserPointer(window)) };
if (instance)
{
// ユーザー定義のコールバック関数の呼び出し
if (instance->wheelFunc) (*instance->wheelFunc)(instance, x, y);
// 対象のユーザインタフェース
auto& current_if{ instance->interfaceData[instance->interfaceNo] };
// マウスホイールの回転量の保存
current_if.wheel[0] += static_cast<GLfloat>(x);
current_if.wheel[1] += static_cast<GLfloat>(y);
// マウスによる平行移動量の z 値の更新
const auto z{ current_if.wheel[1] * instance->velocity[2] };
for (auto& t : current_if.translation) t[1][2] = z;
}
}
//
// Window クラスのコンストラクタ
//
GgApp::Window::Window(const std::string& title, int width, int height, int fullscreen, GLFWwindow* share) :
window{ nullptr },
size{ width, height },
fboSize{ width, height },
#if defined(IMGUI_VERSION)
menubarHeight{ 0 },
#endif
aspect{ 1.0f },
velocity{ 1.0f, 1.0f, 0.1f },
status{ false },
interfaceNo{ 0 },
userPointer{ nullptr },
resizeFunc{ nullptr },
keyboardFunc{ nullptr },
mouseFunc{ nullptr },
wheelFunc{ nullptr }
{
// ディスプレイの情報
GLFWmonitor* monitor{ nullptr };
// フルスクリーン表示
if (fullscreen > 0)
{
// 接続されているモニタの数を数える
int mcount;
auto** const monitors{ glfwGetMonitors(&mcount) };
// セカンダリモニタがあればそれを使う
if (fullscreen > mcount) fullscreen = mcount;
monitor = monitors[fullscreen - 1];
// モニタのモードを調べる
const auto* mode{ glfwGetVideoMode(monitor) };
// ウィンドウのサイズをディスプレイのサイズにする
width = mode->width;
height = mode->height;
}
// GLFW のウィンドウを作成する
window = glfwCreateWindow(width, height, title.c_str(), 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);
// 垂直同期タイミングに合わせる
glfwSwapInterval(1);
// 実際のフレームバッファのサイズを取得する
glfwGetFramebufferSize(window, &width, &height);
// ビューポートと投影変換行列を初期化する
resize(window, width, height);
#if defined(IMGUI_VERSION)
// 最初のウィンドウを開いたとき
static bool firstTime{ true };
if (firstTime)
{
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(nullptr);
// 実行済みであることを記録する
firstTime = false;
}
#endif
}
//
// イベントを取得してループを継続すべきかどうか調べる
//
GgApp::Window::operator bool()
{
// イベントを取り出す
glfwPollEvents();
// ウィンドウを閉じるべきなら false を返す
if (shouldClose()) return false;
// 対象のユーザインタフェース
auto& current_if{ interfaceData[interfaceNo] };
#if defined(IMGUI_VERSION)
// ImGui の新規フレームを作成する
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
// ImGui の状態を取り出す
const ImGuiIO& io{ ImGui::GetIO() };
// ImGui がマウスを使うときは Window クラスのマウス位置を更新しない
if (io.WantCaptureMouse) return true;
// マウスの位置を更新する
current_if.mouse = std::array<GLfloat, 2>{ io.MousePos.x, io.MousePos.y };
#else
// マウスの現在位置を調べる
double x, y;
glfwGetCursorPos(window, &x, &y);
// マウスの位置を更新する
current_if.mouse = std::array<GLfloat, 2>{ static_cast<GLfloat>(x), static_cast<GLfloat>(y) };
#endif
// マウスドラッグ
for (int button = GLFW_MOUSE_BUTTON_1; button < GLFW_MOUSE_BUTTON_1 + GG_BUTTON_COUNT; ++button)
{
// マウスボタンを押していたら
if (status[button])
{
// 現在位置と平行移動量を更新する
current_if.calcTranslation(button, velocity);
}
}
return true;
}
//
// カラーバッファを入れ替える
//
void GgApp::Window::swapBuffers() const
{
#if defined(IMGUI_VERSION)
// ImGui の描画データがあればフレームをレンダリングする
ImDrawData* data{ ImGui::GetDrawData() };
if (data) ImGui_ImplOpenGL3_RenderDrawData(data);
#endif
// エラーチェック
ggError();
// カラーバッファを入れ替える
glfwSwapBuffers(window);
}
//
// ビューポートのサイズを更新する
//
void GgApp::Window::updateViewport()
{
// フレームバッファの大きさを求める
glfwGetFramebufferSize(window, &fboSize[0], &fboSize[1]);
#if defined(IMGUI_VERSION)
// フレームバッファの高さからメニューバーの高さを減じる
fboSize[1] -= menubarHeight;
#endif
// ウィンドウの縦横比を保存する
aspect = static_cast<GLfloat>(fboSize[0]) / static_cast<GLfloat>(fboSize[1]);
// ビューポートを設定する
restoreViewport();
}
#if defined(GG_USE_OCULUS_RIFT)
# if OVR_PRODUCT_VERSION > 0
//
// グラフィックスカードのデフォルトの LUID を得る
//
ovrGraphicsLuid GgApp::Oculus::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 の比較
//
int GgApp::Oculus::Compare(const ovrGraphicsLuid& lhs, const ovrGraphicsLuid& rhs)
{
return memcmp(&lhs, &rhs, sizeof(ovrGraphicsLuid));
}
# endif
//
// コンストラクタ
//
GgApp::Oculus::Oculus() :
session{ nullptr },
oculusFbo{ 0 },
screen{ -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, },
mirrorFbo{ 0 },
window{ nullptr },
# if OVR_PRODUCT_VERSION > 0
frameIndex{ 0LL },
oculusDepth{ 0 },
mirrorWidth{ 1280 },
mirrorHeight{ 640 },
# endif
mirrorTexture{ nullptr }
{
}
//
// Oculus Rift のセッションを作成する
//
GgApp::Oculus& GgApp::Oculus::initialize(const Window& window)
{
// Oculus Rift のコンテキスト
static Oculus oculus;
// 既に Oculus Rift のセッションが作成されていたら参照を返す
if (oculus.session) return oculus;
// 最初に呼び出したときだけ実行する
static bool firstTime{ true };
if (firstTime)
{
// 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(ovr_Shutdown);
// 実行済みであることを記録する
firstTime = false;
}
// Oculus Rift のセッションを作成する
ovrGraphicsLuid luid;
if (OVR_FAILURE(ovr_Create(&oculus.session, &luid)))
throw std::runtime_error("Can't create Oculus Rift session");
# if OVR_PRODUCT_VERSION > 0
// デフォルトのグラフィックスアダプタが使われているか確かめる
if (Compare(luid, GetDefaultAdapterLuid()))
throw std::runtime_error("Graphics adapter is not default");
# endif
// session が無効ならエラー
if (!oculus.session) std::runtime_error("Unable to use the Oculus Rift.");
// ミラー表示を行うウィンドウを設定する
oculus.window = &window;
// Oculus Rift の情報を取り出す
oculus.hmdDesc = ovr_GetHmdDesc(oculus.session);
# if defined(_DEBUG)
// Oculus Rift の情報を表示する
std::cerr
<< "\nProduct name: " << oculus.hmdDesc.ProductName
<< "\nResolution: " << oculus.hmdDesc.Resolution.w << " x " << oculus.hmdDesc.Resolution.h
<< "\nDefault Fov: (" << oculus.hmdDesc.DefaultEyeFov[ovrEye_Left].LeftTan
<< "," << oculus.hmdDesc.DefaultEyeFov[ovrEye_Left].DownTan
<< ") - (" << oculus.hmdDesc.DefaultEyeFov[ovrEye_Left].RightTan
<< "," << oculus.hmdDesc.DefaultEyeFov[ovrEye_Left].UpTan
<< ")\n (" << oculus.hmdDesc.DefaultEyeFov[ovrEye_Right].LeftTan
<< "," << oculus.hmdDesc.DefaultEyeFov[ovrEye_Right].DownTan
<< ") - (" << oculus.hmdDesc.DefaultEyeFov[ovrEye_Right].RightTan
<< "," << oculus.hmdDesc.DefaultEyeFov[ovrEye_Right].UpTan
<< ")\nMaximum Fov: (" << oculus.hmdDesc.MaxEyeFov[ovrEye_Left].LeftTan
<< "," << oculus.hmdDesc.MaxEyeFov[ovrEye_Left].DownTan
<< ") - (" << oculus.hmdDesc.MaxEyeFov[ovrEye_Left].RightTan
<< "," << oculus.hmdDesc.MaxEyeFov[ovrEye_Left].UpTan
<< ")\n (" << oculus.hmdDesc.MaxEyeFov[ovrEye_Right].LeftTan
<< "," << oculus.hmdDesc.MaxEyeFov[ovrEye_Right].DownTan
<< ") - (" << oculus.hmdDesc.MaxEyeFov[ovrEye_Right].RightTan
<< "," << oculus.hmdDesc.MaxEyeFov[ovrEye_Right].UpTan
<< ")\n" << std::endl;
# endif
// Oculus Rift に転送する描画データを作成する
# if OVR_PRODUCT_VERSION > 0
oculus.layerData.Header.Type = ovrLayerType_EyeFov;
# else
oculus.layerData.Header.Type = ovrLayerType_EyeFovDepth;
# endif
// OpenGL なので左下が原点
oculus.layerData.Header.Flags = ovrLayerFlag_TextureOriginAtBottomLeft;
// Oculus Rift のレンダリングに使う FBO を作成する
glGenFramebuffers(ovrEye_Count, oculus.oculusFbo);
// 全ての目について
for (int eye = 0; eye < ovrEye_Count; ++eye)
{
// Oculus Rift の視野を取得する
const auto& fov{ oculus.hmdDesc.DefaultEyeFov[ovrEyeType(eye)] };
// Oculus Rift 用の FBO のサイズを求める
const auto textureSize{ ovr_GetFovTextureSize(oculus.session, ovrEyeType(eye), fov, 1.0f) };
// Oculus Rift のスクリーンのサイズを保存する
oculus.screen[eye][0] = -fov.LeftTan;
oculus.screen[eye][1] = fov.RightTan;
oculus.screen[eye][2] = -fov.DownTan;
oculus.screen[eye][3] = fov.UpTan;
# if OVR_PRODUCT_VERSION > 0
// 描画データに視野を設定する
oculus.layerData.Fov[eye] = fov;
// 描画データにビューポートを設定する
oculus.layerData.Viewport[eye].Pos = OVR::Vector2i(0, 0);
oculus.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 のレンダーターゲットとして使うテクスチャチェインを作成する
oculus.layerData.ColorTexture[eye] = nullptr;
if (OVR_SUCCESS(ovr_CreateTextureSwapChainGL(oculus.session, &colorDesc, &oculus.layerData.ColorTexture[eye])))
{
// 作成したテクスチャチェインの長さを取得する
int length(0);
if (OVR_SUCCESS(ovr_GetTextureSwapChainLength(oculus.session, oculus.layerData.ColorTexture[eye], &length)))
{
// テクスチャチェインの個々の要素について
for (int i = 0; i < length; ++i)
{
// テクスチャのパラメータを設定する
GLuint texId{ 0 };
ovr_GetTextureSwapChainBufferGL(oculus.session, oculus.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, oculus.oculusDepth + eye);
glBindTexture(GL_TEXTURE_2D, oculus.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
// 描画データに視野を設定する
oculus.layerData.EyeFov.Fov[eye] = fov;
// 描画データにビューポートを設定する
oculus.layerData.EyeFov.Viewport[eye].Pos = OVR::Vector2i(0, 0);
oculus.layerData.EyeFov.Viewport[eye].Size = textureSize;
// Oculus Rift 用の FBO のカラーバッファとして使うテクスチャセットを作成する
ovrSwapTextureSet* colorTexture{ nullptr };
ovr_CreateSwapTextureSetGL(oculus.session, GL_SRGB8_ALPHA8, textureSize.w, textureSize.h, &colorTexture);
oculus.layerData.EyeFov.ColorTexture[eye] = colorTexture;
// Oculus Rift 用の FBO のデプスバッファとして使うテクスチャセットを作成する
ovrSwapTextureSet* depthTexture{ nullptr };
ovr_CreateSwapTextureSetGL(oculus.session, GL_DEPTH_COMPONENT32F, textureSize.w, textureSize.h, &depthTexture);
oculus.layerData.EyeFovDepth.DepthTexture[eye] = depthTexture;
// Oculus Rift のレンズ補正等の設定値を取得する
oculus.eyeRenderDesc[eye] = ovr_GetRenderDesc(oculus.session, ovrEyeType(eye), fov);
# endif
}
# if OVR_PRODUCT_VERSION > 0
// 姿勢のトラッキングにおける床の高さを 0 に設定する
ovr_SetTrackingOriginType(oculus.session, ovrTrackingOrigin_FloorLevel);
// ミラー表示用の FBO を作成する
const GLsizei* size{ oculus.window->getSize() };
const ovrMirrorTextureDesc mirrorDesc
{
OVR_FORMAT_R8G8B8A8_UNORM_SRGB, // Format
oculus.mirrorWidth = size[0], // Width
oculus.mirrorHeight = size[1], // Height
0 // Flags
};
// ミラー表示用の FBO のカラーバッファとして使うテクスチャを作成する
if (OVR_SUCCESS(ovr_CreateMirrorTextureGL(oculus.session, &mirrorDesc, &oculus.mirrorTexture)))
{
// 作成したテクスチャのテクスチャ名を得る
GLuint texId{ 0 };
if (OVR_SUCCESS(ovr_GetMirrorTextureBufferGL(oculus.session, oculus.mirrorTexture, &texId)))
{
// ミラー表示用の FBO を作成してテクスチャをカラーバッファとして組み込む
glGenFramebuffers(1, &oculus.mirrorFbo);
glBindFramebuffer(GL_READ_FRAMEBUFFER, oculus.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
// 作成したテクスチャのテクスチャ名を得る
if (OVR_SUCCESS(ovr_CreateMirrorTextureGL(oculus.session, GL_SRGB8_ALPHA8, width, height, reinterpret_cast<ovrTexture**>(&mirrorTexture))))
{
// ミラー表示用の FBO を作成してテクスチャをカラーバッファとして組み込む
oculus.mirrorFbo = 0;
glGenFramebuffers(1, &oculus.mirrorFbo);
glBindFramebuffer(GL_READ_FRAMEBUFFER, oculus.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 にレンダリングするときは sRGB カラースペースを使う
glEnable(GL_FRAMEBUFFER_SRGB);
// フロントバッファに描く
glDrawBuffer(GL_FRONT);
// Oculus Rift への表示では垂直同期タイミングに合わせない
glfwSwapInterval(0);
return oculus;
}
//
// Oculus Rift のセッションを破棄する
//
void GgApp::Oculus::terminate()
{
// session が無効なら何もしない
if (!session) return;
// ミラー表示用の FBO を作っていたら削除する
if (mirrorFbo)
{
glDeleteFramebuffers(1, &mirrorFbo);
mirrorFbo = 0;
}
// ミラー表示用の FBO のカラーバッファ用のテクスチャを作っていたら削除する
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
mirrorTexture = nullptr;
}
// 全ての目について
for (int eye = 0; eye < ovrEye_Count; ++eye)
{
// Oculus Rift へのレンダリング用の FBO を削除する
glDeleteFramebuffers(1, oculusFbo + eye);
oculusFbo[eye] = 0;
# 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;
// カラースペースを元に戻す
glDisable(GL_FRAMEBUFFER_SRGB);
// バックバッファに描く
glDrawBuffer(GL_BACK);
// 垂直同期タイミングに合わせる
glfwSwapInterval(1);
}
//
// Oculus Rift による描画開始
//
bool GgApp::Oculus::begin()
{
# if OVR_PRODUCT_VERSION > 0
// セッションの状態を取得する
ovrSessionStatus sessionStatus;
ovr_GetSessionStatus(session, &sessionStatus);
// アプリケーションが終了を要求しているときはウィンドウのクローズフラグを立てる
if (sessionStatus.ShouldQuit) window->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;
}
//
// Oculus Rift の描画する目の指定
//
void GgApp::Oculus::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 };