Skip to main content

rayengine_core/
camera.rs

1//! Corresponding cameras with a stable visible height or vertical field of view.
2
3use crate::viewport::Viewport;
4use glam::{Mat2, Mat4, Vec2, Vec3};
5
6/// Camera expressed in world units, independent of display pixel dimensions.
7#[derive(Clone, Copy, Debug)]
8pub struct Camera2D {
9    /// World point at the center of the viewport.
10    pub target: Vec2,
11    /// Rotation in radians.
12    pub rotation: f32,
13    /// Visible world height.
14    pub view_height: f32,
15}
16
17impl Default for Camera2D {
18    fn default() -> Self {
19        Self {
20            target: Vec2::ZERO,
21            rotation: 0.0,
22            view_height: 540.0,
23        }
24    }
25}
26
27impl Camera2D {
28    /// Converts world coordinates to UI units using the current viewport.
29    pub fn world_to_ui(&self, world: Vec2, view: &Viewport) -> Vec2 {
30        view.logical_size * 0.5
31            + Mat2::from_angle(self.rotation)
32                * (world - self.target)
33                * (view.logical_size.y / self.view_height)
34    }
35
36    /// Converts a logical screen pointer to world coordinates, rejecting bars.
37    pub fn screen_to_world(&self, screen: Vec2, view: &Viewport) -> Option<Vec2> {
38        view.screen_to_ui(screen).map(|ui| {
39            self.target
40                + Mat2::from_angle(-self.rotation)
41                    * ((ui - view.logical_size * 0.5) * (self.view_height / view.logical_size.y))
42        })
43    }
44}
45
46/// Perspective 3D camera; resizing never changes its vertical FOV.
47#[derive(Clone, Copy, Debug)]
48pub struct Camera3D {
49    /// Eye position.
50    pub position: Vec3,
51    /// World point the camera looks toward.
52    pub target: Vec3,
53    /// Up direction, normally positive Y.
54    pub up: Vec3,
55    /// Vertical field of view in degrees.
56    pub vertical_fov: f32,
57}
58
59impl Default for Camera3D {
60    fn default() -> Self {
61        Self {
62            position: Vec3::new(8.0, 6.0, 8.0),
63            target: Vec3::ZERO,
64            up: Vec3::Y,
65            vertical_fov: 55.0,
66        }
67    }
68}
69
70impl Camera3D {
71    /// Right-handed world-to-view matrix.
72    pub fn view_matrix(&self) -> Mat4 {
73        Mat4::look_at_rh(self.position, self.target, self.up)
74    }
75
76    /// OpenGL perspective matrix for the viewport and explicit clipping distances.
77    pub fn projection(&self, view: &Viewport, near: f32, far: f32) -> Mat4 {
78        Mat4::perspective_rh_gl(self.vertical_fov.to_radians(), view.aspect(), near, far)
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85    use crate::viewport::ScaleMode;
86
87    #[test]
88    fn resized_rotated_camera_preserves_world_pointer_round_trip() {
89        let camera = Camera2D {
90            target: Vec2::new(120.0, -50.0),
91            rotation: 0.7,
92            view_height: 20.0,
93        };
94        let point = Vec2::new(122.0, -48.0);
95        for size in [Vec2::new(1920.0, 1080.0), Vec2::new(900.0, 1400.0)] {
96            let view = Viewport::new(size, Vec2::new(960.0, 540.0), ScaleMode::Fit).unwrap();
97            let screen = view.ui_to_screen(camera.world_to_ui(point, &view));
98            assert!(
99                camera
100                    .screen_to_world(screen, &view)
101                    .unwrap()
102                    .abs_diff_eq(point, 0.0001)
103            );
104        }
105    }
106
107    #[test]
108    fn fitted_3d_projection_is_identical_across_aspects_and_dpi() {
109        let a = Viewport::new(
110            Vec2::new(960.0, 540.0),
111            Vec2::new(960.0, 540.0),
112            ScaleMode::Fit,
113        )
114        .unwrap();
115        let b = Viewport::new(
116            Vec2::new(800.0, 1200.0),
117            Vec2::new(960.0, 540.0),
118            ScaleMode::Fit,
119        )
120        .unwrap();
121        let camera = Camera3D::default();
122        assert!(
123            camera
124                .projection(&a, 0.1, 1000.0)
125                .abs_diff_eq(camera.projection(&b, 0.1, 1000.0), 0.00001)
126        );
127    }
128}