tracked device refactoring + read device property

This commit is contained in:
Rene Eichhorn
2016-05-05 20:03:47 +02:00
parent 95b15050e4
commit 5801325a54
6 changed files with 120 additions and 49 deletions

View File

@ -23,6 +23,20 @@ pub fn main() {
}
};
// init render model subsystem
let models = match openvr::render_models() {
Ok(ext) => ext,
Err(err) => {
println!("Failed to create IVRRenderModels subsystem {:?}", err);
return;
}
};
for device in system.tracked_devices(0.0).connected_iter() {
println!("device found :) -> {}",
device.get_property_string(openvr::ETrackedDeviceProperty_Prop_RenderModelName_String).unwrap_or_else(|_| { panic!("No render model")} ));
}
// init compositor subsystem
/*let comp = match openvr::compositor() {
Ok(ext) => ext,
@ -32,14 +46,6 @@ pub fn main() {
}
};*/
// init render model subsystem
let models = match openvr::render_models() {
Ok(ext) => ext,
Err(err) => {
println!("Failed to create IVRRenderModels subsystem {:?}", err);
return;
}
};
// create glium window and context
use glium::{DisplayBuild, Surface};
@ -86,7 +92,7 @@ pub fn main() {
let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap();
// load controller models
let controller = models.load(String::from("vr_controller_vive_1_5")).unwrap_or_else(|err| {
let controller = models.load(String::from("generic_hmd")).unwrap_or_else(|err| {
openvr::shutdown(); panic!("controller render model not found: {:?}", err) });
let mut controller_vertices: Vec<Vertex> = Vec::new();

View File

@ -60,40 +60,3 @@ impl TextureBounds {
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct TrackedDevicePose {
pub to_device: [[f32; 4]; 3],
pub velocity: [f32; 3],
pub angular_velocity: [f32; 3],
pub is_valid: bool,
pub is_connected: bool,
}
#[derive(Debug, Copy, Clone)]
pub struct TrackedDevicePoses {
pub count: usize,
pub poses: [TrackedDevicePose; 16],
}
impl TrackedDevicePoses {
pub fn as_slice(&self) -> &[TrackedDevicePose] {
&self.poses[0..self.count]
}
}
pub unsafe fn to_tracked(data: [openvr_sys::TrackedDevicePose_t; 16]) -> TrackedDevicePoses {
use std;
let mut out: TrackedDevicePoses = std::mem::zeroed();
for (i, d) in data.iter().enumerate() {
if d.bDeviceIsConnected > 0 {
out.count = i + 1;
}
out.poses[i].is_connected = d.bDeviceIsConnected > 0;
out.poses[i].is_valid = d.bPoseIsValid > 0;
out.poses[i].to_device = d.mDeviceToAbsoluteTracking.m;
out.poses[i].velocity = d.vVelocity.v;
out.poses[i].angular_velocity = d.vAngularVelocity.v;
}
out
}

View File

@ -3,6 +3,7 @@ use openvr_sys::Enum_EGraphicsAPIConvention::*;
use openvr_sys::Enum_EVRSubmitFlags::*;
use openvr_sys::Enum_EColorSpace::*;
use common::*;
use tracking::*;
/// A VR compositor
pub struct IVRCompositor(*const ());

View File

@ -1,8 +1,10 @@
extern crate openvr_sys;
use openvr_sys::Enum_EVRInitError::*;
use openvr_sys::Enum_EVRApplicationType::*;
pub use openvr_sys::Enum_EVRInitError::*;
pub use openvr_sys::Enum_EVRApplicationType::*;
pub use openvr_sys::Enum_ETrackedDeviceProperty::*;
pub mod common;
pub mod tracking;
pub mod system;
pub mod extended_display;
pub mod compositor;

View File

@ -3,8 +3,9 @@ use openvr_sys::Enum_EGraphicsAPIConvention::*;
use openvr_sys::Enum_ETrackingUniverseOrigin::*;
use common::*;
use tracking::*;
pub struct IVRSystem(*const ());
pub struct IVRSystem(pub *const ());
impl IVRSystem {
pub unsafe fn from_raw(ptr: *const ()) -> Self {
@ -90,6 +91,9 @@ impl IVRSystem {
}
/// Fetch the tracked results from the HMD
/// when time is bigger than 0, it will give you the predicted poses for that time
/// Time is counted in photons, see https://github.com/ValveSoftware/openvr/wiki/IVRSystem::GetDeviceToAbsoluteTrackingPose
/// for time to photons conversion
pub fn tracked_devices(&self, time: f32) -> TrackedDevicePoses {
use std;

95
src/tracking.rs Normal file
View File

@ -0,0 +1,95 @@
use openvr_sys;
use openvr_sys::Enum_ETrackedPropertyError::*;
use subsystems::*;
#[derive(Debug, Copy, Clone)]
pub struct TrackedDevicePose {
pub index: usize,
pub to_device: [[f32; 4]; 3],
pub velocity: [f32; 3],
pub angular_velocity: [f32; 3],
pub is_valid: bool,
pub is_connected: bool,
}
impl TrackedDevicePose {
/// gets a propery as a string
pub fn get_property_string(&self, property: openvr_sys::Enum_ETrackedDeviceProperty) -> Result<String, openvr_sys::Enum_ETrackedPropertyError> {
unsafe {
let system = * { system().unwrap().0 as *mut openvr_sys::Struct_VR_IVRSystem_FnTable};
let val_out = String::with_capacity(256);
let mut err = ETrackedPropertyError_TrackedProp_Success;
let size = system.GetStringTrackedDeviceProperty.unwrap()(
self.index as u32,
property,
val_out.as_ptr() as *mut i8,
256,
&mut err
);
if size > 0 {
return Ok(String::from_raw_parts(val_out.as_ptr() as *mut _, (size - 1) as usize, (size - 1) as usize));
} else {
return Err(err);
}
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct TrackedDevicePoses {
pub count: usize,
pub poses: [TrackedDevicePose; 16],
}
pub struct TrackedDevicePosesIterator<'a> {
pub target: &'a TrackedDevicePoses,
pub index: usize
}
impl TrackedDevicePoses {
pub fn as_slice(&self) -> &[TrackedDevicePose] {
&self.poses[0..self.count]
}
/// creates an iterator that will iterate over all connected devices
pub fn connected_iter(&self) -> TrackedDevicePosesIterator {
TrackedDevicePosesIterator { target: self, index: 0 }
}
}
impl<'a> Iterator for TrackedDevicePosesIterator<'a> {
type Item = &'a TrackedDevicePose;
fn next(&mut self) -> Option<&'a TrackedDevicePose> {
// end reached
if self.index == self.target.count {
return None;
}
let res = &self.target.poses[self.index];
self.index += 1;
Some(res)
}
}
pub unsafe fn to_tracked(data: [openvr_sys::TrackedDevicePose_t; 16]) -> TrackedDevicePoses {
use std;
let mut out: TrackedDevicePoses = std::mem::zeroed();
for (i, d) in data.iter().enumerate() {
if d.bDeviceIsConnected > 0 {
out.count = i + 1;
}
out.poses[i].index = i;
out.poses[i].is_connected = d.bDeviceIsConnected > 0;
out.poses[i].is_valid = d.bPoseIsValid > 0;
out.poses[i].to_device = d.mDeviceToAbsoluteTracking.m;
out.poses[i].velocity = d.vVelocity.v;
out.poses[i].angular_velocity = d.vAngularVelocity.v;
}
out
}