Skip to content

Commit

Permalink
feat: use VR projection
Browse files Browse the repository at this point in the history
  • Loading branch information
philpax committed Oct 12, 2022
1 parent 71deb05 commit 3d06970
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 32 deletions.
53 changes: 43 additions & 10 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,55 @@ pub struct PerspectiveCamera {
}
impl PerspectiveCamera {
pub fn to_view_proj_matrices(&self) -> Vec<f32> {
let ipd = 68.3 / 1_000.0;
let ipd = 63.0 / 1_000.0;
let offset = vec4(ipd / 2.0, 0.0, 0.0, 0.0);

let view = Mat4::look_at_rh(self.eye, self.target, self.up);
let proj = Mat4::perspective_rh(self.fov_y_rad, self.aspect_ratio, self.z_near, self.z_far);

let mut view_l = view;
view_l.w_axis += view * -offset;
[-offset, offset]
.map(|o| {
let mut view = view;
view.w_axis += view * o;
(proj * view).to_cols_array()
})
.concat()
}
#[cfg(feature = "xr")]
pub fn to_view_proj_matrices_with_xr_views(&self, views: &[openxr::View]) -> Vec<f32> {
let view = Mat4::look_at_rh(self.eye, self.target, self.up);

views
.iter()
.flat_map(|v| {
let tan_left = v.fov.angle_left.tan();
let tan_right = v.fov.angle_right.tan();

let tan_down = v.fov.angle_down.tan();
let tan_up = v.fov.angle_up.tan();

let tan_width = tan_right - tan_left;
let tan_height = tan_up - tan_down;

let a11 = 2.0 / tan_width;
let a22 = 2.0 / tan_height;

let a31 = (tan_right + tan_left) / tan_width;
let a32 = (tan_up + tan_down) / tan_height;
let a33 = -self.z_far / (self.z_far - self.z_near);

let a43 = -(self.z_far * self.z_near) / (self.z_far - self.z_near);

let proj = glam::Mat4::from_cols_array(&[
a11, 0.0, 0.0, 0.0, //
0.0, a22, 0.0, 0.0, //
a31, a32, a33, -1.0, //
0.0, 0.0, a43, 0.0, //
]);

let mut view_r = view;
view_r.w_axis += view * offset;
[
(proj * view_l).to_cols_array(),
(proj * view_r).to_cols_array(),
]
.concat()
(proj * view).to_cols_array()
})
.collect()
}
pub fn resize(&mut self, inner_size: winit::dpi::PhysicalSize<u32>) {
self.aspect_ratio = inner_size.width as f32 / inner_size.height as f32;
Expand Down
53 changes: 31 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,37 +221,46 @@ fn main() -> anyhow::Result<()> {
blit_state.encode_draw_pass(&mut encoder, &view, Some(view_index));

let time_since_start = start_time.elapsed().as_secs_f32();
main_state.instances[0].1 = Quat::from_rotation_y(time_since_start / std::f32::consts::PI);
main_state.upload_instances(&wgpu_state.queue);

camera_state.data.eye.z = time_since_start.cos() - 1.0;
#[cfg(feature = "xr")]
let views = xr_state
.as_mut()
.zip(xr_frame_state)
.map(|(xr_state, xr_frame_state)| {
xr_state
.post_frame(
&wgpu_state.device,
xr_frame_state,
&mut encoder,
&blit_state,
)
.unwrap()
});

wgpu_state.queue.write_buffer(
camera_state.buffer(),
0,
bytemuck::cast_slice(&camera_state.data.to_view_proj_matrices()),
bytemuck::cast_slice(&{
#[cfg(feature = "xr")]
match &views {
Some(views) => camera_state.data.to_view_proj_matrices_with_xr_views(views),
None => camera_state.data.to_view_proj_matrices(),
}
#[cfg(not(feature = "xr"))]
camera_state.data.to_view_proj_matrices()
}),
);

main_state.instances[0].1 = Quat::from_rotation_y(time_since_start / std::f32::consts::PI);
main_state.upload_instances(&wgpu_state.queue);

#[cfg(feature = "xr")]
// todo: upload views just before submitting the queue
let views = if let Some((xr_state, xr_frame_state)) = xr_state.as_mut().zip(xr_frame_state)
{
xr_state
.post_frame(
&wgpu_state.device,
xr_frame_state,
&mut encoder,
&blit_state,
)
.unwrap()
} else {
vec![]
};

wgpu_state.queue.submit(Some(encoder.finish()));

#[cfg(feature = "xr")]
if let Some((xr_state, xr_frame_state)) = xr_state.as_mut().zip(xr_frame_state) {
xr_state.post_queue_submit(xr_frame_state, &views).unwrap();
if let (Some(xr_state), Some(xr_frame_state), Some(views)) =
(xr_state.as_mut(), xr_frame_state, &views)
{
xr_state.post_queue_submit(xr_frame_state, views).unwrap();
}

frame.present();
Expand Down

0 comments on commit 3d06970

Please sign in to comment.