File: VRInputStuffTest.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (894 lines) | stat: -rw-r--r-- 33,822 bytes parent folder | download
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
function VRInputStuffTest(withHapticFeedback, withMTStressTest, specialReqs, refSpace, withGazeTracking, withHandTracking)
% VRInputStuffTest([withHapticFeedback=0][, withMTStressTest=0][, specialReqs='DebugDisplay'][, refSpace][, withGazeTracking=0][, withHandTracking=0])
%
% Test input functionality related to VR devices.
%
% Tries to enumerate available controllers and other properties related to
% input. After any key press or controller button press, reports live state
% of buttons, sensors, triggers etc. of connected controllers.
%
% If the optional parameter 'withHapticFeedback' is set to 1, then also
% exercises the haptic feedback functionality of any of the left and right
% hand controllers, if the A/B or X/Y buttons are pressed for low/high
% frequency rumble on right or left controller. Haptic feedback is not
% exercised by default, as it empties the controllers batteries relatively
% fast.
%
% The optional parameter 'withMTStressTest' if set to 1 will test
% single-threading to multi-threading switching on the fly. Multi-threaded
% mode is less efficient, more prone to skipped deadlines or lower
% animation rates, but stabilizes visuals in 'Stop'ed 3D mode, and is
% needed on most OpenXR runtimes to get even a semblance of correct frame
% presentation timing and timestamping.
%
% The optional parameter 'specialReqs' allows to pass in extra
% basicRequirments into the driver. Meaningful keywords could be:
%
% ForceSize=1230x4560 = Enforce a per-eye image size of 1230x4560 pixels.
% Use2DViewsWhen3DStopped = Use different display mode for stopped 3D rendering.
% 2DViewDistMeters=2.1 = Enforce 2D views to be 2.1 meters away from the
% viewer, instead of the default of 1 meter. Allows scaling of 2D image
% views.
% DontCareAboutVisualGlitchesWhenStopped = Don't care about glitches when stopped.
% ForbidMultiThreading = Do not use multi-threaded presentation ever.
% DebugDisplay = Also show rendered stimuli on the experimenters monitor.
% This is the default.
%
% The optional parameter 'refSpace', if provided and non-zero, allows to
% select a specific OpenXR reference space under OpenXR. It is ignored
% under other drivers. The most interesting values are 1 for head locked, 2
% for a local reference space, and 3 for a stage reference space. 3 often
% enables additional goodies, but support for it is not mandatory for an
% OpenXR runtime, so selecting 3 could fail. However, so far 4 out of 4
% tested OpenXR runtimes on Linux and Windows did support the stage
% reference space, which provided a more natural coordinate system and also
% visualization of the "play area". The driver default is 2 for local, as
% that is always supported.
%
% The optional parameter 'withGazeTracking', if provided and non-zero, will
% enable some basic tests of eye gaze tracking with VR HMD's which support
% eye tracking. A setting of 1 will visualize the 2D gaze position, a setting of
% 2 will visualize a 3D gaze ray in addition.
%
% The optional parameter 'withHandTracking', if provided and non-zero, will
% enable some basic tests of hand tracking with VR hardware that supports hand
% tracking. A setting of 1 will visualize the measured hand joint locations.
%
% After a keypress (or Enter/Back button press on the controller),
% visualizes tracked hand position and orientation of hand controllers and
% allows to do some nice visual effects based on trigger / grip button
% presses, thumbsticks movement etc.
%
% Tested with XBox controller, Oculus remote, and the two Oculus touch
% controllers of the Oculus Rift CV-1 on Windows-10 and Linux, with the
% OculusVR v1 runtime on Windows, and with various OpenXR runtimes like
% Monado, OculusVR, SteamVR. Additionally tested with the Vive Wand
% controllers and builtin binocular eye gaze tracker of a HTC Vive Pro Eye.

% Constants for use in VR applications:
global OVR; %#ok<GVMIS>

% GL data structure needed for all OpenGL demos:
global GL; %#ok<GVMIS>

% No testing of haptic feedback by default, as it sucks up batteries a lot:
if nargin < 1 || isempty(withHapticFeedback)
    withHapticFeedback = 0;
end

% No single/multi-threading stress test by default:
if nargin < 2 || isempty(withMTStressTest)
    withMTStressTest = 0;
end

% Use DebugDisplay by default, if specialReqs omitted:
if nargin < 3 || isempty(specialReqs)
    specialReqs = 'DebugDisplay';
end

% No specific reference space by default - Allow driver to do its thing:
if nargin < 4 || isempty(refSpace)
    refSpace = 0;
end

% Disable test of eye gaze tracking by default:
if nargin < 5 || isempty(withGazeTracking)
    withGazeTracking = 0;
end

if withGazeTracking
    % Tell that eyetracking is desired:
    specialReqs = [specialReqs ' Eyetracking '];
end

if nargin < 6 || isempty(withHandTracking)
    withHandTracking = 0;
end

if withHandTracking
    % Tell that hand tracking is desired:
    specialReqs = [specialReqs ' Handtracking '];
end

canary = onCleanup(@sca);

% Setup unified keymapping and unit color range:
PsychDefaultSetup(2);

% Setup Psychtoolbox for OpenGL 3D rendering support and initialize the
% mogl OpenGL for Matlab/Octave wrapper:
InitializeMatlabOpenGL;

% Select screen with highest id as Oculus output display:
screenid = max(Screen('Screens'));

PsychDebugWindowConfiguration;

% Open our fullscreen onscreen window with black background clear color:
PsychImaging('PrepareConfiguration');

% Request Head-tracked 3D mode and tell driver that we don't care about
% properly timed presentation or accurate/trustworthy stimulus onset
% timestamps. We use timestamps for simple performance tests, but they are
% not critical for our purpose.
% Optional: Use2DViewsWhen3DStopped DontCareAboutVisualGlitchesWhenStopped ForbidMultiThreading
hmd = PsychVRHMD('AutoSetupHMD', 'Tracked3DVR', ['NoTimingSupport NoTimestampingSupport ' specialReqs]);
if isempty(hmd)
    fprintf('No VR HMDs connected. Game over!\n');
    return;
end

[win, winRect] = PsychImaging('OpenWindow', screenid, [0 0 1]);
hmdinfo = PsychVRHMD('GetInfo', hmd);

if strcmpi(hmdinfo.type, 'OpenXR') && refSpace
    % Select different reference space:
    PsychOpenXR('ReferenceSpaceType', hmd, refSpace)
end

% OpenXR and Use2DViewsWhen3DStopped requested for stopped 3D rendering?
if strcmpi(hmdinfo.type, 'OpenXR') && ~isempty(strfind(specialReqs, 'Use2DViewsWhen3DStopped')) %#ok<STREMP>
    % Set some default position for the 2D views that matches kleinerm's
    % eyes well and provides matching imaging geometry between 3D
    % projection layers and 2D quadviews, at least as tested with HTC Vive Pro Eye
    % under SteamVR:
    PsychVRHMD('View2DParameters', hmd, 0, [-0.098726, 0.000000, -1.000000]);
    PsychVRHMD('View2DParameters', hmd, 1, [+0.098726, 0.000000, -1.000000]);
end

% Retrieve the initial settings for position and size for 2D quad views:
oldPositionL = PsychVRHMD('View2DParameters', hmd, 0);
oldPositionR = PsychVRHMD('View2DParameters', hmd, 1);

% Test of eye gaze tracking requested and eye tracking supported by this system?
if withGazeTracking && hmdinfo.eyeTrackingSupported
    % Yes. Request gaze samples during calls to 'PrepareRender':
    reqMask = 1 + 2 + 4;
else
    % No. Disable any eye gaze tracking:
    reqMask = 1 + 2;
    withGazeTracking = 0;
end

if withHandTracking && hmdinfo.articulatedHandTrackingSupported
    % Yes. Request hand tracker samples during calls to 'PrepareRender':
    reqMask = reqMask + 8;
else
    % No. Disable any hand tracking:
    withHandTracking = 0;
end

clc;

% Mark our own tracking + rendering loop as stopped for initial section of test/demo:
PsychVRHMD('Stop', hmd);

if strcmpi(hmdinfo.subtype, 'Oculus-1')
    fprintf('Properties of our subject:\n\n');
    fprintf('Player height: %f\n', PsychOculusVR1('FloatProperty', hmd, OVR.KEY_PLAYER_HEIGHT));
    fprintf('Player eye height: %f\n', PsychOculusVR1('FloatProperty', hmd, OVR.KEY_EYE_HEIGHT));
    fprintf('Player neck-to-eye: %f\n', PsychOculusVR1('FloatsProperty', hmd, OVR.KEY_NECK_TO_EYE_DISTANCE));
    fprintf('Player eye-to-nose: %f\n', PsychOculusVR1('FloatsProperty', hmd, OVR.KEY_EYE_TO_NOSE_DISTANCE));
    % Suppress error output during username query, as these properties are often undefined and
    % would spill pointless error messages:
    oldverbosity = PsychOculusVR1('Verbosity', 0);
    fprintf('User name: %s\n', PsychOculusVR1('StringProperty', hmd, OVR.KEY_USER, 'Hans'));
    fprintf('Player name: %s\n', PsychOculusVR1('StringProperty', hmd, OVR.KEY_NAME, 'Mueller'));
    PsychOculusVR1('Verbosity', oldverbosity);
    fprintf('Player gender: %s\n', PsychOculusVR1('StringProperty', hmd, OVR.KEY_GENDER, OVR.KEY_DEFAULT_GENDER));
    fprintf('\n\n');
end

if hmdinfo.VRControllersSupported
    fprintf('\n\nConnected controllers:\n\n');
    controllerTypes = PsychVRHMD('Controllers', hmd);
    if bitand(controllerTypes, OVR.ControllerType_Remote)
        fprintf('Remote control connected.\n');
    end
    if bitand(controllerTypes, OVR.ControllerType_XBox)
        fprintf('XBox controller connected.\n');
    end
    if bitand(controllerTypes, OVR.ControllerType_LTouch)
        fprintf('Left hand controller connected.\n');
    end
    if bitand(controllerTypes, OVR.ControllerType_RTouch)
        fprintf('Right hand controller connected.\n');
    end
    fprintf('\n\n');
else
    controllerTypes = 0;
end

if hmdinfo.hapticFeedbackSupported && ~withHapticFeedback
    hmdinfo.hapticFeedbackSupported = 0;
    fprintf('\nHaptic feedback supported but disabled, to conserve battery life.\n');
end

% Fetch play area / guardian boundaries:
[~, playboundsxyz, outerboundsxyz] = PsychVRHMD('VRAreaBoundary', hmd);

fprintf('\n\nPress any key or controller button to continue. Will continue in 10 seconds automatically.\n');
DrawFormattedText(win, 'Press any key or controller button to continue. Will continue in 10 seconds automatically.', 'center', 'center', [1 1 0], 20);
Screen('Flip', win);

timeout = GetSecs + 10;
while 1
    down = KbCheck(-1);
    istate = PsychVRHMD('GetInputState', hmd, OVR.ControllerType_Active);
    if any(istate.Buttons) || down || GetSecs > timeout
        break;
    end
end

KbReleaseWait(-1);
while any(istate.Buttons)
    istate = PsychVRHMD('GetInputState', hmd, OVR.ControllerType_Active);
end

pulseEnd = 0;

while 1
    WaitSecs('YieldSecs', 0.1);
    clc;

    % Show instructions in HMD on how to continue:
    DrawFormattedText(win, sprintf('Press BackSpace key on keyboard, or Enter-Button or\nBack-Button on controller\n\n\nTime: %f', GetSecs), 'center', 'center', [1 1 0]);
    Screen('Flip', win);

    [~, ~, keycode] = KbCheck(-1);
    if keycode(KbName('BackSpace'))
        break;
    end

    % Query and display all input state:
    istate = PsychVRHMD('GetInputState', hmd, OVR.ControllerType_Active);

    if ~istate.Valid
        fprintf('No VR controller input available, possibly due to VR input focus loss.\n');
        fprintf('Make sure our application has input focus and possibly that the HMD is on the users head.\n');
        continue;
    end

    fprintf('Press Back button on remote control or other controllers, or Enter-Button on controller, or backspace key, to finish.\n');
    fprintf('Delta now - controller update time in msecs: %f\n\n', 1000 * (GetSecs - istate.Time));
    disp(istate);

    if istate.Buttons(OVR.Button_A)
        fprintf('Button_A ');
    end

    if istate.Buttons(OVR.Button_B)
        fprintf('Button_B ');
    end

    if istate.Buttons(OVR.Button_X)
        fprintf('Button_X ');
    end

    if istate.Buttons(OVR.Button_Y)
        fprintf('Button_Y ');
    end

    if istate.Buttons(OVR.Button_RThumb)
        fprintf('Button_RThumb ');
    end

    if istate.Buttons(OVR.Button_RShoulder)
        fprintf('Button_RShoulder ');
    end

    if istate.Buttons(OVR.Button_LThumb)
        fprintf('Button_LThumb ');
    end

    if istate.Buttons(OVR.Button_LShoulder)
        fprintf('Button_LShoulder ');
    end

    if istate.Buttons(OVR.Button_Up)
        fprintf('Button_Up ');
    end

    if istate.Buttons(OVR.Button_Down)
        fprintf('Button_Down ');
    end

    if istate.Buttons(OVR.Button_Left)
        fprintf('Button_Left ');
    end

    if istate.Buttons(OVR.Button_Right)
        fprintf('Button_Right ');
    end

    if istate.Buttons(OVR.Button_Enter)
        fprintf('Button_Enter ');
    end

    if istate.Buttons(OVR.Button_Back)
        fprintf('Button_Back ');
    end

    if istate.Buttons(OVR.Button_VolUp)
        fprintf('Button_VolUp ');
    end

    if istate.Buttons(OVR.Button_VolDown)
        fprintf('Button_VolDown ');
    end

    if istate.Buttons(OVR.Button_MicMute)
        fprintf('Button_MicMute ');
    end

    if istate.Buttons(OVR.Button_Home)
        fprintf('Button_Home ');
    end

    if any(istate.Buttons(OVR.Button_RMask))
        fprintf('Button_RMask ');
    end

    if any(istate.Buttons(OVR.Button_LMask))
        fprintf('Button_LMask ');
    end

    if any(istate.Buttons(OVR.Button_Private))
        fprintf('Button_Private ');
    end

    if istate.Buttons(OVR.Button_Back) || istate.Buttons(OVR.Button_Enter) || keycode(KbName('BackSpace'))
        break;
    end

    fprintf('\n');

    % Oculus VR 1.11+ controlled VR setup?
    if strcmpi(hmdinfo.subtype, 'Oculus-1')
        % Get info about collisions of tracked devices with the VR play area boundaries:
        [isTriggering, closestDistance, closestPointxyz, surfaceNormal] = PsychOculusVR1('TestVRBoundary', hmd, OVR.TrackedDevice_All, 0);
        if isTriggering
            fprintf('VR collision: closestDistance %f m, point [%f,%f,%f], normal [%f,%f,%f]\n', closestDistance, closestPointxyz(1), closestPointxyz(2), ...
                    closestPointxyz(3), surfaceNormal(1), surfaceNormal(2), surfaceNormal(3));
        end

        % Get info about collision of test point with the VR play area boundaries:
        mypoint = [1, 2, 3];
        [isTriggering, closestDistance, closestPointxyz, surfaceNormal] = PsychOculusVR1('TestVRBoundaryPoint', hmd, mypoint, 0);
        if isTriggering
            fprintf('VR mypoint collision: closestDistance %f m, point [%f,%f,%f], normal [%f,%f,%f]\n', closestDistance, closestPointxyz(1), closestPointxyz(2), ...
                    closestPointxyz(3), surfaceNormal(1), surfaceNormal(2), surfaceNormal(3));
        end
    end

    if (istate.Buttons(OVR.Button_A) || istate.Buttons(OVR.Button_B)) && hmdinfo.hapticFeedbackSupported
        if GetSecs < pulseEnd
            % Pressed A before end of previously initiated pulse. Abort current pulse:
            pulseEnd = PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_RTouch, [], 0, 0);
        else
            % Initiate new pulse: 0.75 seconds, 25% or 100% frequency, 0.8 amplitude:
            if istate.Buttons(OVR.Button_B)
                pulseEnd = PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_RTouch, 0.75, 1.0, 0.8);
            else
                pulseEnd = PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_RTouch, 0.75, 0.25, 0.8);
            end
            % Debounce button:
            WaitSecs('YieldSecs', 0.1);
        end
    end

    if (istate.Buttons(OVR.Button_X) || istate.Buttons(OVR.Button_Y)) && hmdinfo.hapticFeedbackSupported
        if GetSecs < pulseEnd
            % Pressed A before end of previously initiated pulse. Abort current pulse:
            pulseEnd = PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_LTouch, [], 0, 0);
        else
            % Initiate new pulse: 0.75 seconds, 25% or 100% frequency, 0.8 amplitude:
            if istate.Buttons(OVR.Button_Y)
                pulseEnd = PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_LTouch, 0.75, 1.0, 0.8);
            else
                pulseEnd = PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_LTouch, 0.75, 0.25, 0.8);
            end
            % Debounce button:
            WaitSecs('YieldSecs', 0.1);
        end
    end

    if (istate.Buttons(OVR.Button_Up) || istate.Buttons(OVR.Button_Down)) && hmdinfo.hapticFeedbackSupported
        if GetSecs < pulseEnd
            % Pressed A before end of previously initiated pulse. Abort current pulse:
            pulseEnd = PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_XBox, [], 0, 0);
        else
            % Initiate new pulse: 0.75 seconds, 25% or 100% frequency, 0.8 amplitude:
            if istate.Buttons(OVR.Button_Up)
                pulseEnd = PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_XBox, 0.75, 1.0, 0.8);
            else
                pulseEnd = PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_XBox, 0.75, 0.25, 0.8);
            end
            % Debounce button:
            WaitSecs('YieldSecs', 0.1);
        end
    end
end

while any(istate.Buttons)
    istate = PsychVRHMD('GetInputState', hmd, OVR.ControllerType_Active);
end

% Part 3: Actual hand tracking and visualisation:
if hmdinfo.handTrackingSupported || withGazeTracking || withHandTracking
  % Number of fountain particles whose positions are computed on the GPU:
  nparticles = 10000;

  % Diameter of particles in pixels:
  particleSize = 5;

  % 'StartPosition' is the 3D position where all particles originate. It is
  % faked to a position, so that the particles seem to originate from the
  % teapots "nozzle":

  % Lifetime for each simulated particle, is chosen so that there seems to be
  % an infinite stream of particles, although the same particles are recycled
  % over and over:
  particlelifetime = 2;

  % Amount of "flow": A value of 1 will create a continuous stream, whereas
  % smaller value create bursts of particles:
  flowfactor = 1;

  % Load and setup the vertex shader for particle fountain animation:
  shaderpath = [PsychtoolboxRoot 'PsychDemos/OpenGL4MatlabDemos/GLSLDemoShaders/ParticleSimple'];
  glsl = LoadGLSLProgramFromFiles(shaderpath,1);

  % Bind shader so it can be setup:
  glUseProgram(glsl);

  % Assign static 3D startposition for fountain at tip of nozzles:
  StartPosition = [0, 0, -0.4];
  glUniform3f(glGetUniformLocation(glsl, 'StartPosition'), StartPosition(1), StartPosition(2), StartPosition(3));

  % Assign lifetime:
  glUniform1f(glGetUniformLocation(glsl, 'LifeTime'), particlelifetime);

  % Assign simulated gravity constant 'g' for proper trajectory:
  glUniform1f(glGetUniformLocation(glsl, 'Acceleration'), 0.5);

  % Done with setup:
  glUseProgram(0);

  % Assign random RGB colors to the particles: The shader will use these, but
  % also assign an alpha value that makes the particles "fade out" at the end
  % of there lifetime:
  particlecolors = rand(3, nparticles);

  % Maximum speed for particles:
  maxspeed = 1.25;

  % Per-component speed: We select these to shape the fountain in our wanted
  % direction:
  vxmax = 0.1 * maxspeed;
  vymax = 0.1 * maxspeed;
  vzmax = maxspeed;

  % Assign random velocities in (vx,vy,vz) direction: Intervals chosen to
  % shape the beam into something visually pleasing for a teapot:
  particlesxyzt(1,:) = RandLim([1, nparticles], -vxmax, +vxmax); % Beam dispersion horizontal.
  particlesxyzt(2,:) = RandLim([1, nparticles], -vymax, +vymax); % Beam dispersion vertical.
  particlesxyzt(3,:) = RandLim([1, nparticles], -vzmax, -0.7 * vzmax); % Emission speed range.

  % The w-component (4th dimension) encodes the birthtime of the particle. We
  % assign random birthtimes within the possible particlelifetime to get a
  % nice continuous stream of particles. Well, kind of: The flowfactor
  % controls the "burstiness" of particle flow. A value of 1 will create a
  % continous stream, whereas smaller values will create bursts of particles,
  % as if the teapot is choking:
  particlesxyzt(4,:) = RandLim([1, nparticles], 0.0, particlelifetime * flowfactor);

  % Setup the OpenGL rendering context of the onscreen window for use by the
  % OpenGL wrapper. After this command, all following OpenGL commands will
  % draw into the onscreen window 'win':
  Screen('BeginOpenGL', win);

  % Set viewport properly:
  glViewport(0, 0, RectWidth(winRect), RectHeight(winRect));

  % Setup default drawing color to yellow (R,G,B)=(1,1,0). This color only
  % gets used when lighting is disabled - if you comment out the call to
  % glEnable(GL.LIGHTING).
  glColor3f(1,1,0);

  % Enable the first local light source GL.LIGHT_0.
  glEnable(GL.LIGHT0);

  % Enable alpha-blending for smooth dot drawing:
  glEnable(GL.BLEND);
  glBlendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);

  % Retrieve and set camera projection matrix for optimal rendering on the HMD:
  [projMatrix{1}, projMatrix{2}] = PsychVRHMD('GetStaticRenderParameters', hmd);

  % Setup modelview matrix: This defines the position, orientation and
  % looking direction of the virtual camera:
  glMatrixMode(GL.MODELVIEW);
  glLoadIdentity;

  % Set background clear color to 'black' (R,G,B,A)=(0,0,0,0):
  glClearColor(0,0,0,0);

  % Clear out the backbuffer: This also cleans the depth-buffer for
  % proper occlusion handling: You need to glClear the depth buffer whenever
  % you redraw your scene, e.g., in an animation loop. Otherwise occlusion
  % handling will screw up in funny ways...
  glClear;

  % Enable lighting:
  glEnable(GL.LIGHTING);

  % Enable proper occlusion handling via depth tests:
  glEnable(GL.DEPTH_TEST);

  % Predraw the particles. Here particlesxyzt does not encode position, but
  % speed -- this because our shader interprets positions as velocities!
  gld = glGenLists(1);
  glNewList(gld, GL.COMPILE);
  moglDrawDots3D(win, particlesxyzt, particleSize, particlecolors, [], 1);
  glEndList;

  % Finish OpenGL rendering into PTB window. This will switch back to the
  % standard 2D drawing functions of Screen and will check for OpenGL errors.
  Screen('EndOpenGL', win);

  % Make sure all keys are released:
  KbReleaseWait;

  % Realtime scheduling:
  %Priority(MaxPriority(win));

  fcount = 0;
  globalPos = [0, 0, 3];
  heading = 0;
  pulseEnd = [];

  if ~bitand(controllerTypes, OVR.ControllerType_LTouch + OVR.ControllerType_RTouch)
    [xc, yc] = RectCenter(winRect);
    SetMouse(xc,yc, screenid);
    [xo, yo] = GetMouse(screenid);
  else
    SetMouse(0, 0, screenid);
  end

  HideCursor(screenid);

  % Mark our own tracking + rendering loop as started for remainder of test/demo,
  % as from here on we do perform head tracking driven 3D rendering and presentation:
  PsychVRHMD('Start', hmd);

  % Initial flip to sync us to VBL and get start timestamp:
  vbl = Screen('Flip', win);
  tstart = vbl;

  % VR render loop: Runs until keypress:
  while ~KbCheck && ~(istate.Buttons(OVR.Button_Back) || istate.Buttons(OVR.Button_Enter))
    if ~bitand(controllerTypes, OVR.ControllerType_LTouch + OVR.ControllerType_RTouch)
        % Update global position (x,y,z) by mouse movement:
        [xm, ym, buttons] = GetMouse(screenid);
        if ~any(buttons)
          % x-movement:
          dx = (xm - xo);
          globalPos(1) = globalPos(1) + 0.005 * dx;

          % y-movement:
          dy = (yo - ym);
          globalPos(2) = globalPos(2) + 0.005 * dy;
        else
          if buttons(1)
            % z-movement:
            dz = (ym - yo);
            globalPos(3) = globalPos(3) + 0.005 * dz;
          end

          if buttons(2)
            % Heading, ie. looking direction:
            dh = (xm - xo);
            heading = heading + 0.01 * dh;
          end
        end

        % Reposition mouse cursor for next render cycle:
        SetMouse(xc,yc, screenid);
        [xo, yo] = GetMouse(screenid);
    end

    % Allow to use thumbsticks, if any, to move the teapot:
    if istate.Buttons(OVR.Button_LThumb) || istate.Buttons(OVR.Button_RThumb)
      thumbmult = 0.05;
    else
      thumbmult = 0.005;
    end

    if hmdinfo.VRControllersSupported
      globalPos(1) = globalPos(1) - thumbmult * istate.Thumbstick(1,1);
      globalPos(2) = globalPos(2) - thumbmult * istate.Thumbstick(2,1);
      globalPos(3) = globalPos(3) + thumbmult * istate.Thumbstick(2,2);
      oldPositionL(1) = oldPositionL(1) + 0.001 * istate.Thumbstick(1,2);
      oldPositionR(1) = oldPositionR(1) - 0.001 * istate.Thumbstick(1,2);
    end

    % Compute a transformation matrix to globally position and orient the
    % observer in the scene. This allows mouse control of observer position
    % and heading on top of the head tracking:
    globalHeadPose = PsychGetPositionYawMatrix(globalPos, heading);

    % Track and predict head + hands position and orientation, possibly also
    % eye gaze, depending on reqMask. Retrieve modelview camera matrices
    % for rendering of each eye. Apply some global transformation to
    % returned camera matrices. In this case a translation + rotation, as
    % defined by the PsychGetPositionYawMatrix() helper function:
    state = PsychVRHMD('PrepareRender', hmd, globalHeadPose, reqMask);
    %fprintf('HMD state.tracked %i  = Controller tracked %i : %i\n', state.tracked, state.handStatus(1), state.handStatus(2));

    % Get controller input state, buttons, triggers etc.:
    istate = PsychVRHMD('GetInputState', hmd, OVR.ControllerType_Active);

    % We render the scene separately for each eye:
    for renderPass = 0:1
      % Selected 'view' to render (left- or right-eye):
      Screen('SelectStereoDrawbuffer', win, renderPass);

      % Extract modelView matrix for this renderPass's eye:
      modelView = state.modelView{renderPass + 1};

      % Manually reenable 3D mode in preparation of eye draw cycle:
      Screen('BeginOpenGL', win);

      % Set per-eye projection matrix: This defines a perspective projection,
      % corresponding to the model of a pin-hole camera - which is a good
      % approximation of the human eye and of standard real world cameras --
      % well, the best aproximation one can do with 2 lines of code ;-)
      glMatrixMode(GL.PROJECTION);
      glLoadMatrixd(projMatrix{renderPass+1});

      % Setup camera position and orientation for this eyes view:
      glMatrixMode(GL.MODELVIEW);
      glLoadMatrixd(modelView);

      % Light position:
      glLightfv(GL.LIGHT0,GL.POSITION,[ 1 2 3 0 ]);
      glDisable(GL.LIGHTING);

      % Clear color and depths buffers:
      glClear;

      % Visualize projection of guardian "walls" to the floor, if any are defined:
      if ~isempty(outerboundsxyz)
        % Change color of guardian lines, depending if guardian grid visible or not:
        if PsychVRHMD('VRAreaBoundary', hmd)
          glColor3f(1.0, 0.0, 0.0);
        else
          glColor3f(1.0, 1.0, 0.0);
        end

        % Outer bounds - hard walls:
        glBegin(GL.LINE_LOOP);
          for i = 1:size(outerboundsxyz, 2)
            glVertex3dv(outerboundsxyz(:, i) + globalPos');
          end
        glEnd;

        % Inner play area - a  rectangle inscribed to the outer bounds:
        glBegin(GL.LINE_LOOP);
          for i = 1:size(playboundsxyz , 2)
            glVertex3dv(playboundsxyz(:, i) + globalPos');
          end
        glEnd;
      end

      % Visualize 3D gaze direction if requested:
      if (withGazeTracking >= 2) && (length(state.gazeStatus) >= renderPass + 1) && ...
         (state.gazeStatus(renderPass + 1) >= 3)
        % Draw a 15 meters long gaze-ray, using the gaze ray equation
        % provided by gaze tracking, ie. global start position of the ray
        % 'tv', corresponding to the the estimated optical center of the
        % eye, and the gaze direction vector 'dv', along the optical axis /
        % looking direction of that eye -- equivalent to the negative
        % z-axis of the eye gaze reference frame:
        tv = state.gazeRayGlobal{renderPass + 1}.gazeC;
        dv = state.gazeRayGlobal{renderPass + 1}.gazeD;

        glColor3f(1,0,1);
        glBegin(GL.LINES);
        glVertex3dv(tv);
        vp = tv + dv * 15;
        glVertex3dv(vp);
        glEnd;
      end

      % Further rendering needs proper lighting:
      glEnable(GL.LIGHTING);
      glPushMatrix;

      % Compute simulation time for this draw cycle:
      telapsed = vbl - tstart;

      % Bring a bit of extra spin into this :-)
      glRotated(10 * telapsed, 0, 1, 0);
      glRotated(5  * telapsed, 1, 0, 0);

      % Draw a solid teapot of size 1.0:
      glutSolidTeapot(1);

      % Done drawing the mighty teapot:
      glPopMatrix;

      % Visualize users hands / hand controllers:
      for hand = 1:2
        % Skip hand controller visualization if hand tracking is enabled and this
        % hand is tracked. We use hand tracking visualization instead in this case:
        if withHandTracking && state.trackedHandStatus(hand)
          continue;
        end

        % Position and orientation of hand tracked? Otherwise we don't show them:
        if bitand(state.handStatus(hand), 3) == 3
          % Yes: Lets visualize it:

          % Have fallback if controller trigger and grip buttons unsupported:
          if hmdinfo.VRControllersSupported
            tr = istate.Trigger(hand);
            di = istate.Grip(hand);
          else
            tr = 0.1;
            di = 0;
          end

          glPushMatrix;
          glMultMatrixd(state.globalHandPoseMatrix{hand});
          glutSolidCone(0.1 * (1.1 - di), -0.4, 10, 10);

          if tr > 0.015
            % Draw the particle fountain. We use a vertex shader in the shader
            % program glsl to compute the physics:
            glUseProgram(glsl);

            % Assign updated simulation time to shader:
            glUniform1f(glGetUniformLocation(glsl, 'Time'), telapsed);

            % Assign simulated gravity constant 'g' for proper trajectory:
            glUniform1f(glGetUniformLocation(glsl, 'Acceleration'), 1 - tr);

            % Draw the particles: We have preencoded them into a OpenGL display list
            % above for higher performance of drawing:
            glCallList(gld);

            % Done with shaded drawing:
            glUseProgram(0);
          end

          glPopMatrix;
        end
      end

      % Visualize tracked hand joints locations if withHandTracking:
      if withHandTracking
        glDisable(GL.LIGHTING);

        % Iterate over both hands:
        for hand = 1:2
          % hand fully tracked? Otherwise we don't show it:
          if state.trackedHandStatus(hand)
            % Yes: Lets visualize all its tracked joint locations.
            glColor3f(hand - 1, 1, 1);

            for joint = find(state.trackedJoints(hand, :))
              glPushMatrix;
              glMultMatrixd(state.globalJointPoseMatrix{hand, joint});
              glutSolidCube(state.trackedJointsRadius(hand, joint));
              glPopMatrix;
            end
          end
        end
      end

      % Manually disable 3D mode before switching to other eye or to flip:
      Screen('EndOpenGL', win);

      % Visualize tracked left-eye 2D gaze position in left-eye view, if any:
      if withGazeTracking && (length(state.gazeStatus) >= renderPass + 1) && ...
         (state.gazeStatus(renderPass + 1) >= 3)
          Screen('DrawDots', win, state.gazePos{renderPass + 1}, 5, [1, 0, 0]);
      end

      % Repeat for renderPass of other eye:
    end

    % Head position tracked?
    if ~bitand(state.tracked, 2)
      % Nope, user out of cameras view frustum. Tell it like it is:
      DrawFormattedText(win, 'Vision based tracking lost\nGet back into the cameras field of view!', 'center', 'center', [1 0 0]);
    end

    if hmdinfo.hapticFeedbackSupported && hmdinfo.VRControllersSupported
      if (istate.Grip(1) > 0.5 || istate.Grip(2) > 0.5) && isempty(pulseEnd)
          % Initiate new pulse: 0.75 seconds, 25% or 100% frequency, 0.8 amplitude:
          if istate.Grip(2) > 0.5
              controller = OVR.ControllerType_RTouch;
          else
              controller = OVR.ControllerType_LTouch;
          end

          PsychVRHMD('HapticPulse', hmd, controller, [], 0.25, 0.8);

          % Set end time for pulse:
          pulseEnd = GetSecs + 0.75;
      elseif GetSecs > pulseEnd
          % End current pulse:
          pulseEnd = [];
          PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_RTouch, [], 0, 0);
      end
    end

    secs = GetSecs;
    if hmdinfo.hapticFeedbackSupported && hmdinfo.VRControllersSupported && ...
       istate.Buttons(OVR.Button_A) && (isempty(pulseEnd) || pulseEnd + 1 < secs)
        pulseEnd = PsychVRHMD('HapticPulse', hmd, OVR.ControllerType_XBox, [], 0.25, 0.8);
        KbReleaseWait;
    end

    if ~isempty(pulseEnd) && pulseEnd > secs
        DrawFormattedText(win, sprintf('t = %f secs.', pulseEnd - secs), 'center', 'center');
    end

    % MT->ST->MT->... switching stress test, if enabled:
    if withMTStressTest
        % Apply horizontal deflection of right thumbstick to change
        % x-position of the 2D quad views, to change required vergence in
        % 'Stop' mode when 2D views are used:
        oldPositionL = PsychVRHMD('View2DParameters', hmd, 0, oldPositionL);
        oldPositionR = PsychVRHMD('View2DParameters', hmd, 1, oldPositionR);

        if mod(fcount, 360) == 0
            PsychVRHMD('Stop', hmd);
        end

        if mod(fcount, 360) == 180
            PsychVRHMD('Start', hmd);
        end
    end

    % Stimulus ready. Show it on the HMD. We don't clear the color buffer here,
    % as this is done in the next iteration via glClear() call anyway:
    vbl = Screen('Flip', win, [], 1);
    fcount = fcount + 1;
  end

  % Cleanup:
  Priority(0);

  % Stats for nerds:
  fprintf('Final settings for 2D views positionL = [%f, %f, %f] positionR = [%f, %f, %f]\n\n', ...
          oldPositionL(1), oldPositionL(2), oldPositionL(3), oldPositionR(1), oldPositionR(2), oldPositionR(3));
  fps = fcount / (vbl - tstart);
  fprintf('Average framerate was %f fps. Bye!\n', fps);
end

% Close windows, shutdown HMD, show cursor, general visual cleanup:
sca;