refactor projections into their own module

This commit is contained in:
Hartmut Seichter 2022-12-09 21:30:47 +01:00
parent 4e59d730fc
commit a7e92ba70a
6 changed files with 124 additions and 174 deletions

View file

@ -7,6 +7,7 @@ use bevy::math::Mat4;
use crate::screeninfo::ScreenInfo;
use crate::viewer::*;
use crate::projection::*;
#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component, Default)]
@ -16,66 +17,10 @@ pub struct OffAxisProjection {
aspect: f32,
}
pub fn make_projection_rh_from_frustum(left: f32, right: f32, bottom: f32, top: f32, near:f32, far:f32) -> Mat4
{
// based on OpenSceneGraph / glFrustum implementation
let a = (right + left) / (right - left);
let b: f32 = (top + bottom) / (top - bottom);
let c= if far.abs() > f32::MAX { -1.0 } else { -(far + near) / (far - near)};
let d = if far.abs() > f32::MAX { -2.0 * near } else { -2.0 * far * near / (far - near) };
Mat4::from_cols(
Vec4::new(2.0 * near/(right-left),0.0,0.0,0.0),
Vec4::new(0.0,2.0*near/(top-bottom), 0.0,0.0),
Vec4::new(a, b, c, -1.0),
Vec4::new(0.0, 0.0, d, 0.0))
}
pub fn make_projection_rh_from_frustum_reversed(left: f32, right: f32, bottom: f32, top: f32, near:f32, far:f32) -> Mat4
{
assert!(near > 0.0);
//
// reversed z 0..1 projection based on https://thxforthefish.com/posts/reverse_z/
//
let a = (right + left) / (right - left);
let b: f32 = (top + bottom) / (top - bottom);
// let c= if far.abs() > f32::MAX { -1.0 } else { -(far + near) / (far - near)};
// let d = if far.abs() > f32::MAX { -2.0 * near } else { -2.0 * far * near / (far - near) };
let c = near / (far - near);
let d = far * near / (far - near);
Mat4::from_cols(
Vec4::new(2.0 * near/(right-left),0.0,0.0,0.0),
Vec4::new(0.0,2.0*near/(top-bottom), 0.0,0.0),
Vec4::new(a, b, c, -1.0),
Vec4::new(0.0, 0.0, d, 0.0))
}
pub fn make_projection_rh_custom(fov_y: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Mat4
{
let tan_fovy = (fov_y * 0.5).tan(); // use half angle beta
let right = tan_fovy * aspect_ratio * z_near;
let left = -right;
let top = tan_fovy * z_near;
let bottom = -top;
//make_projection_rh_from_frustum(left, right, bottom, top, z_near, z_far)
make_projection_rh_from_frustum_reversed(left, right, bottom, top, z_near, z_far)
}
impl CameraProjection for OffAxisProjection {
fn get_projection_matrix(&self) -> Mat4 {
make_projection_rh_custom(45.0_f32.to_radians(), self.aspect, self.near, self.far)
// Mat4::perspective_rh(45.0_f32.to_radians(),
@ -101,7 +46,7 @@ impl Default for OffAxisProjection {
Self {
near: 0.1,
far: 1000.0,
aspect: 1.0,
aspect: 1.5,
}
}
}
@ -141,42 +86,3 @@ pub fn offaxis_camera_setup(mut commands: Commands) {
}
#[cfg(test)]
mod tests {
use bevy::prelude::Mat4;
use super::make_projection_rh_from_frustum;
#[test]
fn compare_projections() {
// build an on-axis frustum
let fovy = 33.0_f32;
let aspect_ratio = 1.6666_f32;
let z_near = 1.0_f32;
let z_far = 500.0_f32;
let tan_fovy = (fovy * 0.5).to_radians().tan(); // use half angle beta
let right = tan_fovy * aspect_ratio * z_near;
let left = -right;
let top = tan_fovy * z_near;
let bottom = -top;
let mat_frust = make_projection_rh_from_frustum(left, right, bottom, top, z_near, z_far);
println!("mat 1 {:?}",mat_frust);
let mat_pers = Mat4::perspective_rh_gl(fovy.to_radians(),aspect_ratio,z_near,z_far);
println!("mat 2 {:?}",mat_pers);
assert!(mat_frust.abs_diff_eq(mat_pers, f32::EPSILON));
}
}