forked from Detegr/openRBRVR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openRBRVR.cpp
666 lines (596 loc) · 25.1 KB
/
openRBRVR.cpp
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
#include <MinHook.h>
#include <d3d9.h>
#include <format>
#include <optional>
#include <ranges>
#include <vector>
#include "Config.hpp"
#include "D3D.hpp"
#include "Hook.hpp"
#include "Licenses.hpp"
#include "Quaternion.hpp"
#include "Util.hpp"
#include "VR.hpp"
#include "Version.hpp"
#include "Vertex.hpp"
#include "glm/gtx/matrix_decompose.hpp"
#include "openRBRVR.hpp"
IRBRGame* gGame;
static IDirect3DDevice9* gD3Ddev = nullptr;
Config gCfg;
static Config gSavedCfg;
namespace shader {
static M4 gCurrentProjectionMatrix;
static M4 gCurrentProjectionMatrixInverse;
static M4 gCurrentViewMatrix;
static M4 gCurrentViewMatrixInverse;
}
namespace fixedfunction {
D3DMATRIX gCurrentProjectionMatrix;
D3DMATRIX gCurrentViewMatrix;
}
static uintptr_t GetRBRBaseAddress()
{
// If ASLR is enabled, the base address is randomized
static uintptr_t addr;
addr = reinterpret_cast<uintptr_t>(GetModuleHandle(nullptr));
if (!addr) {
Dbg("Could not retrieve RBR base address, this may be bad.");
}
return addr;
}
static uintptr_t GetRBRAddress(uintptr_t target)
{
constexpr uintptr_t RBRAbsoluteLoadAddr = 0x400000;
return GetRBRBaseAddress() + target - RBRAbsoluteLoadAddr;
}
static uintptr_t RBRTrackIdAddr = GetRBRAddress(0x1660804);
static uintptr_t RBRRenderFunctionAddr = GetRBRAddress(0x47E1E0);
static uintptr_t RBRCarQuatPtrAddr = GetRBRAddress(0x8EF660);
void __fastcall RBRHook_Render(void* p);
namespace hooks {
Hook<decltype(&Direct3DCreate9)> create;
Hook<decltype(IDirect3D9Vtbl::CreateDevice)> createdevice;
Hook<decltype(IDirect3DDevice9Vtbl::SetVertexShaderConstantF)> setvertexshaderconstantf;
Hook<decltype(IDirect3DDevice9Vtbl::SetTransform)> settransform;
Hook<decltype(IDirect3DDevice9Vtbl::Present)> present;
Hook<decltype(&RBRHook_Render)> render;
Hook<decltype(IDirect3DDevice9Vtbl::CreateVertexShader)> createvertexshader;
Hook<decltype(IDirect3DDevice9Vtbl::SetRenderTarget)> btbsetrendertarget;
}
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
MH_Initialize();
return TRUE;
}
static std::optional<RenderTarget> gVRRenderTarget;
static IDirect3DSurface9 *gOriginalScreenTgt, *gOriginalDepthStencil;
static bool gDriving;
static uint32_t gGameMode;
static std::vector<IDirect3DVertexShader9*> gOriginalShaders;
static Quaternion* gCarQuat;
static M4 gLockToHorizonMatrix = glm::identity<M4>();
constexpr uintptr_t RBRRXDeviceVtblOffset = 0x4f56c;
constexpr uintptr_t RBRRXTrackStatusOffset = 0x608d0;
static volatile uint8_t* gBTBTrackStatus;
static bool IsRBRRXLoaded()
{
return reinterpret_cast<uintptr_t>(gBTBTrackStatus) != RBRRXTrackStatusOffset;
}
static bool IsRBRHUDLoaded()
{
static bool triedToObtain;
static bool isLoaded;
if (!triedToObtain) {
auto hudHandle = GetModuleHandle("Plugins\\RBRHUD.dll");
isLoaded = (hudHandle != nullptr);
}
return isLoaded;
}
static std::chrono::steady_clock::time_point gFrameStart;
static bool IsLoadingBTBStage()
{
return gBTBTrackStatus && *gBTBTrackStatus == 1 && gGameMode == 5;
}
HRESULT __stdcall DXHook_CreateVertexShader(IDirect3DDevice9* This, const DWORD* pFunction, IDirect3DVertexShader9** ppShader)
{
static int i = 0;
auto ret = hooks::createvertexshader.call(This, pFunction, ppShader);
if (i < 40) {
// These are the original shaders for RBR that need
// to be patched with the VR projection.
gOriginalShaders.push_back(*ppShader);
}
i++;
return ret;
}
// Call the RBR render function with a texture as the render target
// Even though the render pipeline changes the render target while rendering,
// the original render target is respected and restored at the end of the pipeline.
void RenderVREye(void* p, RenderTarget eye, bool clear = true)
{
gVRRenderTarget = eye;
PrepareVRRendering(gD3Ddev, eye, clear);
hooks::render.call(p);
FinishVRRendering(gD3Ddev, eye);
gVRRenderTarget = std::nullopt;
}
// Render `renderTarget2d` on a plane for both eyes
void RenderVROverlay(RenderTarget renderTarget2D, bool clear)
{
auto size = renderTarget2D == Menu ? gCfg.menuSize : gCfg.overlaySize;
auto translation = renderTarget2D == Overlay ? gCfg.overlayTranslation : glm::vec3 { 0.0f, 0.0f, 0.0f };
auto horizLock = renderTarget2D == Overlay ? std::make_optional(gLockToHorizonMatrix) : std::nullopt;
PrepareVRRendering(gD3Ddev, LeftEye, clear);
RenderMenuQuad(gD3Ddev, LeftEye, renderTarget2D, size, translation, horizLock);
FinishVRRendering(gD3Ddev, LeftEye);
PrepareVRRendering(gD3Ddev, RightEye, clear);
RenderMenuQuad(gD3Ddev, RightEye, renderTarget2D, size, translation, horizLock);
FinishVRRendering(gD3Ddev, RightEye);
}
// RBR 3D scene draw function is rerouted here
void __fastcall RBRHook_Render(void* p)
{
auto ptr = reinterpret_cast<uintptr_t>(p);
auto doRendering = *reinterpret_cast<uint32_t*>(ptr + 0x720) == 0;
gGameMode = *reinterpret_cast<uint32_t*>(ptr + 0x728);
gDriving = gGameMode == 1;
gD3Ddev->GetRenderTarget(0, &gOriginalScreenTgt);
gD3Ddev->GetDepthStencilSurface(&gOriginalDepthStencil);
if (!doRendering) [[unlikely]] {
return;
}
if (gHMD) [[likely]] {
if (gDriving && (gCfg.lockToHorizon != Config::HorizonLock::LOCK_NONE) && !gCarQuat) [[unlikely]] {
uintptr_t p = *reinterpret_cast<uintptr_t*>(RBRCarQuatPtrAddr) + 0x100;
gCarQuat = reinterpret_cast<Quaternion*>(p);
} else if (gCarQuat && !gDriving) [[unlikely]] {
gCarQuat = nullptr;
gLockToHorizonMatrix = glm::identity<M4>();
}
// UpdateVRPoses should be called as close to rendering as possible
UpdateVRPoses(gCarQuat, gCfg.lockToHorizon, &gLockToHorizonMatrix);
if (gCfg.debug) [[unlikely]] {
gFrameStart = std::chrono::steady_clock::now();
}
if (gDriving) [[likely]] {
RenderVREye(p, LeftEye);
RenderVREye(p, RightEye);
PrepareVRRendering(gD3Ddev, Overlay);
} else {
// We are not driving, render the scene into a plane
gVRRenderTarget = std::nullopt;
auto shouldSwapRenderTarget = !(IsLoadingBTBStage() && !gCfg.drawLoadingScreen);
if (shouldSwapRenderTarget) {
PrepareVRRendering(gD3Ddev, Menu);
}
auto shouldRender = !(gGameMode == 5 && !gCfg.drawLoadingScreen);
if (shouldRender) {
hooks::render.call(p);
}
}
} else {
hooks::render.call(p);
}
// Reset VR eye information so that the rest of the shaders won't use the VR projection.
// It's necessary for rendering the menu elements correctly.
gVRRenderTarget = std::nullopt;
}
HRESULT __stdcall DXHook_Present(IDirect3DDevice9* This, const RECT* pSourceRect, const RECT* pDestRect, HWND hDestWindowOverride, const RGNDATA* pDirtyRegion)
{
if (gHMD) [[likely]] {
auto current2DRenderTarget = gDriving ? Overlay : Menu;
auto shouldRender = !(IsLoadingBTBStage() && !gCfg.drawLoadingScreen);
if (shouldRender) [[likely]] {
FinishVRRendering(gD3Ddev, current2DRenderTarget);
RenderVROverlay(current2DRenderTarget, !gDriving);
}
SubmitFramesToHMD(gD3Ddev);
}
gD3Ddev->SetRenderTarget(0, gOriginalScreenTgt);
gD3Ddev->SetDepthStencilSurface(gOriginalDepthStencil);
gOriginalScreenTgt->Release();
gOriginalDepthStencil->Release();
if (gHMD && gCfg.drawCompanionWindow) {
RenderCompanionWindowFromRenderTarget(This, gDriving ? LeftEye : Menu);
}
auto ret = hooks::present.call(This, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
if (gCfg.debug && gHMD) [[unlikely]] {
auto frameEnd = std::chrono::steady_clock::now();
auto frameTime = std::chrono::duration_cast<std::chrono::microseconds>(frameEnd - gFrameStart);
auto t = GetFrameTiming();
static uint32_t totalDroppedFrames = 0;
static uint32_t totalMispresentedFrames = 0;
totalDroppedFrames += t.droppedFrames;
totalMispresentedFrames += t.mispresentedFrames;
gGame->SetColor(1, 0, 1, 1.0);
gGame->SetFont(IRBRGame::EFonts::FONT_DEBUG);
gGame->WriteText(0, 18 * 0, std::format("openRBRVR {}", VERSION_STR).c_str());
gGame->WriteText(0, 18 * 1,
std::format("CPU: render time: {:.2f}ms, present: {:.2f}ms, waitforpresent: {:.2f}ms",
frameTime.count() / 1000.0,
t.cpuPresentCall,
t.cpuWaitForPresent)
.c_str());
gGame->WriteText(0, 18 * 2,
std::format("GPU: presubmit {:.2f}ms, postSubmit: {:.2f}ms, total: {:.2f}ms",
t.gpuPreSubmit,
t.gpuPostSubmit,
t.gpuTotal)
.c_str());
gGame->WriteText(0, 18 * 3,
std::format("Compositor: CPU: {:.2f}ms, GPU: {:.2f}ms, submit: {:.2f}ms",
t.compositorCpu,
t.compositorGpu,
t.compositorSubmitFrame)
.c_str());
gGame->WriteText(0, 18 * 4,
std::format("Total mispresented frames: {}, Total dropped frames: {}, Reprojection flags: {:X}",
totalMispresentedFrames,
totalDroppedFrames,
t.reprojectionFlags)
.c_str());
gGame->WriteText(0, 18 * 5, std::format("Mods: {} {}", IsRBRRXLoaded() ? "RBRRX" : "", IsRBRHUDLoaded() ? "RBRHUD" : "").c_str());
const auto& [lw, lh] = GetRenderResolution(LeftEye);
const auto& [rw, rh] = GetRenderResolution(RightEye);
gGame->WriteText(0, 18 * 6, std::format("Render resolution: {}x{} (left), {}x{} (right)", lw, lh, rw, rh).c_str());
}
return ret;
}
HRESULT __stdcall DXHook_SetVertexShaderConstantF(IDirect3DDevice9* This, UINT StartRegister, const float* pConstantData, UINT Vector4fCount)
{
auto shouldPatchShader = true;
if (gBTBTrackStatus && *gBTBTrackStatus == 1) {
// While rendering a BTB stage, patch only the original shaders.
// If we touch BTB shaders, bad things will happen.
IDirect3DVertexShader9* shader;
This->GetVertexShader(&shader);
shouldPatchShader = std::find(gOriginalShaders.cbegin(), gOriginalShaders.cend(), shader) != gOriginalShaders.end();
}
if (shouldPatchShader && gVRRenderTarget && Vector4fCount == 4) {
if (StartRegister == 0) {
// MVP matrix
// MV = P^-1 * MVP
// MVP[VRRenderTarget] = P[VRRenderTarget] * MV
// For some reason the Z-axis is pointing to the wrong direction, so it is flipped here as well
auto orig = glm::transpose(M4FromShaderConstantPtr(pConstantData));
auto mv = shader::gCurrentProjectionMatrixInverse * orig;
auto mvp = glm::transpose(gProjection[gVRRenderTarget.value()] * gEyePos[gVRRenderTarget.value()] * gHMDPose * gFlipZMatrix * gLockToHorizonMatrix * mv);
return hooks::setvertexshaderconstantf.call(This, StartRegister, glm::value_ptr(mvp), Vector4fCount);
} else if (StartRegister == 20) {
// Sky/fog
// It seems this parameter contains the orientation of the car and the
// skybox and fog is rendered correctly only in the direction where the car
// points at. By rotating this with the HMD's rotation, the skybox and possible
// fog is rendered correctly.
auto orig = glm::transpose(M4FromShaderConstantPtr(pConstantData));
glm::vec4 perspective;
glm::vec3 scale, translation, skew;
glm::quat orientation;
glm::decompose(gHMDPose, scale, orientation, translation, skew, perspective);
auto rotation = glm::mat4_cast(glm::conjugate(orientation));
auto m = glm::transpose(rotation * orig);
return hooks::setvertexshaderconstantf.call(This, StartRegister, glm::value_ptr(m), Vector4fCount);
}
}
return hooks::setvertexshaderconstantf.call(This, StartRegister, pConstantData, Vector4fCount);
}
HRESULT __stdcall DXHook_SetTransform(IDirect3DDevice9* This, D3DTRANSFORMSTATETYPE State, const D3DMATRIX* pMatrix)
{
if (gVRRenderTarget && State == D3DTS_PROJECTION) {
shader::gCurrentProjectionMatrix = M4FromD3D(*pMatrix);
shader::gCurrentProjectionMatrixInverse = glm::inverse(shader::gCurrentProjectionMatrix);
fixedfunction::gCurrentProjectionMatrix = D3DFromM4(gProjection[gVRRenderTarget.value()]);
return hooks::settransform.call(This, State, &fixedfunction::gCurrentProjectionMatrix);
} else if (gVRRenderTarget && State == D3DTS_VIEW) {
fixedfunction::gCurrentViewMatrix = D3DFromM4(gEyePos[gVRRenderTarget.value()] * gHMDPose * gFlipZMatrix * gLockToHorizonMatrix * M4FromD3D(*pMatrix));
return hooks::settransform.call(This, State, &fixedfunction::gCurrentViewMatrix);
}
return hooks::settransform.call(This, State, pMatrix);
}
HRESULT __stdcall BTB_SetRenderTarget(IDirect3DDevice9* This, DWORD RenderTargetIndex, IDirect3DSurface9* pRenderTarget)
{
// This was found purely by luck after testing all kinds of things.
// For some reason, if this call is called with the original This pointer (from RBRRX)
// plugins switching the render target (i.e. RBRHUD) will cause the stage geometry
// to not be rendered at all. Routing the call to the D3D device created by openRBRVR,
// it seems to work correctly.
return gD3Ddev->SetRenderTarget(RenderTargetIndex, pRenderTarget);
}
HRESULT __stdcall DXHook_CreateDevice(
IDirect3D9* This,
UINT Adapter,
D3DDEVTYPE DeviceType,
HWND hFocusWindow,
DWORD BehaviorFlags,
D3DPRESENT_PARAMETERS* pPresentationParameters,
IDirect3DDevice9** ppReturnedDeviceInterface)
{
IDirect3DDevice9* dev = nullptr;
auto ret = hooks::createdevice.call(This, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, &dev);
if (FAILED(ret)) {
Dbg("D3D initialization failed: CreateDevice");
return ret;
}
*ppReturnedDeviceInterface = dev;
auto devvtbl = GetVtable<IDirect3DDevice9Vtbl>(dev);
hooks::setvertexshaderconstantf = Hook(devvtbl->SetVertexShaderConstantF, DXHook_SetVertexShaderConstantF);
hooks::settransform = Hook(devvtbl->SetTransform, DXHook_SetTransform);
hooks::present = Hook(devvtbl->Present, DXHook_Present);
hooks::createvertexshader = Hook(devvtbl->CreateVertexShader, DXHook_CreateVertexShader);
gD3Ddev = dev;
RECT winBounds;
GetWindowRect(hFocusWindow, &winBounds);
InitVR(dev, gCfg, &gD3DVR, winBounds.right, winBounds.bottom);
// Initialize this pointer here, as it's too early to do this in openRBRVR constructor
auto rxHandle = GetModuleHandle("Plugins\\rbr_rx.dll");
if (rxHandle) {
auto rxAddr = reinterpret_cast<uintptr_t>(rxHandle);
gBTBTrackStatus = reinterpret_cast<uint8_t*>(rxAddr + RBRRXTrackStatusOffset);
IDirect3DDevice9Vtbl* rbrrxdev = reinterpret_cast<IDirect3DDevice9Vtbl*>(rxAddr + RBRRXDeviceVtblOffset);
hooks::btbsetrendertarget = Hook(rbrrxdev->SetRenderTarget, BTB_SetRenderTarget);
}
return ret;
}
IDirect3D9* __stdcall DXHook_Direct3DCreate9(UINT SDKVersion)
{
auto d3d = hooks::create.call(SDKVersion);
auto d3dvtbl = GetVtable<IDirect3D9Vtbl>(d3d);
hooks::createdevice = Hook(d3dvtbl->CreateDevice, DXHook_CreateDevice);
return d3d;
}
openRBRVR::openRBRVR(IRBRGame* g)
: game(g)
, menuIdx(0)
, menuPage(0)
, menuScroll(0)
{
gGame = g;
Dbg("Hooking DirectX");
auto d3ddll = GetModuleHandle("d3d9.dll");
if (!d3ddll) {
Dbg("failed to get handle to d3d9.dll\n");
return;
}
auto d3dcreate = reinterpret_cast<decltype(&Direct3DCreate9)>(GetProcAddress(d3ddll, "Direct3DCreate9"));
if (!d3dcreate) {
Dbg("failed to find address to Direct3DCreate9\n");
return;
}
hooks::create = Hook(d3dcreate, DXHook_Direct3DCreate9);
hooks::render = Hook(*reinterpret_cast<decltype(RBRHook_Render)*>(RBRRenderFunctionAddr), RBRHook_Render);
gCfg = Config::fromFile("Plugins\\openRBRVR.ini");
gSavedCfg = gCfg;
}
openRBRVR::~openRBRVR()
{
ShutdownVR();
}
static Config::HorizonLock ChangeHorizonLock(Config::HorizonLock lock, bool forward)
{
switch (lock) {
case Config::HorizonLock::LOCK_NONE:
return forward ? Config::HorizonLock::LOCK_ROLL : static_cast<Config::HorizonLock>((Config::HorizonLock::LOCK_ROLL | Config::HorizonLock::LOCK_PITCH));
case Config::HorizonLock::LOCK_ROLL:
return forward ? Config::HorizonLock::LOCK_PITCH : Config::HorizonLock::LOCK_NONE;
case Config::HorizonLock::LOCK_PITCH:
return forward ? static_cast<Config::HorizonLock>((Config::HorizonLock::LOCK_ROLL | Config::HorizonLock::LOCK_PITCH)) : Config::HorizonLock::LOCK_ROLL;
case (Config::HorizonLock::LOCK_ROLL | Config::HorizonLock::LOCK_PITCH):
return forward ? Config::HorizonLock::LOCK_NONE : Config::HorizonLock::LOCK_PITCH;
default:
return Config::HorizonLock::LOCK_NONE;
}
}
enum MenuItems {
RECENTER_VIEW = 0,
TOGGLE_DEBUG = 1,
COMPANION_WINDOW = 2,
LOADING_SCREEN = 3,
HORIZON = 4,
LICENSES = 5,
SAVE = 6,
MENU_ITEM_COUNT = 7,
};
void openRBRVR::HandleFrontEndEvents(char txtKeyboard, bool bUp, bool bDown, bool bLeft, bool bRight, bool bSelect)
{
if (menuPage == 0) {
if (bDown) {
(++menuIdx) %= MENU_ITEM_COUNT;
if (menuIdx == SAVE && gCfg == gSavedCfg) {
(++menuIdx) %= MENU_ITEM_COUNT;
}
} else if (bUp) {
if (--menuIdx < 0) {
menuIdx = MENU_ITEM_COUNT - 2;
}
} else if (bSelect) {
switch (menuIdx) {
case RECENTER_VIEW: {
vr::VRChaperone()->ResetZeroPose(vr::ETrackingUniverseOrigin::TrackingUniverseSeated);
break;
}
case TOGGLE_DEBUG: {
gCfg.debug = !gCfg.debug;
break;
}
case COMPANION_WINDOW: {
gCfg.drawCompanionWindow = !gCfg.drawCompanionWindow;
break;
}
case LOADING_SCREEN: {
gCfg.drawLoadingScreen = !gCfg.drawLoadingScreen;
break;
}
case HORIZON: {
gCfg.lockToHorizon = ChangeHorizonLock(gCfg.lockToHorizon, true);
break;
}
case LICENSES: {
menuPage = 1;
break;
}
case SAVE: {
if (gCfg.Write("Plugins\\openRBRVR.ini")) {
gSavedCfg = gCfg;
}
break;
}
default:
break;
}
} else if (bLeft) {
switch (menuIdx) {
case TOGGLE_DEBUG: {
gCfg.debug = !gCfg.debug;
break;
}
case COMPANION_WINDOW: {
gCfg.drawCompanionWindow = !gCfg.drawCompanionWindow;
break;
}
case LOADING_SCREEN: {
gCfg.drawLoadingScreen = !gCfg.drawLoadingScreen;
break;
}
case HORIZON: {
gCfg.lockToHorizon = ChangeHorizonLock(gCfg.lockToHorizon, false);
break;
}
}
} else if (bRight) {
switch (menuIdx) {
case TOGGLE_DEBUG: {
gCfg.debug = !gCfg.debug;
break;
}
case COMPANION_WINDOW: {
gCfg.drawCompanionWindow = !gCfg.drawCompanionWindow;
break;
}
case LOADING_SCREEN: {
gCfg.drawLoadingScreen = !gCfg.drawLoadingScreen;
break;
}
case HORIZON: {
gCfg.lockToHorizon = ChangeHorizonLock(gCfg.lockToHorizon, true);
break;
}
}
}
} else if (menuPage == 1) {
if (bSelect || bLeft) {
menuPage = 0;
} else if (bDown) {
if (menuScroll < gLicenseInformation.size()) {
menuScroll++;
}
} else if (bUp) {
if (menuScroll > 0) {
menuScroll--;
}
}
}
}
static std::string GetHorizonLockStr()
{
switch (gCfg.lockToHorizon) {
case Config::HorizonLock::LOCK_NONE:
return "Off";
case Config::HorizonLock::LOCK_ROLL:
return "Roll";
case Config::HorizonLock::LOCK_PITCH:
return "Pitch";
case (Config::HorizonLock::LOCK_ROLL | Config::HorizonLock::LOCK_PITCH):
return "Pitch and roll";
default:
return "Unknown";
}
}
struct MenuEntry {
std::string text;
std::optional<IRBRGame::EFonts> font;
std::optional<IRBRGame::EMenuColors> menuColor;
std::optional<std::tuple<float, float, float, float>> color;
std::optional<std::tuple<float, float>> position;
};
void openRBRVR::DrawMenuEntries(const std::ranges::forward_range auto& entries, float rowHeight)
{
auto i = 0;
auto x = 0.0f;
auto y = 0.0f;
for (const auto& entry : entries) {
if (entry.font) {
game->SetFont(entry.font.value());
}
if (entry.menuColor) {
game->SetMenuColor(entry.menuColor.value());
} else if (entry.color) {
auto [r, g, b, a] = entry.color.value();
game->SetColor(r, g, b, a);
}
if (entry.position) {
auto [newx, newy] = entry.position.value();
x = newx;
y = newy;
}
game->WriteText(x, y, entry.text.c_str());
y += rowHeight;
}
}
void openRBRVR::DrawFrontEndPage()
{
constexpr auto menuItemsStartPos = 70.0f;
constexpr auto rowHeight = 21.0f;
constexpr auto licenseRowHeight = 14.0f;
constexpr auto configurationTextColor = std::make_tuple(0.7f, 0.7f, 0.7f, 1.0f);
MenuEntry renderResolution;
if (gHMD) {
const auto& [lw, lh] = GetRenderResolution(LeftEye);
const auto& [rw, rh] = GetRenderResolution(RightEye);
renderResolution = { std::format("Render resolution: {}x{} (left), {}x{} (right)", lw, lh, rw, rh) };
} else {
renderResolution = { "VR mode not initialized" };
}
switch (menuPage) {
case 0: {
game->DrawBlackOut(520.0f, 0.0f, 190.0f, 500.0f);
game->DrawSelection(0.0f, menuItemsStartPos - 2.0f + (static_cast<float>(menuIdx) * rowHeight), 440.0f);
// clang-format off
DrawMenuEntries(std::to_array<MenuEntry>({
{"openRBRVR", IRBRGame::EFonts::FONT_BIG, IRBRGame::EMenuColors::MENU_HEADING, std::nullopt, std::make_tuple(65.0f, 49.0f)},
{"Recenter view", std::nullopt, IRBRGame::EMenuColors::MENU_TEXT, std::nullopt, std::make_tuple(65.0f, menuItemsStartPos)},
{std::format("Debug information: {}", gCfg.debug ? "ON" : "OFF")},
{std::format("Draw desktop window: {}", gCfg.drawCompanionWindow ? "ON" : "OFF")},
{std::format("Draw loading screen: {}", gCfg.drawLoadingScreen ? "ON" : "OFF")},
{std::format("Lock horizon: {}", GetHorizonLockStr())},
{"Licenses"},
{.text = "Save the current config to openRBRVR.ini", .color = (gCfg == gSavedCfg) ? std::make_tuple(0.5f, 0.5f, 0.5f, 1.0f) : std::make_tuple(1.0f, 1.0f, 1.0f, 1.0f)},
{""},
{"Configuration:", IRBRGame::EFonts::FONT_BIG, std::nullopt, configurationTextColor},
{std::format("Menu size: {:.2f}", gCfg.menuSize), IRBRGame::EFonts::FONT_SMALL},
{std::format("Overlay size: {:.2f}", gCfg.overlaySize)},
{std::format("Supersampling: {:.2f}", gCfg.superSampling)},
renderResolution,
{"https://github.com/Detegr/openRBRVR", std::nullopt, std::nullopt, std::nullopt, std::make_tuple(10.0f, 500.0f - rowHeight*2)},
}));
//clang-format on
break;
}
case 1: {
game->SetFont(IRBRGame::EFonts::FONT_SMALL);
game->DrawBlackOut(520.0f, 0.0f, 190.0f, 500.0f);
game->SetFont(IRBRGame::EFonts::FONT_BIG);
game->SetMenuColor(IRBRGame::EMenuColors::MENU_HEADING);
game->WriteText(65.0f, 10.0f, "openRBRVR licenses");
game->SetMenuColor(IRBRGame::EMenuColors::MENU_TEXT);
game->WriteText(10.0f, 10.0f + rowHeight, "Press enter or left to go back");
game->SetFont(IRBRGame::EFonts::FONT_SMALL);
uint32_t i = 0;
for (auto& row : gLicenseInformation) {
if (i++ < menuScroll)
continue;
game->WriteText(10.0f, 10.0f + rowHeight * 2 + (licenseRowHeight * (i - menuScroll)), row);
}
}
}
}