formatting cleanup
This commit is contained in:
@@ -40,11 +40,4 @@ void main() {
|
|||||||
vec3 normal = vec3(toroid(thetaphi, 0, height, height));
|
vec3 normal = vec3(toroid(thetaphi, 0, height, height));
|
||||||
gl_Position = u_matrix * camera *
|
gl_Position = u_matrix * camera *
|
||||||
vec4(toroid(thetaphi, R1, R2, R1) + a_pos.z * normal, 1.0);
|
vec4(toroid(thetaphi, R1, R2, R1) + a_pos.z * normal, 1.0);
|
||||||
/*
|
|
||||||
gl_Position = u_matrix * camera *
|
|
||||||
vec4(R2 * -1.0 * sin(TWO_PI_CIRC * (a_pos.x + treadmill_x)),
|
|
||||||
(R1 + R2 * cos(TWO_PI_CIRC * (a_pos.x + treadmill_x))) * cos(TWO_PI_CIRC * (a_pos.z + treadmill_y)),
|
|
||||||
(R1 + R2 * cos(TWO_PI_CIRC * (a_pos.x + treadmill_x))) * sin(TWO_PI_CIRC * (a_pos.z + treadmill_y)),
|
|
||||||
1.0);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
224
src/vr.rs
224
src/vr.rs
@@ -17,127 +17,127 @@ use self::num_traits::identities::One;
|
|||||||
use self::openvr_sys::{VREvent_Controller_t, VREvent_t};
|
use self::openvr_sys::{VREvent_Controller_t, VREvent_t};
|
||||||
|
|
||||||
pub struct VR {
|
pub struct VR {
|
||||||
system: vr::IVRSystem,
|
system: vr::IVRSystem,
|
||||||
compositor: vr::IVRCompositor,
|
compositor: vr::IVRCompositor,
|
||||||
gfx_handles: gfx::handle::Manager<GLResources>,
|
gfx_handles: gfx::handle::Manager<GLResources>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
Touch { dev_idx: u32, controller: VREvent_Controller_t },
|
Touch { dev_idx: u32, controller: VREvent_Controller_t },
|
||||||
Press { dev_idx: u32, controller: VREvent_Controller_t },
|
Press { dev_idx: u32, controller: VREvent_Controller_t },
|
||||||
Unpress { dev_idx: u32, controller: VREvent_Controller_t },
|
Unpress { dev_idx: u32, controller: VREvent_Controller_t },
|
||||||
Untouch { dev_idx: u32, controller: VREvent_Controller_t },
|
Untouch { dev_idx: u32, controller: VREvent_Controller_t },
|
||||||
Other(VREvent_t),
|
Other(VREvent_t),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VR {
|
impl VR {
|
||||||
pub fn new() -> Result<VR, vr::Error<openvr_sys::EVRInitError>> {
|
pub fn new() -> Result<VR, vr::Error<openvr_sys::EVRInitError>> {
|
||||||
Ok(VR {
|
Ok(VR {
|
||||||
system: try!(vr::init()),
|
system: try!(vr::init()),
|
||||||
compositor: try!(vr::compositor()),
|
compositor: try!(vr::compositor()),
|
||||||
gfx_handles: gfx::handle::Manager::new(),
|
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<GLResources, 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) -> Size {
|
||||||
|
self.system.recommended_render_target_size()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn projection_matrix(self: &Self, eye: Eye, near: f32, far: f32) -> na::Matrix4<f32> {
|
||||||
|
self.system.projection_matrix(eye, near, far).as_matrix4()
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
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();
|
||||||
|
|
||||||
|
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),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn poses(&mut self) -> vr::tracking::TrackedDevicePoses {
|
pub fn get_controller_state(&self, index: u32) -> Option<openvr_sys::VRControllerState_t> {
|
||||||
self.gfx_handles.clear();
|
unsafe {
|
||||||
self.compositor.wait_get_poses()
|
let system = * { self.system.0 as *const openvr_sys::VR_IVRSystem_FnTable };
|
||||||
}
|
let mut state: openvr_sys::VRControllerState_t = ::std::mem::zeroed();
|
||||||
|
|
||||||
pub fn submit<T>(&mut self, eye: Eye, tex: &gfx::handle::Texture<GLResources, T>) {
|
match system.GetControllerState.unwrap()(
|
||||||
let tex_id = match self.gfx_handles.ref_texture(tex.raw()) {
|
index,
|
||||||
&gfx_device_gl::NewTexture::Surface(id) => id,
|
&mut state,
|
||||||
_ => panic!("Not a surface")
|
) {
|
||||||
};
|
0 => None,
|
||||||
self.compositor.submit(eye,
|
_ => Some(state)
|
||||||
tex_id as usize,
|
}
|
||||||
vr::common::TextureBounds::new((0.0, 1.0), (0.0, 1.0)));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn recommended_render_target_size(&self) -> Size {
|
|
||||||
self.system.recommended_render_target_size()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn projection_matrix(self: &Self, eye: Eye, near: f32, far: f32) -> na::Matrix4<f32> {
|
|
||||||
self.system.projection_matrix(eye, near, far).as_matrix4()
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
|
||||||
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();
|
|
||||||
|
|
||||||
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),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for VR {
|
impl Drop for VR {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
vr::shutdown()
|
vr::shutdown()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait AsMatrix4<N> {
|
pub trait AsMatrix4<N> {
|
||||||
fn as_matrix4(self) -> na::Matrix4<N>;
|
fn as_matrix4(self) -> na::Matrix4<N>;
|
||||||
}
|
}
|
||||||
impl<N: Copy + Zero + One> AsMatrix4<N> for [[N; 4]; 3] {
|
impl<N: Copy + Zero + One> AsMatrix4<N> for [[N; 4]; 3] {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_matrix4(self) -> na::Matrix4<N> {
|
fn as_matrix4(self) -> na::Matrix4<N> {
|
||||||
na::Matrix4::new(self[0][0], self[0][1], self[0][2], self[0][3],
|
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[1][0], self[1][1], self[1][2], self[1][3],
|
||||||
self[2][0], self[2][1], self[2][2], self[2][3],
|
self[2][0], self[2][1], self[2][2], self[2][3],
|
||||||
N::zero(), N::zero(), N::zero(), N::one())
|
N::zero(), N::zero(), N::zero(), N::one())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<N: Copy> AsMatrix4<N> for [[N; 4]; 4] {
|
impl<N: Copy> AsMatrix4<N> for [[N; 4]; 4] {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_matrix4(self) -> na::Matrix4<N> {
|
fn as_matrix4(self) -> na::Matrix4<N> {
|
||||||
na::Matrix4::new(self[0][0], self[0][1], self[0][2], self[0][3],
|
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[1][0], self[1][1], self[1][2], self[1][3],
|
||||||
self[2][0], self[2][1], self[2][2], self[2][3],
|
self[2][0], self[2][1], self[2][2], self[2][3],
|
||||||
self[3][0], self[3][1], self[3][2], self[3][3])
|
self[3][0], self[3][1], self[3][2], self[3][3])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EyeBuffer<T, D>
|
pub struct EyeBuffer<T, D>
|
||||||
@@ -152,16 +152,16 @@ pub struct EyeBuffer<T, D>
|
|||||||
pub fn create_eyebuffer<T, D>(factory: &mut gfx_device_gl::Factory,
|
pub fn create_eyebuffer<T, D>(factory: &mut gfx_device_gl::Factory,
|
||||||
size: Size)
|
size: Size)
|
||||||
-> Result<EyeBuffer<T, D>, gfx::CombinedError>
|
-> Result<EyeBuffer<T, D>, gfx::CombinedError>
|
||||||
where T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
where T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
||||||
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
||||||
let tex = try!(factory.create_texture(
|
let tex = try!(factory.create_texture(
|
||||||
tex::Kind::D2(size.width as tex::Size, size.height as tex::Size, tex::AaMode::Single),
|
tex::Kind::D2(size.width as tex::Size, size.height as tex::Size, tex::AaMode::Single),
|
||||||
1, // levels
|
1, // levels
|
||||||
gfx::RENDER_TARGET, // bind
|
gfx::RENDER_TARGET, // bind
|
||||||
gfx::Usage::GpuOnly, // Usage
|
gfx::Usage::GpuOnly, // Usage
|
||||||
Some(<T::Channel as gfx::format::ChannelTyped>::get_channel_type()))); // hint: format::ChannelType?
|
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));
|
let tgt = try!(factory.view_texture_as_render_target(&tex, 0, None));
|
||||||
let depth = try!(factory.create_depth_stencil_view_only(size.width as tex::Size,
|
let depth = try!(factory.create_depth_stencil_view_only(size.width as tex::Size,
|
||||||
size.height as tex::Size));
|
size.height as tex::Size));
|
||||||
Ok(EyeBuffer { tex: tex, target: tgt, depth: depth })
|
Ok(EyeBuffer { tex: tex, target: tgt, depth: depth })
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user