Skip to main content

trackball/
scope.rs

1use crate::Fixed;
2use glam::{Mat4, Vec2, camera::rh::proj::opengl};
3
4/// Scope defining enclosing viewing frustum.
5///
6/// Implements [`Default`] and can be created with `Scope::default()`.
7#[derive(Debug, Clone, Copy, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[cfg_attr(
10    feature = "rkyv-08",
11    derive(rkyv_08::Archive, rkyv_08::Serialize, rkyv_08::Deserialize),
12    rkyv(crate = rkyv_08)
13)]
14pub struct Scope {
15    /// Fixed quantity wrt field of view.
16    ///
17    /// Default is fixed vertical field of view of π/4.
18    fov: Fixed,
19    /// Clip plane distances.
20    ///
21    /// Near and far clip plane distances from either target or eye whether [`Self::oim`]. Default
22    /// is `(1e-1, 1e+3)` measured from eye.
23    zcp: (f32, f32),
24    /// Object inspection mode.
25    ///
26    /// Scales clip plane distances by measuring from target instead of eye. Default is `false`.
27    oim: bool,
28    /// Orthographic projection mode.
29    ///
30    /// Computes scale-identical orthographic instead of perspective projection. Default is `false`.
31    opm: bool,
32}
33
34impl Default for Scope {
35    fn default() -> Self {
36        Self {
37            fov: Fixed::default(),
38            zcp: (1e-1, 1e+3),
39            oim: false,
40            opm: false,
41        }
42    }
43}
44
45impl Scope {
46    /// Fixed quantity wrt field of view, see [`Self::set_fov()`].
47    #[must_use]
48    pub const fn fov(&self) -> Fixed {
49        self.fov
50    }
51    /// Sets fixed quantity wrt field of view.
52    ///
53    /// Default is fixed vertical field of view of π/4.
54    ///
55    /// ```
56    /// use trackball::{Scope, glam::Vec2};
57    ///
58    /// // Current screen size.
59    /// let max = Vec2::new(800.0, 600.0);
60    /// // Default scope with fixed vertical field of view of π/4:
61    /// //
62    /// //   * Increasing width increases horizontal field of view (more can be seen).
63    /// //   * Increasing height scales scope zooming in as vertical field of view is fixed.
64    /// let mut scope = Scope::default();
65    /// // Unfix vertical field of view by fixing current unit per pixel on focus plane at distance
66    /// // from eye of one, that is effectively `upp` divided by `zat` to make it scale-independant:
67    /// //
68    /// //   * Increasing width increases horizontal field of view (more can be seen).
69    /// //   * Increasing height increases vertical field of view (more can be seen).
70    /// scope.set_fov(scope.fov().to_upp(max));
71    /// ```
72    pub fn set_fov(&mut self, fov: impl Into<Fixed>) {
73        self.fov = fov.into();
74    }
75    /// Clip plane distances from eye regardless of [`Self::inspect()`] wrt to distance between eye
76    /// and target.
77    ///
78    /// Default is `(1e-1, 1e+3)` measured from eye.
79    #[must_use]
80    pub fn clip_planes(&self, zat: f32) -> (f32, f32) {
81        if self.oim {
82            let (znear, zfar) = self.zcp;
83            (zat - znear, zat + zfar)
84        } else {
85            self.zcp
86        }
87    }
88    /// Sets clip plane distances from target or eye whether [`Self::inspect()`].
89    ///
90    /// Default is `(1e-1, 1e+3)` measured from eye.
91    pub const fn set_clip_planes(&mut self, znear: f32, zfar: f32) {
92        self.zcp = (znear, zfar);
93    }
94    /// Object inspection mode.
95    ///
96    /// Scales clip plane distances by measuring from target instead of eye. Default is `false`.
97    #[must_use]
98    pub const fn inspect(&self) -> bool {
99        self.oim
100    }
101    /// Sets object inspection mode.
102    ///
103    /// Scales clip plane distances by measuring from target instead of eye. Default is `false`.
104    pub const fn set_inspect(&mut self, oim: bool) {
105        self.oim = oim;
106    }
107    /// Orthographic projection mode.
108    ///
109    /// Computes scale-identical orthographic instead of perspective projection. Default is `false`.
110    #[must_use]
111    pub const fn ortho(&self) -> bool {
112        self.opm
113    }
114    /// Sets orthographic projection mode.
115    ///
116    /// Computes scale-identical orthographic instead of perspective projection. Default is `false`.
117    pub const fn set_ortho(&mut self, opm: bool) {
118        self.opm = opm;
119    }
120    /// Projection transformation and unit per pixel on focus plane wrt distance between eye and
121    /// target and maximum position in screen space.
122    #[must_use]
123    pub fn projection_and_upp(&self, zat: f32, max: Vec2) -> (Mat4, f32) {
124        let (znear, zfar) = self.clip_planes(zat);
125        if self.opm {
126            let (max, upp) = self.fov.max_and_upp(zat, max);
127            let mat = opengl::orthographic(-max.x, max.x, -max.y, max.y, znear, zfar);
128            (mat, upp)
129        } else {
130            let fov = self.fov.to_ver(max).into_inner();
131            let (max, upp) = self.fov.max_and_upp(zat, max);
132            let mat = opengl::perspective(fov, max.x / max.y, znear, zfar);
133            (mat, upp)
134        }
135    }
136}