-
Notifications
You must be signed in to change notification settings - Fork 1
/
openvr_openhmd.h
1439 lines (1144 loc) · 49.1 KB
/
openvr_openhmd.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
#include <openhmd.h>
namespace vr {
ohmd_context* ctx;
ohmd_device* hmd;
int openhmd_w;
int openhmd_h;
// 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[len];
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[len];
ohmd_device_geti(hmd, val, iv);
printf("%-25s", name);
for(int i = 0; i < len; i++)
printf("%d ", iv[i]);
printf("\n");
}
void checkSDLError(int line = -1)
{
const char *error = SDL_GetError();
if (*error != '\0')
{
printf("SDL Error: %s\n", error);
if (line != -1)
printf(" + line: %i\n", line);
SDL_ClearError();
}
}
class OpenHMDRenderModels : public IVRRenderModels {
EVRRenderModelError LoadRenderModel_Async( const char *pchRenderModelName, RenderModel_t **ppRenderModel ) {
printf("rendermodel async load requested: %s\n", pchRenderModelName);
//TODO: load
return VRRenderModelError_None;
}
void FreeRenderModel( RenderModel_t *pRenderModel ) {
printf("free render model\n");
//TODO:
}
/** Loads and returns a texture for use in the application. */
EVRRenderModelError LoadTexture_Async( TextureID_t textureId, RenderModel_TextureMap_t **ppTexture ) {
printf("load texture async requested: %d\n", textureId);
//TODO:
return VRRenderModelError_None;
}
void FreeTexture( RenderModel_TextureMap_t *pTexture ) {
printf("free texture requested\n");
//TODO:
}
EVRRenderModelError LoadTextureD3D11_Async( TextureID_t textureId, void *pD3D11Device, void **ppD3D11Texture2D ) {
printf("\n");
//TODO:
return VRRenderModelError_None;
}
EVRRenderModelError LoadIntoTextureD3D11_Async( TextureID_t textureId, void *pDstTexture ) {
printf("\n");
//TODO:
return VRRenderModelError_None;
}
void FreeTextureD3D11( void *pD3D11Texture2D ) {
printf("\n");
//TODO:
}
uint32_t GetRenderModelName( uint32_t unRenderModelIndex, VR_OUT_STRING() char *pchRenderModelName, uint32_t unRenderModelNameLen ) {
printf("get render model name for %d\n", unRenderModelIndex);
//TODO:
char* name = (char*) "OpenHMDModel";
*pchRenderModelName = *name;
return 0;
}
uint32_t GetRenderModelCount() {
printf("get render model count\n");
//TODO:
return 1;
}
uint32_t GetComponentCount( const char *pchRenderModelName ) {
printf("get component count, 0 not supported\n");
//TODO:
return 0;
}
uint32_t GetComponentName( const char *pchRenderModelName, uint32_t unComponentIndex, VR_OUT_STRING( ) char *pchComponentName, uint32_t unComponentNameLen ) {
printf("get component name for %s\n", pchRenderModelName);
//TODO: shouldn't happen with componentcount 0
return 0;
}
uint64_t GetComponentButtonMask( const char *pchRenderModelName, const char *pchComponentName ) {
printf("component button mask\n");
//TODO:
return 0;
}
uint32_t GetComponentRenderModelName( const char *pchRenderModelName, const char *pchComponentName, VR_OUT_STRING( ) char *pchComponentRenderModelName, uint32_t unComponentRenderModelNameLen ) {
printf("component render model name\n");
//TODO:
return 0;
}
bool GetComponentState( const char *pchRenderModelName, const char *pchComponentName, const vr::VRControllerState_t *pControllerState, const RenderModel_ControllerMode_State_t *pState, RenderModel_ComponentState_t *pComponentState ) {
printf("get component state\n");
//TODO:
return false;
}
bool RenderModelHasComponent( const char *pchRenderModelName, const char *pchComponentName ) {
printf("render model has component %s: %s\n", pchRenderModelName, pchComponentName);
//TODO:
return false;
}
uint32_t GetRenderModelThumbnailURL( const char *pchRenderModelName, VR_OUT_STRING() char *pchThumbnailURL, uint32_t unThumbnailURLLen, vr::EVRRenderModelError *peError ) {
printf("thumbnail for %s\n", pchRenderModelName);
//TODO:
return 0;
}
uint32_t GetRenderModelOriginalPath( const char *pchRenderModelName, VR_OUT_STRING() char *pchOriginalPath, uint32_t unOriginalPathLen, vr::EVRRenderModelError *peError ) {
printf("render model original path for %s\n", pchRenderModelName);
//TODO:
return 0;
}
const char *GetRenderModelErrorNameFromEnum( vr::EVRRenderModelError error ) {
printf("string for %d\n", error);
//TODO:
return "error" ;
}
};
class OpenHMDIVRSystem : public IVRSystem
{
public:
OpenHMDIVRSystem() {
printf("initinternal create openhmd context\n");
ctx = ohmd_ctx_create();
// Probe for devices
int num_devices = ohmd_ctx_probe(ctx);
if(num_devices < 0){
printf("failed to probe devices: %s\n", ohmd_ctx_get_error(ctx));
ohmd_ctx_destroy(ctx);
return;
}
printf("num devices: %d\n\n", num_devices);
// Print device information for default device (change i < num_devices for all)
for(int i = 0; i < 1; i++){
printf("device %d\n", i);
printf(" vendor: %s\n", ohmd_list_gets(ctx, i, OHMD_VENDOR));
printf(" product: %s\n", ohmd_list_gets(ctx, i, OHMD_PRODUCT));
printf(" path: %s\n\n", ohmd_list_gets(ctx, i, OHMD_PATH));
}
hmd = ohmd_list_open_device(ctx, 0); // default device (0)
if(!hmd){
printf("failed to open device: %s\n", ohmd_ctx_get_error(ctx));
ohmd_ctx_destroy(ctx);
return;
}
// Print hardware information for the opened device
int ivals[2];
ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, ivals);
ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, ivals + 1);
printf("resolution: %i x %i\n", ivals[0], ivals[1]);
if (fulldbg) {
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);
}
ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, &openhmd_w);
ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, &openhmd_h);
}
void GetRecommendedRenderTargetSize( uint32_t *pnWidth, uint32_t *pnHeight ) {
//TODO:
*pnWidth = openhmd_w;
*pnHeight = openhmd_h;
printf("recommended render target size: %dx%d\n", *pnWidth, *pnHeight);
}
HmdMatrix44_t GetProjectionMatrix( EVREye eEye, float fNearZ, float fFarZ ) {
printf("projection matrix for eye: ");
if (eEye == EVREye::Eye_Left) {
printf("left ");
} else {
printf("right ");
}
float projection[16];
if (eEye == EVREye::Eye_Left) {
ohmd_device_getf(hmd, OHMD_LEFT_EYE_GL_PROJECTION_MATRIX, projection);
} else {
ohmd_device_getf(hmd, OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX, projection);
}
//printf("%s\n", projection[0]);
HmdMatrix44_t matrix;
matrix.m[0][0] = projection[0];
matrix.m[0][1] = projection[4];
matrix.m[0][2] = projection[8];
matrix.m[0][3] = projection[12];
matrix.m[1][0] = projection[1];
matrix.m[1][1] = projection[5];
matrix.m[1][2] = projection[9];
matrix.m[1][3] = projection[13];
matrix.m[2][0] = projection[2];
matrix.m[2][1] = projection[6];
matrix.m[2][2] = projection[10];
matrix.m[2][3] = projection[14];
matrix.m[3][0] = projection[3];
matrix.m[3][1] = projection[7];
matrix.m[3][2] = projection[11];
matrix.m[3][3] = projection[15];
return matrix;
}
void GetProjectionRaw( EVREye eEye, float *pfLeft, float *pfRight, float *pfTop, float *pfBottom ) {
printf("projection raw\n");
//TODO:
}
bool ComputeDistortion( EVREye eEye, float fU, float fV, DistortionCoordinates_t *pDistortionCoordinates ) {
//printf("\n");
//TODO:
return true;
}
HmdMatrix34_t GetEyeToHeadTransform( EVREye eEye ) {
printf("eye to head transform for eye: ");
if (eEye == EVREye::Eye_Left) {
printf("left\n");
} else {
printf("right\n");
}
//TODO:
glm::mat4 Projection = glm::mat4(1.0);
//std::cout << glm::to_string(Projection) << std::endl;
HmdMatrix34_t matrix;
matrix.m[0][0] = Projection[0][0];
matrix.m[0][1] = Projection[0][1];
matrix.m[0][2] = Projection[0][2];
matrix.m[0][3] = Projection[0][3];
matrix.m[1][0] = Projection[1][0];
matrix.m[1][1] = Projection[1][1];
matrix.m[1][2] = Projection[1][2];
matrix.m[1][3] = Projection[1][3];
matrix.m[2][0] = Projection[2][0];
matrix.m[2][1] = Projection[2][1];
matrix.m[2][2] = Projection[2][2];
matrix.m[2][3] = Projection[2][3];
return matrix;
}
bool GetTimeSinceLastVsync( float *pfSecondsSinceLastVsync, uint64_t *pulFrameCounter ) {
//printf("\n");
//TODO:
pfSecondsSinceLastVsync = 0;
pulFrameCounter = 0;
return false;
}
int32_t GetD3D9AdapterIndex() {
return 0;
}
void GetDXGIOutputInfo( int32_t *pnAdapterIndex ) {
}
void GetOutputDevice( uint64_t *pnDevice, ETextureType textureType ) {
printf("output device\n");
//TODO:
*pnDevice = 0;
}
bool IsDisplayOnDesktop() {
printf("extended mode: \n");
//TODO:
return true;
}
bool SetDisplayVisibility( bool bIsVisibleOnDesktop ) {
return true;
}
void GetDeviceToAbsoluteTrackingPose( ETrackingUniverseOrigin eOrigin, float fPredictedSecondsToPhotonsFromNow, VR_ARRAY_COUNT(unTrackedDevicePoseArrayCount) TrackedDevicePose_t *pTrackedDevicePoseArray, uint32_t unTrackedDevicePoseArrayCount ) {
printf("device to abs tracking pose\n");
//TODO:
}
void ResetSeatedZeroPose() {
printf("reset seated zero pose\n");
//TODO:
}
HmdMatrix34_t GetSeatedZeroPoseToStandingAbsoluteTrackingPose() {
printf("get seated zero to standing absolute\n");
//TODO:
HmdMatrix34_t matrix;
return matrix;
}
HmdMatrix34_t GetRawZeroPoseToStandingAbsoluteTrackingPose() {
printf("get rar zero to standing absolute\n");
//TODO:
HmdMatrix34_t matrix;
return matrix;
}
uint32_t GetSortedTrackedDeviceIndicesOfClass( ETrackedDeviceClass eTrackedDeviceClass, VR_ARRAY_COUNT(unTrackedDeviceIndexArrayCount) vr::TrackedDeviceIndex_t *punTrackedDeviceIndexArray, uint32_t unTrackedDeviceIndexArrayCount, vr::TrackedDeviceIndex_t unRelativeToTrackedDeviceIndex = k_unTrackedDeviceIndex_Hmd ) {
printf("get sorted tracked device indices\n");
//TODO:
return 0;
}
EDeviceActivityLevel GetTrackedDeviceActivityLevel( vr::TrackedDeviceIndex_t unDeviceId ) {
printf("tracked device activity for %d: ", unDeviceId);
if (unDeviceId == 0) {
printf("hmd active\n");
return EDeviceActivityLevel::k_EDeviceActivityLevel_UserInteraction;
}
printf("tracked device idle\n");
//TODO:
return EDeviceActivityLevel::k_EDeviceActivityLevel_Unknown;
}
void ApplyTransform( TrackedDevicePose_t *pOutputPose, const TrackedDevicePose_t *pTrackedDevicePose, const HmdMatrix34_t *pTransform ) {
printf("apply transform\n");
//TODO:
}
vr::TrackedDeviceIndex_t GetTrackedDeviceIndexForControllerRole( vr::ETrackedControllerRole unDeviceType ) {
printf("device for role %d\n", unDeviceType);
if (unDeviceType == ETrackedControllerRole::TrackedControllerRole_Invalid) {
printf("invalid\n");
} else if (unDeviceType == ETrackedControllerRole::TrackedControllerRole_LeftHand) {
printf("left hand\n");
} else {
printf("right hand\n");
}
//TODO:
return 0;
}
vr::ETrackedControllerRole GetControllerRoleForTrackedDeviceIndex( vr::TrackedDeviceIndex_t unDeviceIndex ) {
printf("controller role for %d\n", unDeviceIndex);
//TODO:
return ETrackedControllerRole::TrackedControllerRole_Invalid;
}
ETrackedDeviceClass GetTrackedDeviceClass( vr::TrackedDeviceIndex_t unDeviceIndex ) {
printf("get device class for %d: ", unDeviceIndex);
if (unDeviceIndex == 0) {
printf("HMD\n");
return ETrackedDeviceClass::TrackedDeviceClass_HMD;
} else {
printf("invalid\n");
//TODO:
return ETrackedDeviceClass::TrackedDeviceClass_Invalid;
}
}
bool IsTrackedDeviceConnected( vr::TrackedDeviceIndex_t unDeviceIndex ) {
if (fulldbg) printf("is device connected %d\n", unDeviceIndex);
if (unDeviceIndex == 0) {
printf("hmd connected\n");
return true;
}
//TODO:
return false;
}
bool GetBoolTrackedDeviceProperty( vr::TrackedDeviceIndex_t unDeviceIndex, ETrackedDeviceProperty prop, ETrackedPropertyError *pError = 0L ) {
//printf("bool tracked property\n");
//TODO:
return false;
}
float GetFloatTrackedDeviceProperty( vr::TrackedDeviceIndex_t unDeviceIndex, ETrackedDeviceProperty prop, ETrackedPropertyError *pError = 0L ) {
//printf("\n");
//TODO:
return false;
}
int32_t GetInt32TrackedDeviceProperty( vr::TrackedDeviceIndex_t unDeviceIndex, ETrackedDeviceProperty prop, ETrackedPropertyError *pError = 0L ) {
return 0;
}
uint64_t GetUint64TrackedDeviceProperty( vr::TrackedDeviceIndex_t unDeviceIndex, ETrackedDeviceProperty prop, ETrackedPropertyError *pError = 0L ) {
return 0;
}
HmdMatrix34_t GetMatrix34TrackedDeviceProperty( vr::TrackedDeviceIndex_t unDeviceIndex, ETrackedDeviceProperty prop, ETrackedPropertyError *pError = 0L ) {
printf("get matrix tracked device property");
HmdMatrix34_t matrix;
return matrix;
}
uint32_t GetArrayTrackedDeviceProperty( vr::TrackedDeviceIndex_t unDeviceIndex, ETrackedDeviceProperty prop, PropertyTypeTag_t propType, void *pBuffer, uint32_t unBufferSize, ETrackedPropertyError *pError = 0L ) {
printf("GetArrayTrackedDeviceProperty\n");
return 0;
}
uint32_t GetStringTrackedDeviceProperty( vr::TrackedDeviceIndex_t unDeviceIndex, ETrackedDeviceProperty prop, VR_OUT_STRING() char *pchValue, uint32_t unBufferSize, ETrackedPropertyError *pError = 0L ) {
const char *str = [&] {
if (prop == Prop_TrackingSystemName_String) {
return "OpenHMD";
} else if (prop == Prop_SerialNumber_String) {
return ohmd_list_gets(ctx, 0, OHMD_PRODUCT);
} else {
printf("requested not implemented %d prop\n", prop);
return "Not implemented";
}
}();
if (unBufferSize == 0) {
return strlen(str) + 1;
} else {
printf("get prop %d for %d: %s (%lu)\n", prop, unDeviceIndex, str, strlen(str) + 1);
strncpy(pchValue, str, strlen(str) + 1);
}
return sizeof("OpenHMD Test Display");
}
const char *GetPropErrorNameFromEnum( ETrackedPropertyError error ) {
printf("error %d\n", error);
//TODO:
return "error";
}
bool PollNextEvent( VREvent_t *pEvent, uint32_t uncbVREvent ) {
//printf("\n");
//TODO:
return false;
}
bool PollNextEventWithPose( ETrackingUniverseOrigin eOrigin, VREvent_t *pEvent, uint32_t uncbVREvent, vr::TrackedDevicePose_t *pTrackedDevicePose ) {
//printf("\n");
//TODO:
return false;
}
const char *GetEventTypeNameFromEnum( EVREventType eType ) {
printf("event type from enum %d\n", eType);
//TODO:
return "event";
}
HiddenAreaMesh_t GetHiddenAreaMesh( EVREye eEye, EHiddenAreaMeshType type = k_eHiddenAreaMesh_Standard ) {
printf("hidden area mesh\n");
//TODO:
}
bool GetControllerState( vr::TrackedDeviceIndex_t unControllerDeviceIndex, vr::VRControllerState_t *pControllerState, uint32_t unControllerStateSize ) {
if (fulldbg) printf("get controller state for %d\n", unControllerDeviceIndex);
//TODO:
if (unControllerDeviceIndex == 0) {
pControllerState->unPacketNum++; //TODO
return true;
}
return false;
}
bool GetControllerStateWithPose( ETrackingUniverseOrigin eOrigin, vr::TrackedDeviceIndex_t unControllerDeviceIndex, vr::VRControllerState_t *pControllerState, uint32_t unControllerStateSize, TrackedDevicePose_t *pTrackedDevicePose ) {
//printf("\n");
//TODO:
return false;
}
void TriggerHapticPulse( vr::TrackedDeviceIndex_t unControllerDeviceIndex, uint32_t unAxisId, unsigned short usDurationMicroSec ) {
printf("trigger haptic pulse of duration %f ms\n", usDurationMicroSec / 1000.);
//TODO:
}
const char *GetButtonIdNameFromEnum( EVRButtonId eButtonId ) {
printf("button id name from enum %d\n", eButtonId);
//TODO:
return "buttonid";
}
const char *GetControllerAxisTypeNameFromEnum( EVRControllerAxisType eAxisType ) {
printf("controller axis name from enum %d\n", eAxisType);
//TODO:
return "axis";
}
bool IsInputAvailable() {
return true;
}
bool IsSteamVRDrawingControllers() {
return false;
}
bool ShouldApplicationPause() {
return false;
}
bool ShouldApplicationReduceRenderingWork() {
return false;
}
bool CaptureInputFocus() {
printf("capture input focus \n");
//TODO:
return true;
}
void ReleaseInputFocus() {
printf("release input focus\n");
//TODO:
}
bool IsInputFocusCapturedByAnotherProcess() {
if (fulldbg) printf("is input focus captured by other process?\n");
//TODO:
return false;
}
uint32_t DriverDebugRequest( vr::TrackedDeviceIndex_t unDeviceIndex, const char *pchRequest, char *pchResponseBuffer, uint32_t unResponseBufferSize ) {
printf("debug request\n");
//TODO:
return 0;
}
vr::EVRFirmwareError PerformFirmwareUpdate( vr::TrackedDeviceIndex_t unDeviceIndex ) {
printf("firmware update\n");
//TODO:
return EVRFirmwareError::VRFirmwareError_None;
}
void AcknowledgeQuit_Exiting() {
printf("acknowledge quit\n");
//TODO:
}
void AcknowledgeQuit_UserPrompt() {
printf("acknowledge quit prompt\n");
//TODO:
}
void GetOutputDevice( uint64_t *pnDevice, ETextureType textureType, VkInstance_T *pInstance = nullptr ) {
printf("getoutputdevice\n");
}
};
class OpenHMDCompositor : public IVRCompositor
{
private:
SDL_Window *compositorwindow = NULL;
SDL_Window *clientwindow = NULL;
SDL_GLContext clientcontext;
SDL_GLContext compositorcontext;
SDL_Renderer *renderer;
int eyew;
SDL_Texture *texture;
GLuint shader_program;
GLint texture_location;
GLuint vaol, vbol, vaor, vbor;
void mkvaovbo(bool leftEye) {
if (leftEye) {
glGenVertexArrays(1, &vaol);
glBindVertexArray(vaol);
glGenBuffers(1, &vbol);
glBindBuffer(GL_ARRAY_BUFFER, vbol);
GLfloat vertexData[] = {
// X Y Z U V
0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // vertex 0
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // vertex 1
0.0f,-1.0f, 0.0f, 1.0f, 0.0f, // vertex 2
-1.0f,-1.0f, 0.0f, 0.0f, 0.0f, // vertex 3
}; // half fullscreen quad, 4 vertices with 5 components (floats) each
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*4*5, vertexData, GL_STATIC_DRAW);
} else {
glGenVertexArrays(1, &vaor);
glBindVertexArray(vaor);
glGenBuffers(1, &vbor);
glBindBuffer(GL_ARRAY_BUFFER, vbor);
GLfloat vertexData[] = {
// X Y Z U V
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // vertex 0
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // vertex 1
1.0f,-1.0f, 0.0f, 1.0f, 0.0f, // vertex 2
0.0f,-1.0f, 0.0f, 0.0f, 0.0f, // vertex 3
}; // half fullscreen quad, 4 vertices with 5 components (floats) each
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*4*5, vertexData, GL_STATIC_DRAW);
}
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (char*)0 + 0*sizeof(GLfloat));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (char*)0 + 3*sizeof(GLfloat));
GLuint ibo;
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
GLuint indexData[] = {
0,1,2, // first triangle
2,1,3, // second triangle
};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*2*3, indexData, GL_STATIC_DRAW);
glBindVertexArray(0);
}
bool initdone = false;
// can't do this in the constructor because we need to wait until the client window is definitely created.
// we know for sure this is the case when it starts submitting frames, so lazy init in then
void initCompositor() {
initdone = true;
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
eyew = openhmd_w/2;
clientwindow = SDL_GL_GetCurrentWindow();
printf("current SDL window %p\n", clientwindow);
clientcontext = SDL_GL_GetCurrentContext();
printf("current GL context %p\n", clientcontext);
uint32_t windowflags = SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS;
compositorwindow = SDL_CreateWindow("libopenVR Compositor (OpenHMD)", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, openhmd_w, openhmd_h, windowflags);
if (compositorwindow == NULL) {
printf("Could not create window: %s\n", SDL_GetError());
}
checkSDLError(__LINE__);
// current context is the client application rendering
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
compositorcontext = SDL_GL_CreateContext(compositorwindow);
SDL_GL_MakeCurrent(compositorwindow, compositorcontext);
// credit: https://github.com/progschj/OpenGL-Examples/blob/master/03texture.cpp
std::string vertex_source =
"#version 330\n"
"layout(location = 0) in vec4 vposition;\n"
"layout(location = 1) in vec2 vtexcoord;\n"
"out vec2 ftexcoord;\n"
"void main() {\n"
" ftexcoord = vtexcoord;\n"
" gl_Position = vposition;\n"
"}\n";
std::string fragment_source =
"#version 330\n"
"uniform sampler2D tex;\n" // texture uniform
"in vec2 ftexcoord;\n"
"layout(location = 0) out vec4 FragColor;\n"
"void main() {\n"
" FragColor = texture(tex, ftexcoord);\n"
"}\n";
GLuint vertex_shader, fragment_shader;
// we need these to properly pass the strings
const char *source;
int length;
// create and compiler vertex shader
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
source = vertex_source.c_str();
length = vertex_source.size();
glShaderSource(vertex_shader, 1, &source, &length);
glCompileShader(vertex_shader);
//TODO: error checking
// create and compiler fragment shader
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
source = fragment_source.c_str();
length = fragment_source.size();
glShaderSource(fragment_shader, 1, &source, &length);
glCompileShader(fragment_shader);
//TODO: error checking
// create program
shader_program = glCreateProgram();
// attach shaders
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
// link the program and check for errors
glLinkProgram(shader_program);
//TODO: error checking
texture_location = glGetUniformLocation(shader_program, "tex");
mkvaovbo(true);
mkvaovbo(false);
SDL_GL_MakeCurrent(clientwindow, clientcontext);
}
public:
OpenHMDCompositor() {
}
void SetTrackingSpace( ETrackingUniverseOrigin eOrigin ) {
printf("set tracking space\n");
//TODO:
}
ETrackingUniverseOrigin GetTrackingSpace() {
printf("get tracking space\n");
//TODO:
return ETrackingUniverseOrigin::TrackingUniverseStanding;
}
EVRCompositorError WaitGetPoses( VR_ARRAY_COUNT(unRenderPoseArrayCount) TrackedDevicePose_t* pRenderPoseArray, uint32_t unRenderPoseArrayCount,
VR_ARRAY_COUNT(unGamePoseArrayCount) TrackedDevicePose_t* pGamePoseArray, uint32_t unGamePoseArrayCount ) {
if (fulldbg) printf("wait get poses for %d/%d tracked devices\n", unRenderPoseArrayCount, unGamePoseArrayCount);
//TODO:
pRenderPoseArray[0].bPoseIsValid = true;
ohmd_ctx_update(ctx);
float quat[4];
ohmd_device_getf(hmd, OHMD_ROTATION_QUAT, quat);
if (fulldbg) printf("ohmd rotation quat %f %f %f %f\n", quat[0], quat[1], quat[2], quat[3]);
// CAREFUL: w x y z
glm::quat rotation(quat[3], quat[0], quat[1], quat[2]);
glm::mat3 m = glm::mat3_cast(rotation);
pRenderPoseArray[0].mDeviceToAbsoluteTracking.m[0][0] = m[0][0];
pRenderPoseArray[0].mDeviceToAbsoluteTracking.m[0][1] = m[0][1];
pRenderPoseArray[0].mDeviceToAbsoluteTracking.m[0][2] = m[0][2];
pRenderPoseArray[0].mDeviceToAbsoluteTracking.m[1][0] = m[1][0];
pRenderPoseArray[0].mDeviceToAbsoluteTracking.m[1][1] = m[1][1];
pRenderPoseArray[0].mDeviceToAbsoluteTracking.m[1][2] = m[1][2];
pRenderPoseArray[0].mDeviceToAbsoluteTracking.m[2][0] = m[2][0];
pRenderPoseArray[0].mDeviceToAbsoluteTracking.m[2][1] = m[2][1];
pRenderPoseArray[0].mDeviceToAbsoluteTracking.m[2][2] = m[2][2];
pRenderPoseArray[0].eTrackingResult = ETrackingResult::TrackingResult_Running_OK;
return VRCompositorError_None;
}
EVRCompositorError GetLastPoses( VR_ARRAY_COUNT( unRenderPoseArrayCount ) TrackedDevicePose_t* pRenderPoseArray, uint32_t unRenderPoseArrayCount,
VR_ARRAY_COUNT( unGamePoseArrayCount ) TrackedDevicePose_t* pGamePoseArray, uint32_t unGamePoseArrayCount ) {
printf("get last poses\n");
//TODO:
return VRCompositorError_None;
}
EVRCompositorError GetLastPoseForTrackedDeviceIndex( TrackedDeviceIndex_t unDeviceIndex, TrackedDevicePose_t *pOutputPose, TrackedDevicePose_t *pOutputGamePose ) {
printf("get last pose for %d\n", unDeviceIndex);
//TODO:
return VRCompositorError_None;
}
EVRCompositorError Submit( EVREye eEye, const Texture_t *pTexture, const VRTextureBounds_t* pBounds = 0, EVRSubmitFlags nSubmitFlags = Submit_Default ) {
if (!initdone) {
initCompositor();
}
if (fulldbg) {
printf("submit frame - ");
if (pTexture->eType == ETextureType::TextureType_OpenGL) {
printf("OpenGL - ");
printf(eEye == EVREye::Eye_Left ? "left\n" : "right\n");
} else {
printf(" unsupported texture type. Use OpenGL!\n");
return VRCompositorError_IncompatibleVersion; //whatever
}
}
//printf("texture pointer %p ", pTexture->handle);
uintptr_t handle = reinterpret_cast<uintptr_t>(pTexture->handle);
unsigned int cast = (unsigned int) handle;
GLuint gluint = reinterpret_cast<GLuint>(cast);
//printf("gluint %d\n", gluint);
SDL_GL_MakeCurrent(compositorwindow, compositorcontext);
if (eEye == EVREye::Eye_Left) {
glClear(GL_COLOR_BUFFER_BIT);
}
// use the shader program
glUseProgram(shader_program);
// bind texture to texture unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gluint);
// set texture uniform
glUniform1i(texture_location, 0);
if (eEye == EVREye::Eye_Left) {
// bind the vao
glBindVertexArray(vaol);
} else {
glBindVertexArray(vaor);
}
// draw
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// check for errors
GLenum error = glGetError();
if(error != GL_NO_ERROR) {
std::cerr << "on submit frame: OpenGL error: " << error << std::endl;
}
if (eEye == EVREye::Eye_Right) {
// finally swap buffers
SDL_GL_SwapWindow(compositorwindow);
}
SDL_GL_MakeCurrent(clientwindow, clientcontext);
return VRCompositorError_None;
}
void ClearLastSubmittedFrame() {
printf("clear last frame\n");
//TODO:
}
void PostPresentHandoff() {
printf("postpresenthandoff\n");
//TODO:
}
bool GetFrameTiming( Compositor_FrameTiming *pTiming, uint32_t unFramesAgo = 0 ) {
printf("get frame timing\n");
//TODO:
return false;
}
uint32_t GetFrameTimings( Compositor_FrameTiming *pTiming, uint32_t nFrames ) {
printf("get frame timings\n");
//TODO:
return 0;
}
float GetFrameTimeRemaining() {
printf("frame time remaining\n");
//TODO:
return 1;
}
void GetCumulativeStats( Compositor_CumulativeStats *pStats, uint32_t nStatsSizeInBytes ) {
printf("get cumulative stats\n");
//TODO:
}
void FadeToColor( float fSeconds, float fRed, float fGreen, float fBlue, float fAlpha, bool bBackground = false ) {
printf("fade to color %f %f %f\n", fRed, fGreen, fBlue);
//TODO:
}
HmdColor_t GetCurrentFadeColor( bool bBackground = false ) {
printf("get fade color\n");
//TODO:
HmdColor_t hc;
hc.a = .5;
hc.r = .5;
hc.g = .5;
hc.b = .5;
return hc;
}
void FadeGrid( float fSeconds, bool bFadeIn ) {
printf("fade grid\n");
//TODO:
}
float GetCurrentGridAlpha() {
printf("get grid alpha\n");
//TODO:
return 0;
}
EVRCompositorError SetSkyboxOverride( VR_ARRAY_COUNT( unTextureCount ) const Texture_t *pTextures, uint32_t unTextureCount ) {
printf("skybox override\n");
//TODO:
return VRCompositorError_None;
}
void ClearSkyboxOverride() {
printf("clear skybox override\n");
//TODO:
}
void CompositorBringToFront() {
printf("bring compositor to front\n");
}
virtual void CompositorGoToBack() {
printf("compositor to back\n");
}
void CompositorQuit() {
printf("compositor quit\n");
SDL_DestroyWindow(compositorwindow);
SDL_Quit();
}
bool IsFullscreen() {
printf("compositor fullscreen\n");
//TODO:
return true;
}
uint32_t GetCurrentSceneFocusProcess() {
printf("scene process\n");
//TODO:
return -1;
}
uint32_t GetLastFrameRenderer() {
printf("last frame renderer\n");
//TODO:
return -1;
}
bool CanRenderScene() {
printf("can render\n");
//TODO: