remove bitrot, update and set versions of all deps, notably gfx & nalgebra
This commit is contained in:
160
src/vr.rs
160
src/vr.rs
@@ -1,60 +1,84 @@
|
||||
extern crate gfx_device_gl;
|
||||
extern crate openvr as vr;
|
||||
extern crate openvr_sys;
|
||||
|
||||
pub use self::vr::Eye;
|
||||
pub use self::vr::common::Size;
|
||||
pub use self::openvr_sys::VRControllerState_t;
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub use self::vr::{compositor::texture::{ColorSpace, Handle, Texture},
|
||||
ControllerState,
|
||||
Eye,
|
||||
system::Event as OvrEvent};
|
||||
|
||||
use gfx::{self, texture, Factory};
|
||||
use gfx::memory::Typed;
|
||||
use na::{self, Inverse};
|
||||
use num_traits::identities::Zero;
|
||||
use num_traits::identities::One;
|
||||
use na;
|
||||
|
||||
use self::gfx_device_gl::Resources as GLResources;
|
||||
use self::openvr_sys::{VREvent_Controller_t, VREvent_t};
|
||||
|
||||
pub struct VR {
|
||||
system: vr::IVRSystem,
|
||||
compositor: vr::IVRCompositor,
|
||||
_context: vr::Context,
|
||||
system: vr::System,
|
||||
compositor: vr::Compositor,
|
||||
origin: vr::TrackingUniverseOrigin,
|
||||
gfx_handles: gfx::handle::Manager<GLResources>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Event {
|
||||
Touch { dev_idx: u32, controller: VREvent_Controller_t },
|
||||
Press { dev_idx: u32, controller: VREvent_Controller_t },
|
||||
Unpress { dev_idx: u32, controller: VREvent_Controller_t },
|
||||
Untouch { dev_idx: u32, controller: VREvent_Controller_t },
|
||||
Other(VREvent_t),
|
||||
Touch { dev_idx: u32, button: u32 },
|
||||
Press { dev_idx: u32, button: u32 },
|
||||
Unpress { dev_idx: u32, button: u32 },
|
||||
Untouch { dev_idx: u32, button: u32 },
|
||||
Other { dev_idx: u32, event: OvrEvent },
|
||||
}
|
||||
|
||||
pub use self::vr::button_id;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Init(Box<Debug>),
|
||||
Poses(Box<Debug>),
|
||||
Submit(Box<Debug>)
|
||||
}
|
||||
|
||||
impl From<vr::InitError> for Error {
|
||||
fn from(e: vr::InitError) -> Self {
|
||||
Error::Init(Box::new(e))
|
||||
}
|
||||
}
|
||||
|
||||
impl VR {
|
||||
pub fn new() -> Result<VR, vr::Error<openvr_sys::EVRInitError>> {
|
||||
pub fn new() -> Result<VR, vr::InitError> {
|
||||
let context = unsafe { vr::init(vr::ApplicationType::Scene)? };
|
||||
Ok(VR {
|
||||
system: vr::init()?,
|
||||
compositor: vr::compositor()?,
|
||||
system: context.system()?,
|
||||
compositor: context.compositor()?,
|
||||
_context: context,
|
||||
origin: vr::TrackingUniverseOrigin::Standing,
|
||||
gfx_handles: gfx::handle::Manager::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn poses(&mut self) -> vr::tracking::TrackedDevicePoses {
|
||||
pub fn poses(&mut self) -> Result<vr::TrackedDevicePoses, Error> {
|
||||
self.gfx_handles.clear();
|
||||
self.compositor.wait_get_poses()
|
||||
self.compositor.wait_get_poses().map(|p| p.render).map_err(|e| Error::Poses(Box::new(e)))
|
||||
}
|
||||
|
||||
pub fn submit<T>(&mut self, eye: Eye, tex: &gfx::handle::Texture<GLResources, T>) {
|
||||
pub fn submit<T>(&mut self, eye: Eye, tex: &gfx::handle::Texture<GLResources, T>)
|
||||
-> Result<(), Error> {
|
||||
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)));
|
||||
unsafe {
|
||||
self.compositor.submit(eye,
|
||||
&Texture { handle: Handle::OpenGLRenderBuffer(tex_id as usize),
|
||||
color_space: ColorSpace::Linear },
|
||||
None,
|
||||
None).map_err(|e| Error::Submit(Box::new(e)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn recommended_render_target_size(&self) -> Size {
|
||||
pub fn recommended_render_target_size(&self) -> (u32, u32) {
|
||||
self.system.recommended_render_target_size()
|
||||
}
|
||||
|
||||
@@ -64,74 +88,52 @@ impl VR {
|
||||
|
||||
pub fn head_to_eye_transform(self: &Self, eye: Eye) -> na::Matrix4<f32> {
|
||||
let mut mat = self.system.eye_to_head_transform(eye).as_matrix4();
|
||||
assert!(mat.inverse_mut(), "inverse eye matrix");
|
||||
let inv_worked = mat.try_inverse_mut();
|
||||
assert!(inv_worked, "head_to_eye matrix invert");
|
||||
mat
|
||||
}
|
||||
|
||||
pub fn poll_next_event(&mut self) -> Option<Event> {
|
||||
use self::openvr_sys::EVREventType as EvType;
|
||||
unsafe {
|
||||
let system = * { self.system.0 as *mut openvr_sys::VR_IVRSystem_FnTable };
|
||||
let mut event: openvr_sys::VREvent_t = ::std::mem::zeroed();
|
||||
let (event, _) = self.system.poll_next_event_with_pose(self.origin)?;
|
||||
|
||||
if system.PollNextEvent.unwrap()(&mut event,
|
||||
::std::mem::size_of::<openvr_sys::VREvent_t>() as u32
|
||||
) == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let dev_idx = event.trackedDeviceIndex;
|
||||
Some(match ::std::mem::transmute(event.eventType) {
|
||||
EvType::EVREventType_VREvent_ButtonTouch =>
|
||||
Event::Touch { dev_idx: dev_idx as u32, controller: *event.data.controller() },
|
||||
EvType::EVREventType_VREvent_ButtonPress =>
|
||||
Event::Press { dev_idx: dev_idx as u32, controller: *event.data.controller() },
|
||||
EvType::EVREventType_VREvent_ButtonUnpress =>
|
||||
Event::Unpress { dev_idx: dev_idx as u32, controller: *event.data.controller() },
|
||||
EvType::EVREventType_VREvent_ButtonUntouch =>
|
||||
Event::Untouch { dev_idx: dev_idx as u32, controller: *event.data.controller() },
|
||||
_ => Event::Other(event),
|
||||
})
|
||||
}
|
||||
let dev_idx = event.tracked_device_index as u32;
|
||||
Some(match event.event {
|
||||
OvrEvent::ButtonTouch(controller) => Event::Touch { dev_idx: dev_idx,
|
||||
button: controller.button },
|
||||
OvrEvent::ButtonPress(controller) => Event::Press { dev_idx: dev_idx,
|
||||
button: controller.button },
|
||||
OvrEvent::ButtonUnpress(controller) => Event::Unpress { dev_idx: dev_idx,
|
||||
button: controller.button },
|
||||
OvrEvent::ButtonUntouch(controller) => Event::Untouch { dev_idx: dev_idx,
|
||||
button: controller.button },
|
||||
_ => Event::Other { dev_idx: dev_idx, event: event.event },
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_controller_state(&self, index: u32) -> Option<openvr_sys::VRControllerState_t> {
|
||||
unsafe {
|
||||
let system = * { self.system.0 as *const openvr_sys::VR_IVRSystem_FnTable };
|
||||
let mut state: openvr_sys::VRControllerState_t = ::std::mem::zeroed();
|
||||
|
||||
match system.GetControllerState.unwrap()(
|
||||
index,
|
||||
&mut state,
|
||||
) {
|
||||
0 => None,
|
||||
_ => Some(state)
|
||||
}
|
||||
}
|
||||
pub fn get_controller_state(&self, index: u32) -> Option<ControllerState> {
|
||||
self.system.controller_state(index)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for VR {
|
||||
fn drop(&mut self) {
|
||||
vr::shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AsMatrix4<N> {
|
||||
pub trait AsMatrix4<N: na::Real> {
|
||||
fn as_matrix4(self) -> na::Matrix4<N>;
|
||||
}
|
||||
impl<N: Copy + Zero + One> AsMatrix4<N> for [[N; 4]; 3] {
|
||||
impl<N: na::Real> AsMatrix4<N> for [[N; 4]; 3] {
|
||||
#[inline]
|
||||
fn as_matrix4(self) -> na::Matrix4<N> {
|
||||
|
||||
na::Matrix4::new(self[0][0], self[0][1], self[0][2], self[0][3],
|
||||
self[1][0], self[1][1], self[1][2], self[1][3],
|
||||
self[2][0], self[2][1], self[2][2], self[2][3],
|
||||
N::zero(), N::zero(), N::zero(), N::one())
|
||||
}
|
||||
}
|
||||
impl<N: Copy> AsMatrix4<N> for [[N; 4]; 4] {
|
||||
|
||||
|
||||
impl<N: na::Real> AsMatrix4<N> for [[N; 4]; 4] {
|
||||
#[inline]
|
||||
fn as_matrix4(self) -> na::Matrix4<N> {
|
||||
|
||||
na::Matrix4::new(self[0][0], self[0][1], self[0][2], self[0][3],
|
||||
self[1][0], self[1][1], self[1][2], self[1][3],
|
||||
self[2][0], self[2][1], self[2][2], self[2][3],
|
||||
@@ -139,6 +141,7 @@ impl<N: Copy> AsMatrix4<N> for [[N; 4]; 4] {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct EyeBuffer<T, D>
|
||||
where T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
||||
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
||||
@@ -149,20 +152,21 @@ pub struct EyeBuffer<T, D>
|
||||
}
|
||||
|
||||
pub fn create_eyebuffer<T, D>(factory: &mut gfx_device_gl::Factory,
|
||||
size: Size)
|
||||
width: u32,
|
||||
height: u32)
|
||||
-> Result<EyeBuffer<T, D>, gfx::CombinedError>
|
||||
where T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
||||
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
||||
let tex = factory
|
||||
.create_texture(texture::Kind::D2(size.width as texture::Size,
|
||||
size.height as texture::Size,
|
||||
.create_texture(texture::Kind::D2(width as texture::Size,
|
||||
height as texture::Size,
|
||||
texture::AaMode::Single),
|
||||
1, // levels
|
||||
gfx::RENDER_TARGET, // bind
|
||||
gfx::memory::Usage::GpuOnly, // Usage
|
||||
gfx::memory::Bind::RENDER_TARGET, // bind
|
||||
gfx::memory::Usage::Data, // Usage
|
||||
Some(<T::Channel as gfx::format::ChannelTyped>::get_channel_type()))?; // hint: format::ChannelType?
|
||||
let tgt = factory.view_texture_as_render_target(&tex, 0, None)?;
|
||||
let depth = factory.create_depth_stencil_view_only(size.width as texture::Size,
|
||||
size.height as texture::Size)?;
|
||||
let depth = factory.create_depth_stencil_view_only(width as texture::Size,
|
||||
height as texture::Size)?;
|
||||
Ok(EyeBuffer { tex: tex, target: tgt, depth: depth })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user