update to utilize a proper off-axis projection including a demo scene to prototype all systems

This commit is contained in:
Hartmut Seichter 2022-12-11 21:12:08 +01:00
parent 040bc8d88a
commit 9c778afa2b
7 changed files with 130 additions and 81 deletions

View file

@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy::math::Mat4;
use bevy::math::{Mat4, Quat, Affine3A};
/// creates a conventional projection matrix from frustum planes
///
@ -86,15 +86,24 @@ pub fn create_offaxis_matrices(
// println!("Dist {}",dist);
let z_near = 0.001_f32.max(dist - 0.01);
// small offset for the near plane
let min_near_distance_offset = 0.01f32;
let left = vec_right_normalized.dot(frustum_left) * z_near / dist;
let right = vec_right_normalized.dot(frustum_right) * z_near / dist;
let bottom = vec_up_normalized.dot(frustum_left) * z_near / dist;
let top = vec_up_normalized.dot(frustum_up) * z_near / dist;
// set a minimal near distance
let min_near_distance = 0.00001f32;
// println!("l r b t {} {} {} {}",left,right,bottom,top);
// calculate a reasonable near distance
let z_near = min_near_distance.max(dist - min_near_distance_offset);
// distances
let left = vec_right_normalized.dot(frustum_left) * z_near / dist; // left screen edge
let right = vec_right_normalized.dot(frustum_right) * z_near / dist; // right screen edge
let bottom = vec_up_normalized.dot(frustum_left) * z_near / dist; // bottom screen edge
let top = vec_up_normalized.dot(frustum_up) * z_near / dist; // distance eye from screen
info!("l r b t {} {} {} {}",left,right,bottom,top);
// create a view frustum
let projection_matrix = make_projection_rh_from_frustum_reversed(left,right,bottom,top,z_near,z_far);
let view_matrix_rotation = Mat4::from_cols(
@ -104,6 +113,11 @@ pub fn create_offaxis_matrices(
Vec4::W
);
let rotation_quat = Quat::from_mat4(&view_matrix_rotation); /// Quat::from_mat4(view_matrix_rotation);
info!("Rotation Mat {:?}",view_matrix_rotation);
info!("Viewer Rotation {:?}",rotation_quat);
let view_matrix_eye = Mat4::from_cols(
Vec4::X,
Vec4::Y,
@ -111,8 +125,10 @@ pub fn create_offaxis_matrices(
(-pos_eye).extend(1.0)
);
// create resulting view matrix (this should be much simpler using glam API)
let view_matrix = view_matrix_rotation * view_matrix_eye;
// return tuple of view and projection
(view_matrix,projection_matrix)
}