three_d/renderer/
viewer.rs

1mod tone_mapping;
2pub use tone_mapping::*;
3
4mod color_space;
5pub use color_space::*;
6
7mod camera;
8pub use camera::*;
9
10use crate::*;
11
12pub use three_d_asset::Frustum;
13
14macro_rules! impl_viewer_body {
15    ($inner:ident) => {
16        fn position(&self) -> Vec3 {
17            self.$inner().position()
18        }
19
20        fn view(&self) -> Mat4 {
21            self.$inner().view()
22        }
23
24        fn projection(&self) -> Mat4 {
25            self.$inner().projection()
26        }
27
28        fn viewport(&self) -> Viewport {
29            self.$inner().viewport()
30        }
31
32        fn z_near(&self) -> f32 {
33            self.$inner().z_near()
34        }
35
36        fn z_far(&self) -> f32 {
37            self.$inner().z_far()
38        }
39
40        fn color_mapping(&self) -> ColorMapping {
41            self.$inner().color_mapping()
42        }
43
44        fn tone_mapping(&self) -> ToneMapping {
45            self.$inner().tone_mapping()
46        }
47    };
48}
49
50///
51/// Represents a viewer, usually some kind of camera.
52/// The default implementation of this trait is the [Camera] which should be adequate for most use cases.
53///
54pub trait Viewer {
55    /// The position of the viewer.
56    fn position(&self) -> Vec3;
57
58    /// The view matrix which transforms from world space to view space.
59    fn view(&self) -> Mat4;
60
61    /// The projection matrix which transforms from view space to clip space (2D position on the screen).
62    fn projection(&self) -> Mat4;
63
64    /// The 2D [Viewport] of the viewer.
65    fn viewport(&self) -> Viewport;
66
67    /// Defines the minimum depth in world space.
68    fn z_near(&self) -> f32;
69
70    /// Defines the maximum depth in world space.
71    fn z_far(&self) -> f32;
72
73    /// Defines the [ColorMapping] applied to the final rendered image.
74    fn color_mapping(&self) -> ColorMapping;
75
76    /// Defines the [ToneMapping] applied to the final rendered image.
77    fn tone_mapping(&self) -> ToneMapping;
78}
79
80use std::ops::Deref;
81impl<T: Viewer + ?Sized> Viewer for &T {
82    impl_viewer_body!(deref);
83}
84
85impl<T: Viewer + ?Sized> Viewer for &mut T {
86    impl_viewer_body!(deref);
87}
88
89impl<T: Viewer> Viewer for Box<T> {
90    impl_viewer_body!(as_ref);
91}
92
93impl<T: Viewer> Viewer for std::rc::Rc<T> {
94    impl_viewer_body!(as_ref);
95}
96
97impl<T: Viewer> Viewer for std::sync::Arc<T> {
98    impl_viewer_body!(as_ref);
99}
100
101impl<T: Viewer> Viewer for std::cell::RefCell<T> {
102    impl_viewer_body!(borrow);
103}
104
105impl<T: Viewer> Viewer for std::sync::RwLock<T> {
106    fn position(&self) -> Vec3 {
107        self.read().unwrap().position()
108    }
109
110    fn view(&self) -> Mat4 {
111        self.read().unwrap().view()
112    }
113
114    fn projection(&self) -> Mat4 {
115        self.read().unwrap().projection()
116    }
117
118    fn viewport(&self) -> Viewport {
119        self.read().unwrap().viewport()
120    }
121
122    fn z_near(&self) -> f32 {
123        self.read().unwrap().z_near()
124    }
125
126    fn z_far(&self) -> f32 {
127        self.read().unwrap().z_far()
128    }
129
130    fn color_mapping(&self) -> ColorMapping {
131        self.read().unwrap().color_mapping()
132    }
133
134    fn tone_mapping(&self) -> ToneMapping {
135        self.read().unwrap().tone_mapping()
136    }
137}