Skip to content

Commit

Permalink
make zooming work in ortho mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mkovaxx committed Nov 26, 2023
1 parent 9f0ccbf commit 052bc51
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions crates/viewer/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl OrbitCamera {

/// Zoom in or out, while looking at the same target.
pub fn zoom(&mut self, zoom_delta: f32) {
self.radius = f32::max(self.radius * f32::exp(zoom_delta), f32::EPSILON);
self.zoom_factor = f32::max(self.zoom_factor * f32::exp(zoom_delta), f32::EPSILON);
}

/// Orbit around the target while keeping the distance.
Expand All @@ -71,23 +71,33 @@ impl OrbitCamera {
// These magic numbers are configured so that the particular model we are loading is
// visible in its entirety. They will be dynamically computed eventually when we have "fit
// to view" function or alike.
let proj = match self.projection {
Projection::Orthographic => Mat4::orthographic_rh(
-50.0 * self.zoom_factor * self.aspect_ratio,
50.0 * self.zoom_factor * self.aspect_ratio,
-50.0 * self.zoom_factor,
50.0 * self.zoom_factor,
-1000.0,
1000.0,
),

let (proj, effective_radius) = match self.projection {
Projection::Orthographic => {
let proj = Mat4::orthographic_rh(
-50.0 * self.zoom_factor * self.aspect_ratio,
50.0 * self.zoom_factor * self.aspect_ratio,
-50.0 * self.zoom_factor,
50.0 * self.zoom_factor,
-1000.0,
1000.0,
);
(proj, self.radius)
},
Projection::Perspective => {
Mat4::perspective_rh(std::f32::consts::PI / 2.0, self.aspect_ratio, 0.01, 1000.0)
let proj = Mat4::perspective_rh(
std::f32::consts::PI / 2.0,
self.aspect_ratio,
0.01,
1000.0,
);
(proj, self.zoom_factor * self.radius)
},
};

let local_frame = self.get_local_frame();
let position = self.target + self.radius * local_frame.z_axis;
let upward = local_frame.y_axis;
let position = self.target + effective_radius * local_frame.z_axis;
let view = Mat4::look_at_rh(position, self.target, upward);

proj * view
Expand Down

0 comments on commit 052bc51

Please sign in to comment.