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
#[macro_use] extern crate bitflags;
extern crate libc;
#[cfg(windows)] extern crate winapi;
#[cfg(windows)] extern crate kernel32;
#[cfg(feature = "glutin")]
extern crate glutin;
use std::rc::Rc;
use std::fmt;
mod ffi;
mod shim;
pub use shim::HmdDisplayId;
pub use shim::HmdDisplay;
pub mod render;
pub mod target;
#[derive(Clone, Debug)]
pub enum OculusError {
OculusRuntimeError(String),
SdkError(&'static str),
DuplicateContext
}
impl fmt::Display for OculusError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&OculusError::OculusRuntimeError(ref description) => f.write_str(description),
&OculusError::SdkError(ref description) => f.write_str(description),
&OculusError::DuplicateContext => f.write_str(
"Context creation failed because another Context is already active in this process")
}
}
}
#[derive(Copy, Clone)]
pub enum Eye {
Left,
Right
}
pub struct Context {
shim_context: Rc<shim::Context>
}
impl Context {
pub fn new() -> Result<Context, OculusError> {
let shim_context = Rc::new(try!(shim::Context::new()));
Ok(Context {
shim_context: shim_context
})
}
pub fn build_hmd(&self) -> HmdBuilder {
HmdBuilder::new(self.shim_context.clone())
}
}
pub struct TrackingOptions {
track_caps: ffi::ovrTrackingCaps
}
impl TrackingOptions {
pub fn new() -> TrackingOptions {
TrackingOptions {
track_caps: ffi::ovrTrackingCaps::empty()
}
}
pub fn with_all() -> TrackingOptions {
TrackingOptions {
track_caps: ffi::ovrTrackingCap_Orientation |
ffi::ovrTrackingCap_MagYawCorrection |
ffi::ovrTrackingCap_Position
}
}
pub fn position<'f>(&'f mut self) -> &'f mut TrackingOptions {
self.track_caps.insert(ffi::ovrTrackingCap_Position);
self
}
pub fn orientation<'f>(&'f mut self) -> &'f mut TrackingOptions {
self.track_caps.insert(ffi::ovrTrackingCap_Orientation);
self
}
pub fn mag_yaw_correct<'f>(&'f mut self) -> &'f mut TrackingOptions {
self.track_caps.insert(ffi::ovrTrackingCap_MagYawCorrection);
self
}
}
pub struct HmdBuilder {
caps: ffi::ovrHmdCaps,
track_caps: ffi::ovrTrackingCaps,
allow_debug: bool,
owning_context: Rc<shim::Context>
}
impl HmdBuilder {
fn new(owning_context: Rc<shim::Context>) -> HmdBuilder {
let default_caps = ffi::ovrHmdCap_LowPersistence | ffi::ovrHmdCap_DynamicPrediction;
HmdBuilder {
caps: default_caps,
track_caps: ffi::ovrTrackingCaps::empty(),
allow_debug: false,
owning_context: owning_context
}
}
pub fn no_mirror<'f>(&'f mut self) -> &'f mut HmdBuilder {
self.caps.insert(ffi::ovrHmdCap_NoMirrorToWindow);
self
}
pub fn no_display<'f>(&'f mut self) -> &'f mut HmdBuilder {
self.caps.insert(ffi::ovrHmdCap_DisplayOff);
self
}
pub fn no_low_persistence<'f>(&'f mut self) -> &'f mut HmdBuilder {
self.caps.remove(ffi::ovrHmdCap_LowPersistence);
self
}
pub fn no_dynamic_prediction<'f>(&'f mut self) -> &'f mut HmdBuilder {
self.caps.remove(ffi::ovrHmdCap_DynamicPrediction);
self
}
pub fn no_vsync<'f>(&'f mut self) -> &'f mut HmdBuilder {
self.caps.insert(ffi::ovrHmdCap_NoVSync);
self
}
pub fn track<'f>(&'f mut self, tracking_options: &TrackingOptions) -> &'f mut HmdBuilder {
self.track_caps = tracking_options.track_caps;
self
}
pub fn allow_debug<'f>(&'f mut self) -> &'f mut HmdBuilder {
self.allow_debug = true;
self
}
pub fn build(&self) -> Result<Hmd, OculusError> {
Hmd::new(self.caps, self.track_caps, self.allow_debug, self.owning_context.clone())
}
}
pub trait RenderTarget {
fn get_multisample(&self) -> u32;
unsafe fn get_native_window(&self) -> *const libc::c_void;
}
pub struct Hmd {
shim_hmd: shim::Hmd
}
impl Hmd {
fn new(caps: ffi::ovrHmdCaps,
track_caps: ffi::ovrTrackingCaps,
allow_debug: bool,
owning_context: Rc<shim::Context>) -> Result<Hmd, OculusError> {
let mut shim_hmd = try!(shim::Hmd::new(allow_debug, owning_context));
shim_hmd.set_caps(caps);
if !track_caps.is_empty() {
try!(shim_hmd.configure_tracking(track_caps));
}
Ok(Hmd{ shim_hmd: shim_hmd })
}
pub fn render_to<'a>(&'a self,
target: &'a RenderTarget) -> Result<render::RenderContext, OculusError> {
use shim::CreateRenderContext;
render::RenderContext::new(&self.shim_hmd, target)
}
pub fn resolution(&self) -> (u32, u32) {
self.shim_hmd.resolution()
}
pub fn get_display(&self) -> HmdDisplay {
self.shim_hmd.get_display()
}
}