forked from ChristophHaag/SteamVR-OpenHMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_openhmd.cpp
1151 lines (939 loc) · 41.4 KB
/
driver_openhmd.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
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
//============ Copyright (c) Valve Corporation, All rights reserved. ============
#include "ohmd_config.h" // which hmds and trackers to use
#include <openvr_driver.h>
#include "driverlog.h"
#include <assert.h>
#include <vector>
#include <thread>
#include <chrono>
#include <cstring>
#include <sstream>
#if defined( _WINDOWS )
#include <windows.h>
#endif
#include <openhmd.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
using namespace vr;
#if defined(_WIN32)
#define HMD_DLL_EXPORT extern "C" __declspec( dllexport )
#define HMD_DLL_IMPORT extern "C" __declspec( dllimport )
#elif defined(__GNUC__) || defined(COMPILER_GCC) || defined(__APPLE__)
#define HMD_DLL_EXPORT extern "C" __attribute__((visibility("default")))
#define HMD_DLL_IMPORT extern "C"
#else
#error "Unsupported Platform."
#endif
ohmd_context* ctx;
class COpenHMDDeviceDriverController;
// gets float values from the device and prints them
void print_infof(ohmd_device* hmd, const char* name, int len, ohmd_float_value val)
{
float f[20];
assert (len <= 20);
ohmd_device_getf(hmd, val, f);
printf("%-25s", name);
for(int i = 0; i < len; i++)
printf("%f ", f[i]);
printf("\n");
}
// gets int values from the device and prints them
void print_infoi(ohmd_device* hmd, const char* name, int len, ohmd_int_value val)
{
int iv[20];
assert (len <= 20);
ohmd_device_geti(hmd, val, iv);
printf("%-25s", name);
for(int i = 0; i < len; i++)
printf("%d ", iv[i]);
printf("\n");
}
// keys for use with the settings API
static const char * const k_pch_Sample_Section = "driver_openhmd";
static const char * const k_pch_Sample_SecondsFromVsyncToPhotons_Float = "secondsFromVsyncToPhotons";
static const char * const k_pch_Sample_DisplayFrequency_Float = "displayFrequency";
HmdQuaternion_t identityquat{ 1, 0, 0, 0};
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CWatchdogDriver_OpenHMD : public IVRWatchdogProvider
{
public:
CWatchdogDriver_OpenHMD()
{
DriverLog("Created watchdog object\n");
m_pWatchdogThread = nullptr;
}
virtual ~CWatchdogDriver_OpenHMD() {}
virtual EVRInitError Init( vr::IVRDriverContext *pDriverContext ) ;
virtual void Cleanup() ;
private:
std::thread *m_pWatchdogThread;
};
CWatchdogDriver_OpenHMD g_watchdogDriverOpenHMD;
bool g_bExiting = false;
void WatchdogThreadFunction( )
{
while ( !g_bExiting )
{
#if defined( _WINDOWS )
// on windows send the event when the Y key is pressed.
if ( (0x01 & GetAsyncKeyState( 'Y' )) != 0 )
{
// Y key was pressed.
vr::VRWatchdogHost()->WatchdogWakeUp(vr::TrackedDeviceClass_HMD);
}
std::this_thread::sleep_for( std::chrono::microseconds( 500 ) );
#else
DriverLog("Watchdog wakeup\n");
// for the other platforms, just send one every five seconds
std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
vr::VRWatchdogHost()->WatchdogWakeUp(vr::TrackedDeviceClass_HMD);
#endif
}
DriverLog("Watchdog exit\n");
}
EVRInitError CWatchdogDriver_OpenHMD::Init( vr::IVRDriverContext *pDriverContext )
{
VR_INIT_WATCHDOG_DRIVER_CONTEXT( pDriverContext );
InitDriverLog( vr::VRDriverLog() );
// Watchdog mode on Windows starts a thread that listens for the 'Y' key on the keyboard to
// be pressed. A real driver should wait for a system button event or something else from the
// the hardware that signals that the VR system should start up.
g_bExiting = false;
DriverLog("starting watchdog thread\n");
m_pWatchdogThread = new std::thread( WatchdogThreadFunction );
if ( !m_pWatchdogThread )
{
DriverLog( "Unable to create watchdog thread\n");
return VRInitError_Driver_Failed;
}
return VRInitError_None;
}
void CWatchdogDriver_OpenHMD::Cleanup()
{
g_bExiting = true;
if ( m_pWatchdogThread )
{
m_pWatchdogThread->join();
delete m_pWatchdogThread;
m_pWatchdogThread = nullptr;
}
CleanupDriverLog();
}
class COpenHMDDeviceDriverController : public vr::ITrackedDeviceServerDriver /*, public vr::IVRControllerComponent */ {
public:
int index;
ohmd_device* device;
int device_idx;
int device_flags;
DriverPose_t pose;
COpenHMDDeviceDriverController(int index, ohmd_device* _device, int _device_idx) :
index(index), device(_device), device_idx(_device_idx) {
DriverLog("construct controller object %d (OpenHMD device %d)\n", index, device_idx);
m_unObjectId = vr::k_unTrackedDeviceIndexInvalid;
pose = { 0 };
}
virtual ~COpenHMDDeviceDriverController() {}
EVRInitError Activate( vr::TrackedDeviceIndex_t unObjectId )
{
DriverLog("activate controller %d: %d\n", index, unObjectId);
m_unObjectId = unObjectId;
if (this->device == NULL) {
return VRInitError_Init_InterfaceNotFound;
}
m_ulPropertyContainer = vr::VRProperties()->TrackedDeviceToPropertyContainer( m_unObjectId );
const char *controllerModel = ohmd_list_gets(ctx, device_idx, OHMD_PRODUCT);
ohmd_list_geti(ctx, device_idx, OHMD_DEVICE_FLAGS, &device_flags);
vr::VRProperties()->SetStringProperty( m_ulPropertyContainer, Prop_ModelNumber_String, controllerModel);
//vr::VRProperties()->SetStringProperty( m_ulPropertyContainer, Prop_RenderModelName_String, controllerModel);
vr::VRProperties()->SetStringProperty( m_ulPropertyContainer, Prop_RenderModelName_String, "vr_tracker_vive_1_0");
//vr::VRProperties()->SetStringProperty( m_ulPropertyContainer, Prop_ModelNumber_String, "1" );
vr::VRProperties()->SetStringProperty( m_ulPropertyContainer, Prop_ControllerType_String, "openhmd_controller" );
// return a constant that's not 0 (invalid) or 1 (reserved for Oculus)
vr::VRProperties()->SetUint64Property( m_ulPropertyContainer, Prop_CurrentUniverseId_Uint64, 2 );
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, Prop_DeviceClass_Int32, vr::TrackedDeviceClass_Controller);
// avoid "not fullscreen" warnings from vrmonitor
vr::VRProperties()->SetBoolProperty( m_ulPropertyContainer, Prop_IsOnDesktop_Bool, false );
if (device_flags & OHMD_DEVICE_FLAGS_LEFT_CONTROLLER) {
DriverLog("Left Controller\n");
vr::VRProperties()->SetInt32Property( m_ulPropertyContainer, Prop_ControllerRoleHint_Int32, TrackedControllerRole_LeftHand);
// Set an initial position down and to the left, which will be
// used if there's no positional tracking
pose.vecPosition[0] = -0.25;
pose.vecPosition[1] = -0.5;
pose.vecPosition[2] = 0.15;
} else {
DriverLog("Right Controller\n");
vr::VRProperties()->SetInt32Property( m_ulPropertyContainer, Prop_ControllerRoleHint_Int32, TrackedControllerRole_RightHand);
pose.vecPosition[0] = 0.25;
pose.vecPosition[1] = -0.5;
pose.vecPosition[2] = 0.15;
}
vr::VRProperties()->SetStringProperty( m_ulPropertyContainer, Prop_InputProfilePath_String, "{openhmd}/input/openhmd_controller_profile.json" );
int control_count;
ohmd_device_geti(device, OHMD_CONTROL_COUNT, &control_count);
if (control_count > 64)
control_count = 64;
const char* controls_fn_str[] = { "generic", "trigger", "trigger_click", "squeeze", "menu", "home",
"analog-x", "analog-y", "anlog_press", "button-a", "button-b", "button-x", "button-y",
"volume-up", "volume-down", "mic-mute"};
const char* controls_type_str[] = {"digital", "analog"};
int controls_fn[64];
int controls_types[64];
ohmd_device_geti(device, OHMD_CONTROLS_HINTS, controls_fn);
ohmd_device_geti(device, OHMD_CONTROLS_TYPES, controls_types);
for(int i = 0; i < control_count; i++){
DriverLog("%s (%s)%s\n", controls_fn_str[controls_fn[i]], controls_type_str[controls_types[i]], i == control_count - 1 ? "" : ", ");
const char *control_map = NULL, *touch_map = NULL;
EVRScalarUnits analog_type = VRScalarUnits_NormalizedOneSided;
m_buttons[i] = k_ulInvalidInputComponentHandle;
m_analogControls[i] = k_ulInvalidInputComponentHandle;
m_touchControls[i] = k_ulInvalidInputComponentHandle;
switch (controls_fn[i]) {
case OHMD_GENERIC:
control_map = "/input/generic/click";
break;
case OHMD_TRIGGER:
control_map = "/input/trigger/value";
break;
case OHMD_TRIGGER_CLICK:
control_map = "/input/trigger/click";
break;
case OHMD_SQUEEZE:
control_map = "/input/grip/value";
break;
case OHMD_MENU:
control_map = "/input/menu/click";
break;
case OHMD_HOME:
control_map = "/input/home/click";
break;
case OHMD_ANALOG_X:
control_map = "/input/joystick/x";
analog_type = VRScalarUnits_NormalizedTwoSided;
break;
case OHMD_ANALOG_Y:
control_map = "/input/joystick/y";
analog_type = VRScalarUnits_NormalizedTwoSided;
break;
case OHMD_ANALOG_PRESS:
control_map = "/input/joystick/click";
break;
case OHMD_BUTTON_A:
control_map = "/input/a/click";
touch_map = "/input/a/touch";
break;
case OHMD_BUTTON_B:
control_map = "/input/b/click";
touch_map = "/input/b/touch";
break;
case OHMD_BUTTON_X:
control_map = "/input/x/click";
touch_map = "/input/x/touch";
break;
case OHMD_BUTTON_Y:
control_map = "/input/y/click";
touch_map = "/input/y/touch";
break;
default:
break;
}
/* We fall through here for generic buttons */
if (control_map != NULL) {
if (controls_types[i] == OHMD_DIGITAL) {
vr::VRDriverInput()->CreateBooleanComponent( m_ulPropertyContainer, control_map, m_buttons + i);
}
else {
vr::VRDriverInput()->CreateScalarComponent( m_ulPropertyContainer, control_map, m_analogControls + i, VRScalarType_Absolute, analog_type);
}
}
if (touch_map != NULL) {
vr::VRDriverInput()->CreateScalarComponent( m_ulPropertyContainer, touch_map, m_touchControls + i, VRScalarType_Absolute, analog_type);
}
}
return VRInitError_None;
}
void Deactivate()
{
DriverLog("deactivate controller\n");
m_unObjectId = vr::k_unTrackedDeviceIndexInvalid;
}
void EnterStandby()
{
DriverLog("standby controller\n");
}
void *GetComponent( const char *pchComponentNameAndVersion )
{
DriverLog("get controller component %s | %s ", pchComponentNameAndVersion, /*vr::IVRControllerComponent_Version*/ "<nothing>");
if (!strcmp(pchComponentNameAndVersion, /*vr::IVRControllerComponent_Version*/ "<nothing>"))
{
DriverLog(": yes\n");
return NULL;//(vr::IVRControllerComponent*)this;
}
DriverLog(": no\n");
return NULL;
}
/** debug request from a client */
void DebugRequest( const char *pchRequest, char *pchResponseBuffer, uint32_t unResponseBufferSize )
{
if( unResponseBufferSize >= 1 )
pchResponseBuffer[0] = 0;
}
DriverPose_t GetPose()
{
pose.poseIsValid = true;
pose.result = TrackingResult_Running_OK;
pose.deviceIsConnected = true;
if (device_flags & OHMD_DEVICE_FLAGS_ROTATIONAL_TRACKING) {
float quat[4];
ohmd_device_getf(device, OHMD_ROTATION_QUAT, quat);
pose.qRotation.x = quat[0];
pose.qRotation.y = quat[1];
pose.qRotation.z = quat[2];
pose.qRotation.w = quat[3];
}
if (device_flags & OHMD_DEVICE_FLAGS_POSITIONAL_TRACKING) {
float pos[3];
ohmd_device_getf(device, OHMD_POSITION_VECTOR, pos);
pose.vecPosition[0] = pos[0];
pose.vecPosition[1] = pos[1];
pose.vecPosition[2] = pos[2];
}
// DriverLog("get controller %d pose %f %f %f %f, %f %f %f\n", index, quat[0], quat[1], quat[2], quat[3], pos[0], pos[1], pos[2]);
pose.qWorldFromDriverRotation = identityquat;
pose.qDriverFromHeadRotation = identityquat;
pose.vecDriverFromHeadTranslation[0] = 0.f;
pose.vecDriverFromHeadTranslation[1] = 0.f;
pose.vecDriverFromHeadTranslation[2] = 0.f;
return pose;
}
void RunFrame() {
vr::VRServerDriverHost()->TrackedDevicePoseUpdated(m_unObjectId, GetPose(), sizeof( DriverPose_t ) );
int control_count;
float control_state[256];
ohmd_device_geti(device, OHMD_CONTROL_COUNT, &control_count);
if (control_count > 64)
control_count = 64;
ohmd_device_getf(device, OHMD_CONTROLS_STATE, control_state);
for (int i = 0; i < control_count; i++) {
if (m_buttons[i] != k_ulInvalidInputComponentHandle) {
vr::VRDriverInput()->UpdateBooleanComponent( m_buttons[i], control_state[i] != 0, 0 );
}
else if (m_analogControls[i] != k_ulInvalidInputComponentHandle)
vr::VRDriverInput()->UpdateScalarComponent( m_analogControls[i], control_state[i], 0 );
/* If the control is not 0, mark it touched */
if (m_touchControls[i] != k_ulInvalidInputComponentHandle) {
vr::VRDriverInput()->UpdateScalarComponent( m_touchControls[i], control_state[i] == 0 ? 0.0 : 1.0, 0 );
}
}
}
VRControllerState_t controllerstate;
VRControllerState_t GetControllerState() {
DriverLog("get controller state\n");
//return controllerstate;
controllerstate.unPacketNum = controllerstate.unPacketNum + 1;
//TODO: buttons
//TODO: nolo says when a button was pressed a button was also touched. is that so?
controllerstate.ulButtonTouched |= controllerstate.ulButtonPressed;
//uint64_t ulChangedTouched = controllerstate.ulButtonTouched ^ controllerstate.ulButtonTouched;
//uint64_t ulChangedPressed = controllerstate.ulButtonPressed ^ controllerstate.ulButtonPressed;
return controllerstate;
}
bool TriggerHapticPulse( uint32_t unAxisId, uint16_t usPulseDurationMicroseconds ) {
return false;
}
std::string GetSerialNumber() const {
DriverLog("get controller serial number %s\n", m_sSerialNumber.c_str());
return m_sSerialNumber;
}
private:
std::string m_sSerialNumber = "Controller serial number " + std::to_string(index);
std::string m_sModelNumber = "Controller model number " + std::to_string(index);
vr::TrackedDeviceIndex_t m_unObjectId;
vr::PropertyContainerHandle_t m_ulPropertyContainer;
/* Generic button controls */
vr::VRInputComponentHandle_t m_buttons[64]; /* Maximum components we support */
/* Analog controls */
vr::VRInputComponentHandle_t m_analogControls[64]; /* Maximum components we support */
/* Touch controls */
vr::VRInputComponentHandle_t m_touchControls[64]; /* Maximum components we support */
};
class COpenHMDDeviceDriver final : public vr::ITrackedDeviceServerDriver, public vr::IVRDisplayComponent
{
public:
COpenHMDDeviceDriver(int hmddisplay_idx, int hmdtracker_idx)
{
hmd = ohmd_list_open_device(ctx, hmddisplay_idx);
if (hmdtracker_idx != -1 && hmdtracker_idx != hmddisplay_idx)
hmdtracker = ohmd_list_open_device(ctx, hmdtracker_idx);
else
hmdtracker = NULL;
if(!hmd){
DriverLog("failed to open device: %s\n", ohmd_ctx_get_error(ctx));
}
int ivals[2];
ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, ivals);
ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, ivals + 1);
//DriverLog("resolution: %i x %i\n", ivals[0], ivals[1]);
/*
print_infof(hmd, "hsize:", 1, OHMD_SCREEN_HORIZONTAL_SIZE);
print_infof(hmd, "vsize:", 1, OHMD_SCREEN_VERTICAL_SIZE);
print_infof(hmd, "lens separation:", 1, OHMD_LENS_HORIZONTAL_SEPARATION);
print_infof(hmd, "lens vcenter:", 1, OHMD_LENS_VERTICAL_POSITION);
print_infof(hmd, "left eye fov:", 1, OHMD_LEFT_EYE_FOV);
print_infof(hmd, "right eye fov:", 1, OHMD_RIGHT_EYE_FOV);
print_infof(hmd, "left eye aspect:", 1, OHMD_LEFT_EYE_ASPECT_RATIO);
print_infof(hmd, "right eye aspect:", 1, OHMD_RIGHT_EYE_ASPECT_RATIO);
print_infof(hmd, "distortion k:", 6, OHMD_DISTORTION_K);
print_infoi(hmd, "digital button count:", 1, OHMD_BUTTON_COUNT);
*/
m_unObjectId = vr::k_unTrackedDeviceIndexInvalid;
m_ulPropertyContainer = vr::k_ulInvalidPropertyContainer;
DriverLog( "Using settings values\n" );
ohmd_device_getf(hmd, OHMD_EYE_IPD, &m_flIPD);
{
std::stringstream buf;
buf << ohmd_list_gets(ctx, 0, OHMD_PRODUCT);
buf << ": ";
buf << ohmd_list_gets(ctx, 0, OHMD_PATH);
m_sSerialNumber = buf.str();
}
{
std::stringstream buf;
buf << "OpenHMD: ";
buf << ohmd_list_gets(ctx, 0, OHMD_PRODUCT);
m_sModelNumber = buf.str();
}
// Important to pass vendor through. Gaze cursor is only available for "Oculus". So grab the first word.
char const* vendor_override = getenv("OHMD_VENDOR_OVERRIDE");
if (vendor_override) {
m_sVendor = vendor_override;
} else {
m_sVendor = ohmd_list_gets(ctx, 0, OHMD_VENDOR);
if (m_sVendor.find(' ') != std::string::npos) {
m_sVendor = m_sVendor.substr(0, m_sVendor.find(' '));
}
}
m_nWindowX = 1920; //TODO: real window offset
m_nWindowY = 0;
ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, &m_nWindowWidth);
ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, &m_nWindowHeight );
ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, &m_nRenderWidth);
ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, &m_nRenderHeight );
//m_nRenderWidth /= 2;
//m_nRenderHeight /= 2;
m_flSecondsFromVsyncToPhotons = vr::VRSettings()->GetFloat( k_pch_Sample_Section, k_pch_Sample_SecondsFromVsyncToPhotons_Float );
//TODO: find actual frequency somehow (from openhmd?)
m_flDisplayFrequency = vr::VRSettings()->GetFloat( k_pch_Sample_Section, k_pch_Sample_DisplayFrequency_Float );
DriverLog( "driver_openhmd: Vendor: %s\n", m_sVendor.c_str() );
DriverLog( "driver_openhmd: Serial Number: %s\n", m_sSerialNumber.c_str() );
DriverLog( "driver_openhmd: Model Number: %s\n", m_sModelNumber.c_str() );
DriverLog( "driver_openhmd: Window: %d %d %d %d\n", m_nWindowX, m_nWindowY, m_nWindowWidth, m_nWindowHeight );
DriverLog( "driver_openhmd: Render Target: %d %d\n", m_nRenderWidth, m_nRenderHeight );
DriverLog( "driver_openhmd: Seconds from Vsync to Photons: %f\n", m_flSecondsFromVsyncToPhotons );
DriverLog( "driver_openhmd: Display Frequency: %f\n", m_flDisplayFrequency );
DriverLog( "driver_openhmd: IPD: %f\n", m_flIPD );
float distortion_coeffs[4];
ohmd_device_getf(hmd, OHMD_UNIVERSAL_DISTORTION_K, &(distortion_coeffs[0]));
DriverLog("driver_openhmd: Distortion values a=%f b=%f c=%f d=%f\n", distortion_coeffs[0], distortion_coeffs[1], distortion_coeffs[2], distortion_coeffs[3]);
/* Sleep for 1 second while activating to let the display connect */
std::this_thread::sleep_for( std::chrono::seconds(1) );
}
virtual ~COpenHMDDeviceDriver()
{
}
EVRInitError Activate( vr::TrackedDeviceIndex_t unObjectId )
{
m_unObjectId = unObjectId;
m_ulPropertyContainer = vr::VRProperties()->TrackedDeviceToPropertyContainer( m_unObjectId );
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, Prop_ManufacturerName_String, m_sVendor.c_str());
vr::VRProperties()->SetStringProperty( m_ulPropertyContainer, Prop_ModelNumber_String, m_sModelNumber.c_str() );
vr::VRProperties()->SetStringProperty( m_ulPropertyContainer, Prop_RenderModelName_String, m_sModelNumber.c_str() );
vr::VRProperties()->SetFloatProperty( m_ulPropertyContainer, Prop_UserIpdMeters_Float, m_flIPD );
vr::VRProperties()->SetFloatProperty( m_ulPropertyContainer, Prop_UserHeadToEyeDepthMeters_Float, 0.f );
vr::VRProperties()->SetFloatProperty( m_ulPropertyContainer, Prop_DisplayFrequency_Float, m_flDisplayFrequency );
vr::VRProperties()->SetFloatProperty( m_ulPropertyContainer, Prop_SecondsFromVsyncToPhotons_Float, m_flSecondsFromVsyncToPhotons );
//float sep;
// return a constant that's not 0 (invalid) or 1 (reserved for Oculus)
vr::VRProperties()->SetUint64Property( m_ulPropertyContainer, Prop_CurrentUniverseId_Uint64, 2 );
return VRInitError_None;
}
void Deactivate()
{
m_unObjectId = vr::k_unTrackedDeviceIndexInvalid;
}
void EnterStandby()
{
}
void *GetComponent( const char *pchComponentNameAndVersion )
{
if ( !strcmp( pchComponentNameAndVersion, vr::IVRDisplayComponent_Version ) )
{
return (vr::IVRDisplayComponent*)this;
}
return NULL;
}
void PowerOff()
{
}
void DebugRequest( const char *pchRequest, char *pchResponseBuffer, uint32_t unResponseBufferSize )
{
if( unResponseBufferSize >= 1 )
pchResponseBuffer[0] = 0;
}
void GetWindowBounds( int32_t *pnX, int32_t *pnY, uint32_t *pnWidth, uint32_t *pnHeight )
{
*pnX = m_nWindowX;
*pnY = m_nWindowY;
*pnWidth = m_nWindowWidth;
*pnHeight = m_nWindowHeight;
}
bool IsDisplayOnDesktop()
{
return true;
}
bool IsDisplayRealDisplay()
{
return true;
}
void GetRecommendedRenderTargetSize( uint32_t *pnWidth, uint32_t *pnHeight )
{
*pnWidth = m_nRenderWidth;
*pnHeight = m_nRenderHeight;
}
void GetEyeOutputViewport( EVREye eEye, uint32_t *pnX, uint32_t *pnY, uint32_t *pnWidth, uint32_t *pnHeight )
{
*pnY = 0;
*pnWidth = m_nWindowWidth / 2;
*pnHeight = m_nWindowHeight;
if ( eEye == Eye_Left )
{
*pnX = 0;
}
else
{
*pnX = m_nWindowWidth / 2;
}
}
// flatten 2D indices in a 4x4 matrix explicit so it's easy to see what's happening:
int f1(int i, int j)
{
if (i == 0 && j == 0) return 0;
if (i == 0 && j == 1) return 1;
if (i == 0 && j == 2) return 2;
if (i == 0 && j == 3) return 3;
if (i == 1 && j == 0) return 4;
if (i == 1 && j == 1) return 5;
if (i == 1 && j == 2) return 6;
if (i == 1 && j == 3) return 7;
if (i == 2 && j == 0) return 8;
if (i == 2 && j == 1) return 9;
if (i == 2 && j == 2) return 10;
if (i == 2 && j == 3) return 11;
if (i == 3 && j == 0) return 12;
if (i == 3 && j == 1) return 13;
if (i == 3 && j == 2) return 14;
if (i == 3 && j == 3) return 15;
return -1;
}
int f2(int i, int j)
{
if (i == 0 && j == 0) return 0;
if (i == 0 && j == 1) return 4;
if (i == 0 && j == 2) return 8;
if (i == 0 && j == 3) return 12;
if (i == 1 && j == 0) return 1;
if (i == 1 && j == 1) return 5;
if (i == 1 && j == 2) return 9;
if (i == 1 && j == 3) return 13;
if (i == 2 && j == 0) return 2;
if (i == 2 && j == 1) return 6;
if (i == 2 && j == 2) return 10;
if (i == 2 && j == 3) return 14;
if (i == 3 && j == 0) return 3;
if (i == 3 && j == 1) return 7;
if (i == 3 && j == 2) return 11;
if (i == 3 && j == 3) return 15;
return -1;
}
void columnMatrixToAngles(float *yaw, float *pitch, float *roll, float colMatrix[4][4] ) {
double sinPitch, cosPitch, sinRoll, cosRoll, sinYaw, cosYaw;
sinPitch = -colMatrix[2][0];
cosPitch = sqrt(1 - sinPitch*sinPitch);
if ( abs(cosPitch) > 0.1)
{
sinRoll = colMatrix[2][1] / cosPitch;
cosRoll = colMatrix[2][2] / cosPitch;
sinYaw = colMatrix[1][0] / cosPitch;
cosYaw = colMatrix[0][0] / cosPitch;
}
else
{
sinRoll = -colMatrix[1][2];
cosRoll = colMatrix[1][1];
sinYaw = 0;
cosYaw = 1;
}
*yaw = atan2(sinYaw, cosYaw) * 180 / M_PI;
*pitch = atan2(sinPitch, cosPitch) * 180 / M_PI;
*roll = atan2(sinRoll, cosRoll) * 180 / M_PI;
}
typedef union {
float m[4][4];
float arr[16];
} mat4x4f;
void omat4x4f_mult(const mat4x4f* l, const mat4x4f* r, mat4x4f *o) {
for(int i = 0; i < 4; i++){
float a0 = l->m[i][0], a1 = l->m[i][1], a2 = l->m[i][2], a3 = l->m[i][3];
o->m[i][0] = a0 * r->m[0][0] + a1 * r->m[1][0] + a2 * r->m[2][0] + a3 * r->m[3][0];
o->m[i][1] = a0 * r->m[0][1] + a1 * r->m[1][1] + a2 * r->m[2][1] + a3 * r->m[3][1];
o->m[i][2] = a0 * r->m[0][2] + a1 * r->m[1][2] + a2 * r->m[2][2] + a3 * r->m[3][2];
o->m[i][3] = a0 * r->m[0][3] + a1 * r->m[1][3] + a2 * r->m[2][3] + a3 * r->m[3][3];
}
}
void createUnRotation(float angle, mat4x4f *m) {
memset(m, 0, sizeof(*m));
m->m[0][0] = 1.0f;
m->m[1][1] = 1.0f;
m->m[2][2] = 1.0f;
m->m[3][3] = 1.0f;
DriverLog("Unrotating for angle %f\n", angle);
if (angle > -5 && angle < 5) {
return;
}
else if (angle > 85 && angle < 95) {
m->m[0][0] = 0.0f; m->m[0][1] = -1.0f; m->m[1][0] = 1.0f; m->m[1][1] = 0.0f;
m->m[0][1] = 1.0f; m->m[1][0] = -1.0f;
}
else if (angle > -95 && angle < -85) {
m->m[0][0] = 0.0f; m->m[0][1] = -1.0f; m->m[1][0] = 1.0f; m->m[1][1] = 0.0f;
}
else {
DriverLog("UNIMPLEMENTED ROTATION!!!\n");
}
}
void GetProjectionRaw( EVREye eEye, float *pfLeft, float *pfRight, float *pfTop, float *pfBottom )
{
mat4x4f ohmdprojection;
if (eEye == Eye_Left) {
ohmd_device_getf(hmd, OHMD_LEFT_EYE_GL_PROJECTION_MATRIX, ohmdprojection.arr);
} else {
ohmd_device_getf(hmd, OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX, ohmdprojection.arr);
}
float yaw, pitch, roll;
float p[4][4];
memcpy(p, ohmdprojection.arr, 16 * sizeof(float));
columnMatrixToAngles(&yaw, &pitch, &roll, p);
if (eEye == Eye_Left) {
rotation_left = yaw;
} else {
rotation_right = yaw;
}
mat4x4f unrotation;
createUnRotation(yaw, &unrotation);
DriverLog("unrotation\n%f %f %f %f\n%f %f %f %f %f\n%f %f %f %f\n%f %f %f %f\n",
unrotation.arr[0], unrotation.arr[1], unrotation.arr[2], unrotation.arr[3],
unrotation.arr[4], unrotation.arr[5], unrotation.arr[6], unrotation.arr[7],
unrotation.arr[8], unrotation.arr[9], unrotation.arr[10], unrotation.arr[11],
unrotation.arr[12], unrotation.arr[13], unrotation.arr[14], unrotation.arr[15]);
omat4x4f_mult(&ohmdprojection, &unrotation, &ohmdprojection);
// http://stackoverflow.com/questions/10830293/ddg#12926655
// get projection matrix from openhmd, convert it into lrtb + near,far with SO formula
// then divide by near plane distance to get the tangents of the angles from the center plane (tan = opposite side = these values divided by adjacent side = near plane distance)
// but negate top and bottom. who knows why. there are 3 or so issues for it on github
// f2 switches row-major and column-major
float m00 = ohmdprojection.arr[f2(0,0)];
//float m03 = ohmdprojection[f2(0,3)];
//float m10 = ohmdprojection[f2(1,3)];
float m11 = ohmdprojection.arr[f2(1,1)];
//float m13 = ohmdprojection[f2(1,3)];
float m23 = ohmdprojection.arr[f2(2,3)];
float m22 = ohmdprojection.arr[f2(2,2)];
float m12 = ohmdprojection.arr[f2(1,2)];
float m02 = ohmdprojection.arr[f2(0,2)];
float near = m23/(m22-1);
float far = m23/(m22+1);
*pfBottom = - (m12-1)/m11;
*pfTop = - (m12+1)/m11;
*pfLeft = (m02-1)/m00;
*pfRight = (m02+1)/m00;
DriverLog("m 00 %f, 11 %f, 22 %f, 12 %f, 02 %f\n", m00, m11, m23, m22, m12, m02);
DriverLog("ohmd projection\n%f %f %f %f\n%f %f %f %f %f\n%f %f %f %f\n%f %f %f %f\n",
ohmdprojection.arr[0], ohmdprojection.arr[1], ohmdprojection.arr[2], ohmdprojection.arr[3],
ohmdprojection.arr[4], ohmdprojection.arr[5], ohmdprojection.arr[6], ohmdprojection.arr[7],
ohmdprojection.arr[8], ohmdprojection.arr[9], ohmdprojection.arr[10], ohmdprojection.arr[11],
ohmdprojection.arr[12], ohmdprojection.arr[13], ohmdprojection.arr[14], ohmdprojection.arr[15]
);
DriverLog("projectionraw values lrtb, near far: %f %f %f %f | %f %f\n", *pfLeft, *pfRight, *pfTop, *pfBottom, near, far);
//DriverLog("angles %f %f %f\n", yaw, pitch, roll);
}
DistortionCoordinates_t ComputeDistortion( EVREye eEye, float fU, float fV )
{
float angle = (eEye == Eye_Left ? rotation_left : rotation_right);
//DriverLog("Eye %d before: %f %f\n", eEye, fU, fV);
if (angle > -5 && angle < 5) {
} else if (angle > 85 && angle < 95) {
float tmp = fV;
fV = 1. - fU;
fU = tmp;
} else if (angle > -95 && angle < -85) {
float tmp = fV;
fV = fU;
fU = 1.f - tmp;
} else {
float x = 0 * fU + -1 * fV;
float y = -1 * fU + 0 * 0 * fV;
fU = x;
fV = y;
}
//DriverLog("Eye %d after: %f %f\n", eEye, fU, fV);
int hmd_w;
int hmd_h;
ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, &hmd_w);
ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, &hmd_h);
float ipd;
ohmd_device_getf(hmd, OHMD_EYE_IPD, &ipd);
float viewport_scale[2];
float distortion_coeffs[4];
float aberr_scale[3];
float sep;
float left_lens_center[2];
float right_lens_center[2];
//viewport is half the screen
ohmd_device_getf(hmd, OHMD_SCREEN_HORIZONTAL_SIZE, &(viewport_scale[0]));
viewport_scale[0] /= 2.0f;
ohmd_device_getf(hmd, OHMD_SCREEN_VERTICAL_SIZE, &(viewport_scale[1]));
//distortion coefficients
ohmd_device_getf(hmd, OHMD_UNIVERSAL_DISTORTION_K, &(distortion_coeffs[0]));
ohmd_device_getf(hmd, OHMD_UNIVERSAL_ABERRATION_K, &(aberr_scale[0]));
//calculate lens centers (assuming the eye separation is the distance betweenteh lense centers)
ohmd_device_getf(hmd, OHMD_LENS_HORIZONTAL_SEPARATION, &sep);
ohmd_device_getf(hmd, OHMD_LENS_VERTICAL_POSITION, &(left_lens_center[1]));
ohmd_device_getf(hmd, OHMD_LENS_VERTICAL_POSITION, &(right_lens_center[1]));
left_lens_center[0] = viewport_scale[0] - sep/2.0f;
right_lens_center[0] = sep/2.0f;
//asume calibration was for lens view to which ever edge of screen is further away from lens center
float warp_scale = (left_lens_center[0] > right_lens_center[0]) ? left_lens_center[0] : right_lens_center[0];
float lens_center[2];
lens_center[0] = (eEye == Eye_Left ? left_lens_center[0] : right_lens_center[0]);
lens_center[1] = (eEye == Eye_Left ? left_lens_center[1] : right_lens_center[1]);
float r[2];
r[0] = fU * viewport_scale[0] - lens_center[0];
r[1] = fV * viewport_scale[1] - lens_center[1];
r[0] /= warp_scale;
r[1] /= warp_scale;
float r_mag = sqrt(r[0] * r[0] + r[1] * r[1]);
float r_displaced[2];
r_displaced[0] = r[0] * (distortion_coeffs[3] + distortion_coeffs[2] * r_mag + distortion_coeffs[1] * r_mag * r_mag + distortion_coeffs[0] * r_mag * r_mag * r_mag);
r_displaced[1] = r[1] * (distortion_coeffs[3] + distortion_coeffs[2] * r_mag + distortion_coeffs[1] * r_mag * r_mag + distortion_coeffs[0] * r_mag * r_mag * r_mag);
r_displaced[0] *= warp_scale;
r_displaced[1] *= warp_scale;
float tc_r[2];
tc_r[0] = (lens_center[0] + aberr_scale[0] * r_displaced[0]) / viewport_scale[0];
tc_r[1] = (lens_center[1] + aberr_scale[0] * r_displaced[1]) / viewport_scale[1];
float tc_g[2];
tc_g[0] = (lens_center[0] + aberr_scale[1] * r_displaced[0]) / viewport_scale[0];
tc_g[1] = (lens_center[1] + aberr_scale[1] * r_displaced[1]) / viewport_scale[1];
float tc_b[2];
tc_b[0] = (lens_center[0] + aberr_scale[2] * r_displaced[0]) / viewport_scale[0];
tc_b[1] = (lens_center[1] + aberr_scale[2] * r_displaced[1]) / viewport_scale[1];
//DriverLog("Distort %f %f -> %f %f; %f %f %f %f\n", fU, fV, tc_b[0], tc_b[1], distortion_coeffs[0], distortion_coeffs[1], distortion_coeffs[2], distortion_coeffs[3]);
DistortionCoordinates_t coordinates;
coordinates.rfBlue[0] = tc_b[0];
coordinates.rfBlue[1] = tc_b[1];
coordinates.rfGreen[0] = tc_g[0];
coordinates.rfGreen[1] = tc_g[1];
coordinates.rfRed[0] = tc_r[0];
coordinates.rfRed[1] = tc_r[1];
return coordinates;
}
DriverPose_t GetPose()
{
DriverPose_t pose = { 0 };
pose.poseIsValid = true;
pose.result = TrackingResult_Running_OK;
pose.deviceIsConnected = true;
ohmd_device* d = hmdtracker ? hmdtracker : hmd;
float quat[4];
ohmd_device_getf(d, OHMD_ROTATION_QUAT, quat);
pose.qRotation.x = quat[0];
pose.qRotation.y = quat[1];
pose.qRotation.z = quat[2];
pose.qRotation.w = quat[3];
float pos[3];
ohmd_device_getf(d, OHMD_POSITION_VECTOR, pos);
pose.vecPosition[0] = pos[0];
pose.vecPosition[1] = pos[1];
pose.vecPosition[2] = pos[2];
//printf("%f %f %f %f %f %f %f\n", quat[0], quat[1], quat[2], quat[3], pos[0], pos[1], pos[2]);
//fflush(stdout);
//DriverLog("get hmd pose %f %f %f %f, %f %f %f\n", quat[0], quat[1], quat[2], quat[3], pos[0], pos[1], pos[2]);
pose.qWorldFromDriverRotation = identityquat;
pose.qDriverFromHeadRotation = identityquat;
return pose;
}
void RunFrame()
{
// In a real driver, this should happen from some pose tracking thread.
// The RunFrame interval is unspecified and can be very irregular if some other
// driver blocks it for some periodic task.
if ( m_unObjectId != vr::k_unTrackedDeviceIndexInvalid )
{
vr::VRServerDriverHost()->TrackedDevicePoseUpdated( m_unObjectId, GetPose(), sizeof( DriverPose_t ) );
}
}
std::string GetSerialNumber() const { return m_sSerialNumber; }
private:
ohmd_device* hmd = NULL;
ohmd_device* hmdtracker = NULL;
vr::TrackedDeviceIndex_t m_unObjectId;
vr::PropertyContainerHandle_t m_ulPropertyContainer;
std::string m_sVendor;
std::string m_sSerialNumber;
std::string m_sModelNumber;
int32_t m_nWindowX;
int32_t m_nWindowY;
int32_t m_nWindowWidth;
int32_t m_nWindowHeight;
int32_t m_nRenderWidth;
int32_t m_nRenderHeight;
float m_flSecondsFromVsyncToPhotons;
float m_flDisplayFrequency;
float m_flIPD;
float rotation_left = 0.0;
float rotation_right = 0.0;
};
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CServerDriver_OpenHMD: public IServerTrackedDeviceProvider
{
public:
CServerDriver_OpenHMD()
: m_OpenHMDDeviceDriver( NULL )