@@ -42,7 +42,7 @@ use openxr::{self as xr, sys};
4242use parking_lot:: RwLock ;
4343use presentation:: GraphicsContextHandles ;
4444use serde:: { Deserialize , Serialize } ;
45- use xr:: { Vector3f , View } ;
45+ use xr:: { Quaternionf , Vector3f , View } ;
4646
4747use std:: { error:: Error , ops:: Deref , sync:: Arc , thread, time:: Duration } ;
4848use wgpu:: { TextureUsages , TextureViewDescriptor } ;
@@ -776,40 +776,46 @@ impl Vec3Conv for Vector3f {
776776 }
777777}
778778
779+ trait QuatConv {
780+ fn to_quat ( & self ) -> Quat ;
781+ }
782+
783+ impl QuatConv for Quaternionf {
784+ fn to_quat ( & self ) -> Quat {
785+ Quat :: from_xyzw ( self . x , self . y , self . z , self . w )
786+ }
787+ }
788+
779789pub fn update_xrcamera_view (
780- mut cam : Query < ( & mut XRProjection , & mut Transform , & mut Frustum , & Eye ) > ,
790+ mut cam : Query < ( & mut XRProjection , & mut Transform , & Eye ) > ,
781791 mut xr_cam : Query < ( & mut Transform , & XrCameras ) , Without < Eye > > ,
782792 views : Res < Vec < View > > ,
783793) {
784- //TODO: do this for midpoint of rotation as well using Quat::slerp
785794 let midpoint = ( views. get ( 0 ) . unwrap ( ) . pose . position . to_vec3 ( )
786795 + views. get ( 1 ) . unwrap ( ) . pose . position . to_vec3 ( ) )
787796 / 2. ;
788797 xr_cam. single_mut ( ) . 0 . translation = midpoint;
789798
790- for ( mut projection, mut transform, mut frustum, eye) in cam. iter_mut ( ) {
799+ let left_rot = views. get ( 0 ) . unwrap ( ) . pose . orientation . to_quat ( ) ;
800+ let right_rot = views. get ( 1 ) . unwrap ( ) . pose . orientation . to_quat ( ) ;
801+ let mid_rot = if left_rot. dot ( right_rot) >= 0. {
802+ left_rot. slerp ( right_rot, 0.5 )
803+ } else {
804+ right_rot. slerp ( left_rot, 0.5 )
805+ } ;
806+ let mid_rot_inverse = mid_rot. inverse ( ) ;
807+ xr_cam. single_mut ( ) . 0 . rotation = mid_rot;
808+
809+ for ( mut projection, mut transform, eye) in cam. iter_mut ( ) {
791810 let view_idx = match eye {
792811 Eye :: Left => 0 ,
793812 Eye :: Right => 1 ,
794813 } ;
795814 let view = views. get ( view_idx) . unwrap ( ) ;
796815
797- // let x_fov = view.fov.angle_left.abs() + view.fov.angle_right.abs();
798- // let y_fov = view.fov.angle_up.abs() + view.fov.angle_down.abs();
799- // // y radians
800- // perspective.fov = y_fov;
801- // // width / height
802- // perspective.aspect_ratio = x_fov / y_fov;
803816 projection. fov = view. fov ;
804- // *frustum = Frustum::from_view_projection(
805- // &projection.get_projection_matrix(),
806- // &Vec3::ZERO,
807- // &Vec3::Z,
808- // projection.far(),
809- // );
810-
811- let rot = view. pose . orientation ;
812- transform. rotation = Quat :: from_xyzw ( rot. x , rot. y , rot. z , rot. w ) ;
817+
818+ transform. rotation = mid_rot_inverse * view. pose . orientation . to_quat ( ) ;
813819 let pos = view. pose . position ;
814820 transform. translation = pos. to_vec3 ( ) - midpoint;
815821 }
0 commit comments