hmd pose from view space

This commit is contained in:
galister
2024-08-09 07:53:27 +09:00
parent b6a07c43a5
commit 50c3f49989
5 changed files with 39 additions and 17 deletions

2
Cargo.lock generated
View File

@@ -2682,6 +2682,7 @@ version = "0.10.0"
source = "git+https://github.com/galister/openxrs?rev=af4a55d#af4a55df60125491c80c61464c824219c6019b76" source = "git+https://github.com/galister/openxrs?rev=af4a55d#af4a55df60125491c80c61464c824219c6019b76"
dependencies = [ dependencies = [
"libc", "libc",
"mint",
] ]
[[package]] [[package]]
@@ -4599,6 +4600,7 @@ dependencies = [
"libmonado-rs", "libmonado-rs",
"log", "log",
"log-panics", "log-panics",
"mint",
"once_cell", "once_cell",
"openxr", "openxr",
"ovr_overlay", "ovr_overlay",

View File

@@ -34,6 +34,7 @@ log = "0.4.21"
once_cell = "1.19.0" once_cell = "1.19.0"
openxr = { git = "https://github.com/galister/openxrs", rev = "af4a55d", features = [ openxr = { git = "https://github.com/galister/openxrs", rev = "af4a55d", features = [
"linked", "linked",
"mint",
], optional = true } ], optional = true }
ovr_overlay = { features = [ ovr_overlay = { features = [
"ovr_input", "ovr_input",
@@ -67,6 +68,7 @@ xcb = { version = "1.4.0", optional = true, features = [
image_dds = { version = "0.6.0", default-features = false, features = [ image_dds = { version = "0.6.0", default-features = false, features = [
"ddsfile", "ddsfile",
] } ] }
mint = "0.5.9"
[features] [features]
default = ["openvr", "openxr", "osc", "x11", "wayland"] default = ["openvr", "openxr", "osc", "x11", "wayland"]

View File

@@ -132,21 +132,14 @@ pub(super) unsafe fn create_overlay_session(
} }
} }
pub(super) fn hmd_pose_from_views(views: &[xr::View]) -> (Affine3A, f32) { type Vec3M = mint::Vector3<f32>;
let ipd; type QuatM = mint::Quaternion<f32>;
let pos = {
let pos0: Vec3 = unsafe { std::mem::transmute(views[0].pose.position) };
let pos1: Vec3 = unsafe { std::mem::transmute(views[1].pose.position) };
ipd = (pos0.distance(pos1) * 1000.0).round() * 0.1;
(pos0 + pos1) * 0.5
};
let rot = {
let rot0: Quat = unsafe { std::mem::transmute(views[0].pose.orientation) };
let rot1: Quat = unsafe { std::mem::transmute(views[1].pose.orientation) };
rot0.lerp(rot1, 0.5)
};
(Affine3A::from_rotation_translation(rot, pos), ipd) pub(super) fn ipd_from_views(views: &[xr::View]) -> f32 {
let p0: Vec3 = Vec3M::from(views[0].pose.position).into();
let p1: Vec3 = Vec3M::from(views[1].pose.position).into();
(p0.distance(p1) * 1000.0).round() * 0.1
} }
pub(super) fn transform_to_norm_quat(transform: &Affine3A) -> Quat { pub(super) fn transform_to_norm_quat(transform: &Affine3A) -> Quat {
@@ -181,3 +174,9 @@ pub(super) fn transform_to_posef(transform: &Affine3A) -> xr::Posef {
let rotation = transform_to_norm_quat(transform); let rotation = transform_to_norm_quat(transform);
translation_rotation_to_posef(translation, rotation) translation_rotation_to_posef(translation, rotation)
} }
pub(super) fn posef_to_transform(pose: &xr::Posef) -> Affine3A {
let rotation = QuatM::from(pose.orientation).into();
let translation = Vec3M::from(pose.position).into();
Affine3A::from_rotation_translation(rotation, translation)
}

View File

@@ -15,7 +15,7 @@ use crate::{
state::{AppSession, AppState}, state::{AppSession, AppState},
}; };
use super::XrState; use super::{helpers::posef_to_transform, XrState};
type XrSession = xr::Session<xr::Vulkan>; type XrSession = xr::Session<xr::Vulkan>;
@@ -204,6 +204,22 @@ impl OpenXrInputSource {
pub fn update(&mut self, xr: &XrState, state: &mut AppState) -> anyhow::Result<()> { pub fn update(&mut self, xr: &XrState, state: &mut AppState) -> anyhow::Result<()> {
xr.session.sync_actions(&[(&self.action_set).into()])?; xr.session.sync_actions(&[(&self.action_set).into()])?;
let loc = xr.view.locate(&xr.stage, xr.predicted_display_time)?;
let hmd = posef_to_transform(&loc.pose);
if loc
.location_flags
.contains(xr::SpaceLocationFlags::ORIENTATION_VALID)
{
state.input_state.hmd.matrix3 = hmd.matrix3;
}
if loc
.location_flags
.contains(xr::SpaceLocationFlags::POSITION_VALID)
{
state.input_state.hmd.translation = hmd.translation;
}
for i in 0..2 { for i in 0..2 {
self.hands[i].update(&mut state.input_state.pointers[i], xr, &state.session)?; self.hands[i].update(&mut state.input_state.pointers[i], xr, &state.session)?;
} }

View File

@@ -49,6 +49,7 @@ struct XrState {
session: xr::Session<xr::Vulkan>, session: xr::Session<xr::Vulkan>,
predicted_display_time: xr::Time, predicted_display_time: xr::Time,
stage: Arc<xr::Space>, stage: Arc<xr::Space>,
view: Arc<xr::Space>,
stage_offset: Affine3A, stage_offset: Affine3A,
} }
@@ -123,12 +124,15 @@ pub fn openxr_run(running: Arc<AtomicBool>, show_by_default: bool) -> Result<(),
let stage = let stage =
session.create_reference_space(xr::ReferenceSpaceType::STAGE, xr::Posef::IDENTITY)?; session.create_reference_space(xr::ReferenceSpaceType::STAGE, xr::Posef::IDENTITY)?;
let view = session.create_reference_space(xr::ReferenceSpaceType::VIEW, xr::Posef::IDENTITY)?;
let mut xr_state = XrState { let mut xr_state = XrState {
instance: xr_instance, instance: xr_instance,
system, system,
session, session,
predicted_display_time: xr::Time::from_nanos(0), predicted_display_time: xr::Time::from_nanos(0),
stage: Arc::new(stage), stage: Arc::new(stage),
view: Arc::new(view),
stage_offset: Affine3A::IDENTITY, stage_offset: Affine3A::IDENTITY,
}; };
@@ -274,8 +278,7 @@ pub fn openxr_run(running: Arc<AtomicBool>, show_by_default: bool) -> Result<(),
&xr_state.stage, &xr_state.stage,
)?; )?;
let (hmd, ipd) = helpers::hmd_pose_from_views(&views); let ipd = helpers::ipd_from_views(&views);
app_state.input_state.hmd = hmd;
if (app_state.input_state.ipd - ipd).abs() > 0.01 { if (app_state.input_state.ipd - ipd).abs() > 0.01 {
log::info!("IPD changed: {} -> {}", app_state.input_state.ipd, ipd); log::info!("IPD changed: {} -> {}", app_state.input_state.ipd, ipd);
app_state.input_state.ipd = ipd; app_state.input_state.ipd = ipd;