working version of the frustum based calculation for the perspective matrix
This commit is contained in:
parent
c793c81ee8
commit
2b3d00f1d4
5 changed files with 113 additions and 17 deletions
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
use crate::scene::*;
|
use crate::scene::*;
|
||||||
use crate::utils::*;
|
use crate::utils::*;
|
||||||
|
use crate::viewer::*;
|
||||||
|
|
||||||
use bevy::{prelude::*, window::PresentMode, render::camera::CameraProjectionPlugin};
|
use bevy::{prelude::*, window::PresentMode, render::camera::CameraProjectionPlugin};
|
||||||
use offaxis::{offaxis_camera_setup, OffAxisProjection};
|
use offaxis::{offaxis_camera_setup, OffAxisProjection};
|
||||||
|
@ -15,6 +16,7 @@ mod offaxis;
|
||||||
mod scene;
|
mod scene;
|
||||||
mod screeninfo;
|
mod screeninfo;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
mod viewer;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -38,6 +40,7 @@ fn main() {
|
||||||
.add_startup_system(offaxis_camera_setup)
|
.add_startup_system(offaxis_camera_setup)
|
||||||
.add_plugin(CameraProjectionPlugin::<OffAxisProjection>::default())
|
.add_plugin(CameraProjectionPlugin::<OffAxisProjection>::default())
|
||||||
.add_system(update_offaxis)
|
.add_system(update_offaxis)
|
||||||
|
.add_system(simulate_viewer)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,10 @@ use bevy::prelude::*;
|
||||||
use bevy::render::camera::{Camera, CameraProjection};
|
use bevy::render::camera::{Camera, CameraProjection};
|
||||||
use bevy::render::primitives::Frustum;
|
use bevy::render::primitives::Frustum;
|
||||||
use bevy::render::view::VisibleEntities;
|
use bevy::render::view::VisibleEntities;
|
||||||
|
use bevy::math::Mat4;
|
||||||
|
|
||||||
use crate::screeninfo::ScreenInfo;
|
use crate::screeninfo::ScreenInfo;
|
||||||
|
use crate::viewer::*;
|
||||||
|
|
||||||
#[derive(Component, Debug, Clone, Reflect)]
|
#[derive(Component, Debug, Clone, Reflect)]
|
||||||
#[reflect(Component, Default)]
|
#[reflect(Component, Default)]
|
||||||
|
@ -19,6 +21,8 @@ impl CameraProjection for OffAxisProjection {
|
||||||
|
|
||||||
println!("Here we go! {:?}",self);
|
println!("Here we go! {:?}",self);
|
||||||
|
|
||||||
|
//Mat4::perspective_rh_gl(fov_y_radians, aspect_ratio, z_near, z_far)
|
||||||
|
|
||||||
Mat4::orthographic_rh(-self.aspect, self.aspect, -1.0, 1.0, self.near, self.far)
|
Mat4::orthographic_rh(-self.aspect, self.aspect, -1.0, 1.0, self.near, self.far)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,6 +46,23 @@ impl Default for OffAxisProjection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 offaxis_camera_setup(mut commands: Commands) {
|
pub fn offaxis_camera_setup(mut commands: Commands) {
|
||||||
|
|
||||||
let projection = OffAxisProjection::default();
|
let projection = OffAxisProjection::default();
|
||||||
|
@ -68,6 +89,48 @@ pub fn offaxis_camera_setup(mut commands: Commands) {
|
||||||
VisibleEntities::default(),
|
VisibleEntities::default(),
|
||||||
Camera::default(),
|
Camera::default(),
|
||||||
Camera3d::default(),
|
Camera3d::default(),
|
||||||
ScreenInfo::new("Test")
|
ScreenInfo::new("Test"),
|
||||||
|
Viewer::new(transform.translation)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ use bevy::{prelude::*, math::bool, render::primitives::Frustum};
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use bevy::render::primitives::Plane;
|
use bevy::render::primitives::Plane;
|
||||||
|
|
||||||
|
|
||||||
// pub struct BuildScenePlugin;
|
// pub struct BuildScenePlugin;
|
||||||
|
|
||||||
// impl Plugin for BuildScenePlugin {
|
// impl Plugin for BuildScenePlugin {
|
||||||
|
@ -69,6 +68,7 @@ fn generalized_projection (
|
||||||
_ = 33;
|
_ = 33;
|
||||||
// camera.frustum.planes[4]. // should be near
|
// camera.frustum.planes[4]. // should be near
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_scene(
|
pub fn build_scene(
|
||||||
|
|
21
src/utils.rs
21
src/utils.rs
|
@ -2,6 +2,8 @@ use crate::scene::*;
|
||||||
use crate::offaxis::*;
|
use crate::offaxis::*;
|
||||||
use crate::screeninfo;
|
use crate::screeninfo;
|
||||||
use crate::screeninfo::ScreenInfo;
|
use crate::screeninfo::ScreenInfo;
|
||||||
|
use crate::viewer::Viewer;
|
||||||
|
|
||||||
|
|
||||||
use bevy::{prelude::*, window::PresentMode, render::camera::CameraProjectionPlugin};
|
use bevy::{prelude::*, window::PresentMode, render::camera::CameraProjectionPlugin};
|
||||||
|
|
||||||
|
@ -19,16 +21,25 @@ pub fn cycle_msaa(input: Res<Input<KeyCode>>, mut msaa: ResMut<Msaa>) {
|
||||||
|
|
||||||
pub fn update_offaxis(mut query : Query<(
|
pub fn update_offaxis(mut query : Query<(
|
||||||
&mut OffAxisProjection,
|
&mut OffAxisProjection,
|
||||||
&ScreenInfo
|
&mut Transform,
|
||||||
|
&ScreenInfo,
|
||||||
|
&Viewer
|
||||||
)>
|
)>
|
||||||
) {
|
) {
|
||||||
for (mut q,si) in query.iter_mut() {
|
for (mut projection,mut transform,screen_info,viewer) in query.iter_mut() {
|
||||||
|
|
||||||
// we fake access to far for updating the matrix
|
// we fake access to far for updating the matrix
|
||||||
(*q).far *= 1.0;
|
(*projection).far *= 1.0;
|
||||||
|
|
||||||
|
println!("Projection {:?}",*projection);
|
||||||
|
|
||||||
|
println!("Screen Info {:?}",screen_info.name);
|
||||||
|
|
||||||
|
println!("Viewer {:?}",viewer.position);
|
||||||
|
|
||||||
|
*transform = Transform::from_translation(viewer.position).with_rotation(transform.rotation);
|
||||||
|
|
||||||
|
|
||||||
println!("Update {:?}",q);
|
|
||||||
|
|
||||||
println!("Screeninfo {}",si.name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,22 +1,41 @@
|
||||||
use bevy::{prelude::*, window::PresentMode, render::camera::CameraProjectionPlugin};
|
use bevy::{prelude::*, window::PresentMode, render::camera::CameraProjectionPlugin};
|
||||||
use offaxis::{offaxis_camera_setup, OffAxisProjection};
|
// use offaxis::{offaxis_camera_setup, OffAxisProjection};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component,Default)]
|
||||||
struct Viewer;
|
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct Viewer {
|
pub struct Viewer {
|
||||||
position: Vec3,
|
pub position: Vec3,
|
||||||
orientation: Quat,
|
pub orientation: Quat,
|
||||||
}
|
|
||||||
|
|
||||||
|
pub alpha: f32,
|
||||||
|
}
|
||||||
|
|
||||||
impl Viewer {
|
impl Viewer {
|
||||||
|
|
||||||
fn default() -> Self {
|
pub fn new(position: Vec3) ->Self {
|
||||||
|
Self {
|
||||||
|
position,
|
||||||
|
orientation: Quat::IDENTITY,
|
||||||
|
alpha: 0.0,
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
position: Vec3::ZERO,
|
position: Vec3::ZERO,
|
||||||
|
orientation: Quat::IDENTITY,
|
||||||
|
alpha: 0.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn simulate_viewer(mut query: Query<&mut Viewer>)
|
||||||
|
{
|
||||||
|
for mut v in query.iter_mut() {
|
||||||
|
//v.position += Vec3::Y * 0.005;
|
||||||
|
v.alpha += 0.01;
|
||||||
|
let radius = 0.5;
|
||||||
|
v.position = Vec3::new(v.alpha.sin()*radius,v.alpha.cos()*radius,5.0);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue