encapsulate vr interface

This commit is contained in:
2016-09-10 12:12:02 -07:00
parent bc76738f53
commit 6e3ba7f6db
4 changed files with 85 additions and 52 deletions

67
src/vr.rs Normal file
View File

@@ -0,0 +1,67 @@
extern crate gfx;
extern crate gfx_device_gl;
extern crate openvr as vr;
extern crate openvr_sys;
use self::gfx::{tex, Factory, Typed};
pub use self::vr::Eye;
pub struct VR {
system: vr::IVRSystem,
compositor: vr::IVRCompositor,
gfx_handles: gfx::handle::Manager<gfx_device_gl::Resources>,
}
impl VR {
pub fn new() -> Result<VR, vr::Error<openvr_sys::EVRInitError>> {
Ok(VR {
system: try!(vr::init()),
compositor: try!(vr::compositor()),
gfx_handles: gfx::handle::Manager::new(),
})
}
pub fn poses(&mut self) -> vr::tracking::TrackedDevicePoses {
self.gfx_handles.clear();
self.compositor.wait_get_poses()
}
pub fn submit<T>(&mut self, eye: Eye, tex: &gfx::handle::Texture<gfx_device_gl::Resources, T>) {
let tex_id = match self.gfx_handles.ref_texture(tex.raw()) {
&gfx_device_gl::NewTexture::Surface(id) => id,
_ => panic!("Not a surface")
};
self.compositor.submit(eye,
tex_id as usize,
vr::common::TextureBounds::new((0.0, 1.0), (0.0, 1.0)));
}
pub fn recommended_render_target_size(&self) -> vr::common::Size {
self.system.recommended_render_target_size()
}
}
impl Drop for VR {
fn drop(&mut self) {
vr::shutdown()
}
}
pub fn create_eyebuffer<T>(factory: &mut gfx_device_gl::Factory,
size: vr::common::Size)
-> Result<(gfx::handle::Texture<gfx_device_gl::Resources,
T::Surface>,
gfx::handle::RenderTargetView<gfx_device_gl::Resources,
T>),
gfx::CombinedError>
where T: gfx::format::RenderFormat + gfx::format::TextureFormat {
let tex = try!(factory.create_texture(
tex::Kind::D2(size.width as tex::Size, size.height as tex::Size, tex::AaMode::Single),
1, // levels
gfx::RENDER_TARGET, // bind
gfx::Usage::GpuOnly, // Usage
Some(<T::Channel as gfx::format::ChannelTyped>::get_channel_type()))); // hint: format::ChannelType?
let tgt = try!(factory.view_texture_as_render_target(&tex, 0, None));
Ok((tex, tgt))
}