-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
oculusVR.cs
248 lines (212 loc) · 7.36 KB
/
oculusVR.cs
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
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
// Only load these functions if an Oculus VR device is present
if(!isFunction(isOculusVRDeviceActive))
return;
function setupOculusActionMaps()
{
if (isObject(OculusWarningMap))
return;
new ActionMap(OculusWarningMap);
new ActionMap(OculusCanvasMap);
OculusWarningMap.bind(keyboard, space, dismissOculusVRWarnings);
OculusCanvasMap.bind( mouse, xaxis, oculusYaw );
OculusCanvasMap.bind( mouse, yaxis, oculusPitch );
OculusCanvasMap.bind( mouse, button0, oculusClick );
}
function oculusYaw(%val)
{
OculusCanvas.cursorNudge(%val * 0.10, 0);
}
function oculusPitch(%val)
{
OculusCanvas.cursorNudge(0, %val * 0.10);
}
function oculusClick(%active)
{
OculusCanvas.cursorClick(0, %active);
}
function GuiOffscreenCanvas::checkCursor(%this)
{
%count = %this.getCount();
for(%i = 0; %i < %count; %i++)
{
%control = %this.getObject(%i);
if ((%control.noCursor $= "") || !%control.noCursor)
{
%this.cursorOn();
return true;
}
}
// If we get here, every control requested a hidden cursor, so we oblige.
%this.cursorOff();
return false;
}
function GuiOffscreenCanvas::pushDialog(%this, %ctrl, %layer, %center)
{
Parent::pushDialog(%this, %ctrl, %layer, %center);
%cursorVisible = %this.checkCursor();
if (%cursorVisible)
{
echo("OffscreenCanvas visible");
OculusCanvasMap.pop();
OculusCanvasMap.push();
}
else
{
echo("OffscreenCanvas not visible");
OculusCanvasMap.pop();
}
}
function GuiOffscreenCanvas::popDialog(%this, %ctrl)
{
Parent::popDialog(%this, %ctrl);
%cursorVisible = %this.checkCursor();
if (%cursorVisible)
{
echo("OffscreenCanvas visible");
OculusCanvasMap.pop();
OculusCanvasMap.push();
}
else
{
echo("OffscreenCanvas not visible");
OculusCanvasMap.pop();
}
}
//-----------------------------------------------------------------------------
function oculusSensorMetricsCallback()
{
return ovrDumpMetrics(0);
}
//-----------------------------------------------------------------------------
function onOculusStatusUpdate(%status)
{
$LastOculusTrackingState = %status;
}
//-----------------------------------------------------------------------------
// Call this function from createCanvas() to have the Canvas attach itself
// to the Rift's display. The Canvas' window will still open on the primary
// display if that is different from the Rift, but it will move to the Rift
// when it goes full screen. If the Rift is not connected then nothing
// will happen.
function pointCanvasToOculusVRDisplay()
{
$pref::Video::displayOutputDevice = getOVRHMDDisplayDeviceName(0);
}
//-----------------------------------------------------------------------------
// Call this function from GameConnection::initialControlSet() just before
// your "Canvas.setContent(PlayGui);" call, or at any time you wish to switch
// to a side-by-side rendering and the appropriate barrel distortion. This
// will turn on side-by-side rendering and tell the GameConnection to use the
// Rift as its display device.
// Parameters:
// %gameConnection - The client GameConnection instance
// %trueStereoRendering - If true will enable stereo rendering with an eye
// offset for each viewport. This will render each frame twice. If false
// then a pseudo stereo rendering is done with only a single render per frame.
function enableOculusVRDisplay(%gameConnection, %trueStereoRendering)
{
setOVRHMDAsGameConnectionDisplayDevice(%gameConnection);
PlayGui.renderStyle = "stereo side by side";
setOptimalOVRCanvasSize(Canvas);
if (!isObject(OculusCanvas))
{
new GuiOffscreenCanvas(OculusCanvas) {
targetSize = "512 512";
targetName = "oculusCanvas";
dynamicTarget = true;
};
}
if (!isObject(OculusVROverlay))
{
exec("./oculusVROverlay.gui");
}
OculusCanvas.setContent(OculusVROverlay);
OculusCanvas.setCursor(DefaultCursor);
PlayGui.setStereoGui(OculusCanvas);
OculusCanvas.setCursorPos("128 128");
OculusCanvas.cursorOff();
$GameCanvas = OculusCanvas;
%ext = Canvas.getExtent();
$OculusMouseScaleX = 512.0 / 1920.0;
$OculusMouseScaleY = 512.0 / 1060.0;
//$gfx::wireframe = true;
// Reset all sensors
ovrResetAllSensors();
}
// Call this function when ever you wish to turn off the stereo rendering
// and barrel distortion for the Rift.
function disableOculusVRDisplay(%gameConnection)
{
OculusCanvas.popDialog();
OculusWarningMap.pop();
$GameCanvas = Canvas;
if (isObject(gameConnection))
{
%gameConnection.clearDisplayDevice();
}
PlayGui.renderStyle = "standard";
}
// Helper function to set the standard Rift control scheme. You could place
// this function in GameConnection::initialControlSet() at the same time
// you call enableOculusVRDisplay().
function setStandardOculusVRControlScheme(%gameConnection)
{
if($OculusVR::SimulateInput)
{
// We are simulating a HMD so allow the mouse and gamepad to control
// both yaw and pitch.
%gameConnection.setControlSchemeParameters(true, true, true);
}
else
{
// A HMD is connected so have the mouse and gamepad only add to yaw
%gameConnection.setControlSchemeParameters(true, true, false);
}
}
//-----------------------------------------------------------------------------
// Helper function to set the resolution for the Rift.
// Parameters:
// %fullscreen - If true then the display will be forced to full screen. If
// pointCanvasToOculusVRDisplay() was called before the Canvas was created, then
// the full screen display will appear on the Rift.
function setVideoModeForOculusVRDisplay(%fullscreen)
{
%res = getOVRHMDResolution(0);
Canvas.setVideoMode(%res.x, %res.y, %fullscreen, 32, 4);
}
//-----------------------------------------------------------------------------
// Reset all Oculus Rift sensors. This will make the Rift's current heading
// be considered the origin.
function resetOculusVRSensors()
{
ovrResetAllSensors();
}
function dismissOculusVRWarnings(%value)
{
//if (%value)
//{
ovrDismissWarnings();
OculusWarningMap.pop();
//}
}