Make camera result optional

This commit is contained in:
Maximilian Ammann 2022-01-21 15:46:53 +01:00
parent 0fab6b5114
commit 4c471f8be8
2 changed files with 16 additions and 12 deletions

View File

@ -1,7 +1,7 @@
use super::UpdateState;
use crate::render::camera::Camera;
use crate::render::render_state::RenderState;
use cgmath::{EuclideanSpace, Point3, Vector2, Vector3};
use cgmath::{EuclideanSpace, Point3, Vector2, Vector3, Zero};
use std::time::Duration;
use winit::event::{ElementState, MouseButton};
@ -26,9 +26,14 @@ impl UpdateState for PanHandler {
let perspective = &state.perspective;
let view_proj = reference_camera.calc_view_proj(perspective);
let start = reference_camera.window_to_world_z0(&start_window_position, &view_proj);
let current = reference_camera.window_to_world_z0(&window_position, &view_proj);
let delta = start - current;
let delta = if let (Some(start), Some(current)) = (
reference_camera.window_to_world_z0(&start_window_position, &view_proj),
reference_camera.window_to_world_z0(&window_position, &view_proj),
) {
start - current
} else {
Vector3::zero()
};
if self.start_camera_position.is_none() {
self.start_camera_position = Some(state.camera.position.to_vec());

View File

@ -192,17 +192,18 @@ impl Camera {
&self,
window: &Vector2<f64>,
view_proj: &Matrix4<f64>,
) -> Vector3<f64> {
) -> Option<Vector3<f64>> {
let near_world = self.window_to_world(&Vector3::new(window.x, window.y, 0.0), view_proj);
let far_world = self.window_to_world(&Vector3::new(window.x, window.y, 1.0), view_proj);
// for z = 0 in world coordinates
let u = -near_world.z / (far_world.z - near_world.z);
if !(0.0..=1.0).contains(&u) {
panic!("interpolation factor is out of bounds")
if (0.0..=1.0).contains(&u) {
Some(near_world + u * (far_world - near_world))
} else {
None
}
near_world + u * (far_world - near_world)
}
pub fn view_bounding_box2(&self, perspective: &Perspective) -> Option<Aabb2<f64>> {
@ -215,7 +216,7 @@ impl Camera {
Vector2::new(0.0, self.height),
]
.iter()
.map(|point| self.window_to_world_z0(point, &view_proj))
.filter_map(|point| self.window_to_world_z0(point, &view_proj))
.collect::<Vec<_>>();
let min_x = vec
@ -255,9 +256,7 @@ impl Camera {
Point3::from_vec(a_ndc.mul_element_wise(to_ndc)),
Point3::from_vec(b_ndc.mul_element_wise(to_ndc)),
Point3::from_vec(c_ndc.mul_element_wise(to_ndc)),
)
.unwrap();
println!("{:?}", &plane);
)?;
let points = plane.intersection_points_aabb3(&Aabb3::new(
Point3::new(0.0, 0.0, 0.0),