From 49cfc215a44182c001bf7b8a3ad9371a25dc3284 Mon Sep 17 00:00:00 2001 From: numzero Date: Sun, 18 Oct 2026 00:43:53 +0300 Subject: [PATCH] make field of view fixed regardless of view angle --- src/camera.rs | 4 ++-- src/lib.rs | 17 +++++++---------- ui/src/main_window.cxx | 4 ++-- ui/src/main_window.ui | 14 +++++++------- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 17f69fe..4d3311c 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -12,8 +12,8 @@ pub(crate) fn view(yaw: f32, pitch: f32, distance: f32) -> Affine3A { Affine3A::from_mat3_translation(rotation(-yaw, -pitch), vec3(0., 0., -distance)) } -pub(crate) fn projection(fovy: f32, aspect: f32) -> Mat4 { - Mat4::perspective_rh(fovy, aspect, 1.0, 100.0) +pub(crate) fn projection(fovy: f32, aspect: f32, base_distance: f32) -> Mat4 { + Mat4::perspective_rh(fovy, aspect, 0.1 * base_distance, 10.0 * base_distance) } #[cfg(test)] diff --git a/src/lib.rs b/src/lib.rs index da85cfd..100daa0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,8 +14,8 @@ const BACKGROUND: Vec4 = vec4(0.00, 0.39, 0.52, 1.00); pub struct RedrawArgs { pub cam_yaw: f32, pub cam_pitch: f32, - pub cam_distance: f32, - pub cam_fovy: f32, + pub cam_view_y: f32, + pub cam_view_angle: f32, } impl Default for RedrawArgs { @@ -23,8 +23,8 @@ impl Default for RedrawArgs { Self { cam_yaw: 3. * std::f32::consts::FRAC_PI_4, cam_pitch: std::f32::consts::FRAC_PI_6, - cam_distance: 3., - cam_fovy: std::f32::consts::FRAC_PI_2, + cam_view_y: 80., + cam_view_angle: std::f32::consts::FRAC_PI_2, } } } @@ -109,12 +109,9 @@ impl Core { let h = size.height as f32; w / h }; - let view = Mat4::from(camera::view( - args.cam_yaw, - args.cam_pitch, - args.cam_distance, - )); - let proj = camera::projection(args.cam_fovy, aspect); + let distance = 0.5 * args.cam_view_y / (0.5 * args.cam_view_angle).tan(); + let view = Mat4::from(camera::view(args.cam_yaw, args.cam_pitch, distance)); + let proj = camera::projection(args.cam_view_angle, aspect, distance); self.mesh_pipeline.set_params( &self.queue, render::mesh::Params { diff --git a/ui/src/main_window.cxx b/ui/src/main_window.cxx index 7ec4a2f..afd61d4 100644 --- a/ui/src/main_window.cxx +++ b/ui/src/main_window.cxx @@ -15,8 +15,8 @@ void GearCutter::updateView() { RedrawArgs args{ .cam_yaw = qDegreesToRadians((float)m_ui->camYaw->value()), .cam_pitch = qDegreesToRadians((float)m_ui->camPitch->value()), - .cam_distance = (float)m_ui->camDistance->value(), - .cam_fovy = qDegreesToRadians((float)m_ui->camFoV->value()), + .cam_view_y = (float)m_ui->camFoVLin->value(), + .cam_view_angle = qDegreesToRadians((float)m_ui->camFoVAng->value()), }; m_ui->viewport->setView(args); } diff --git a/ui/src/main_window.ui b/ui/src/main_window.ui index fa127af..19d28a8 100644 --- a/ui/src/main_window.ui +++ b/ui/src/main_window.ui @@ -107,12 +107,12 @@ - Distance + Linear size - + 10.000000000000000 @@ -123,19 +123,19 @@ QAbstractSpinBox::StepType::AdaptiveDecimalStepType - 40.000000000000000 + 80.000000000000000 - Field of view + Angular size - + ° @@ -219,7 +219,7 @@ - camDistance + camFoVLin valueChanged(double) MainWindow updateView() @@ -235,7 +235,7 @@ - camFoV + camFoVAng valueChanged(double) MainWindow updateView()