gl_utils/camera2d.rs
1//! Types and functions for 2D rendering.
2//!
3//! The `Camera2d` type represents a view positioned and oriented in 2D *world
4//! space* defined as a left-hand^* coordinate system with a default scale of 1
5//! unit == 1 pixel, with the origin at the center of the screen.
6//!
7//! ^*: "left-hand" in the sense of using the left hand with palm facing down
8//! (away) and taking the thumb to be the first coordinate and index finger to
9//! be the second coordinate
10//!
11//! The camera position and orientation defines the view space transform from
12//! world coordinates to camera coordinates. This is represented as a transform
13//! where the viewpoint is the x,y position of the camera and the z coordinate
14//! is set to 0.0, looking down the negative z axis, and the "up" vector as the
15//! y vector of the 2D camera orientation with the z component set to 0.0.
16//!
17//! The viewport dimensions and zoom factor defines the view space to clip space
18//! orthographic projection. Because world space is defined with the origin in
19//! the center of the screen, the frustum planes are +/- half screen widths,
20//! scaled by the zoom factor.
21//!
22//! TODO: clarify coordinates when rendering into viewports that don't cover the
23//! entire screen
24//!
25//! Two functions are provided to map between screen space and world space:
26//! `Camera2d::screen_to_world` and `Camera2d::world_to_screen`.
27//!
28//! For information on tile-based rendering and tile coordinates, see the
29//! [tile](crate::tile) module.
30
31use math_utils as math;
32use math_utils::{num, vek};
33use crate::graphics;
34
35/// Represents a camera ("view") positioned and oriented in a 2D scene with a
36/// 2D transformation and a 2D projection
37#[derive(Clone, Debug, PartialEq)]
38pub struct Camera2d {
39 // transform
40 /// Position in 2D world space
41 position : math::Point2 <f32>,
42 /// Yaw represents a counter-clockwise rotation restricted to the range
43 /// $[-\pi, \pi]$.
44 yaw : math::Rad <f32>,
45 /// Basis derived from `yaw`.
46 orientation : math::Rotation2 <f32>,
47 /// Transforms points from 2D world space to 2D camera (view, eye) space as
48 /// specified by the camera position and orientation.
49 transform_mat_world_to_view : math::Matrix4 <f32>,
50
51 // projection
52 viewport_width : u16,
53 viewport_height : u16,
54 /// Determines the extent of the view represented in the `ortho` structure
55 zoom : f32,
56 /// Used to create the ortho projection matrix
57 ortho : vek::FrustumPlanes <f32>,
58 /// Constructed from the parameters in `ortho` to transform points in 2D view
59 /// space to 4D homogenous clip coordinates.
60 projection_mat_ortho : math::Matrix4 <f32>
61}
62
63impl Camera2d {
64 /// Create a new camera centered at the origin looking down the positive Y
65 /// axis with 'up' vector aligned with the Z axis.
66 ///
67 /// The orthographic projection is defined such that at the initial zoom
68 /// level of 1.0, one unit in world space is one *pixel*, so the points in
69 /// the bounding rectangle defined by minimum point `(-half_screen_width+1,
70 /// -half_screen_height+1)` and maximum point `(half_screen_width,
71 /// half_screen_height)` are visible.
72 pub fn new (viewport_width : u16, viewport_height : u16) -> Self {
73 use math::Point;
74 use num::One;
75 // transform: world space -> view space
76 let position = math::Point2::origin();
77 let yaw = math::Rad (0.0);
78 let orientation = math::Rotation2::one();
79 let transform_mat_world_to_view =
80 transform_mat_world_to_view (position, orientation);
81
82 // projection: view space -> clip space
83 let zoom = 1.0;
84 let ortho = Self::ortho_from_viewport_zoom (
85 viewport_width, viewport_height, zoom);
86 let projection_mat_ortho = graphics::projection_mat_orthographic (&ortho);
87
88 Camera2d {
89 position,
90 yaw,
91 orientation,
92 transform_mat_world_to_view,
93 viewport_width,
94 viewport_height,
95 zoom,
96 ortho,
97 projection_mat_ortho
98 }
99 }
100
101 pub const fn yaw (&self) -> math::Rad <f32> {
102 self.yaw
103 }
104 pub const fn position (&self) -> math::Point2 <f32> {
105 self.position
106 }
107 pub const fn orientation (&self) -> math::Rotation2 <f32> {
108 self.orientation
109 }
110 pub const fn viewport_width (&self) -> u16 {
111 self.viewport_width
112 }
113 pub const fn viewport_height (&self) -> u16 {
114 self.viewport_height
115 }
116 pub const fn transform_mat_world_to_view (&self) -> math::Matrix4 <f32> {
117 self.transform_mat_world_to_view
118 }
119 pub const fn zoom (&self) -> f32 {
120 self.zoom
121 }
122 pub const fn ortho (&self) -> vek::FrustumPlanes <f32> {
123 self.ortho
124 }
125 pub const fn projection_mat_ortho (&self) -> math::Matrix4 <f32> {
126 self.projection_mat_ortho
127 }
128
129 /// Convert a screen space coordinate to world space based on the current
130 /// view.
131 ///
132 /// ```
133 /// # use gl_utils::Camera2d;
134 /// let [width, height] = [320, 240];
135 /// let mut camera2d = Camera2d::new (width, height);
136 /// let screen_coord = [width as f32 / 2.0, height as f32 / 2.0].into();
137 /// assert_eq!(camera2d.screen_to_world (screen_coord).0, [0.0, 0.0].into());
138 /// ```
139 pub fn screen_to_world (&self, screen_coord : math::Point2 <f32>)
140 -> math::Point2 <f32>
141 {
142 let screen_dimensions = [self.viewport_width, self.viewport_height].into();
143 let ndc_2d_coord =
144 graphics::screen_2d_to_ndc_2d (screen_dimensions, screen_coord);
145 let ndc_coord = ndc_2d_coord.0.with_z (0.0).with_w (1.0);
146 let view_coord = self.projection_mat_ortho().transposed() * ndc_coord;
147 let world_coord =
148 self.transform_mat_world_to_view().transposed() * view_coord;
149 world_coord.xy().into()
150 }
151
152 /// Convert a world space coordinate to screen space based on the current
153 /// view.
154 ///
155 /// ```
156 /// # use gl_utils::Camera2d;
157 /// # use math_utils as math;
158 /// let [width, height] = [320, 240];
159 /// let mut camera2d = Camera2d::new (width, height);
160 /// let world_coord = math::Point::origin();
161 /// assert_eq!(camera2d.world_to_screen (world_coord).0,
162 /// [width as f32 / 2.0, height as f32 / 2.0].into());
163 /// ```
164 pub fn world_to_screen (&self, world_coord : math::Point2 <f32>)
165 -> math::Point2 <f32>
166 {
167 let screen_dimensions = [self.viewport_width, self.viewport_height].into();
168 let world_4d_coord = world_coord.0.with_z (0.0).with_w (1.0);
169 let view_coord = self.transform_mat_world_to_view() * world_4d_coord;
170 let clip_coord = self.projection_mat_ortho() * view_coord;
171 let ndc_coord = clip_coord.xy().into();
172 graphics::ndc_2d_to_screen_2d (screen_dimensions, ndc_coord)
173 }
174
175 /// Should be called when the screen resolution changes to update the
176 /// orthographic projection state.
177 ///
178 /// # Panics
179 ///
180 /// Panics if the viewport width or height are zero:
181 ///
182 /// ```should_panic
183 /// # use gl_utils::Camera2d;
184 /// let mut camera2d = Camera2d::new (320, 240);
185 /// camera2d.set_viewport_dimensions (0, 0); // panics
186 /// ```
187 pub fn set_viewport_dimensions (&mut self,
188 viewport_width : u16, viewport_height : u16
189 ) {
190 assert!(0 < viewport_width);
191 assert!(0 < viewport_height);
192 self.viewport_width = viewport_width;
193 self.viewport_height = viewport_height;
194 self.compute_ortho();
195 }
196
197 pub fn set_position (&mut self, position : math::Point2 <f32>) {
198 if self.position != position {
199 self.position = position;
200 self.compute_transform();
201 }
202 }
203
204 /// Set the zoom level.
205 ///
206 /// # Panics
207 ///
208 /// Panics if scale factor is zero or negative:
209 ///
210 /// ```should_panic
211 /// # extern crate gl_utils;
212 /// # fn main () {
213 /// # use gl_utils::Camera2d;
214 /// let mut camera2d = Camera2d::new (320, 240);
215 /// camera2d.set_zoom (-1.0); // panics
216 /// # }
217 /// ```
218 pub fn set_zoom (&mut self, zoom : f32) {
219 assert!(0.0 < zoom);
220 if self.zoom != zoom {
221 self.zoom = zoom;
222 self.compute_ortho();
223 }
224 }
225
226 pub fn rotate (&mut self, delta_yaw : math::Rad <f32>) {
227 use std::f32::consts::PI;
228 use num::Zero;
229 use math::Angle;
230 if !delta_yaw.is_zero() {
231 self.yaw += delta_yaw;
232 if self.yaw < math::Rad (-PI) {
233 self.yaw += math::Rad::full_turn();
234 } else if self.yaw > math::Rad (PI) {
235 self.yaw -= math::Rad::full_turn();
236 }
237 self.compute_orientation();
238 }
239 }
240
241 /// Move by delta X and Y values in local coordinates
242 pub fn move_local (&mut self, delta_x : f32, delta_y : f32) {
243 self.position += (delta_x * self.orientation.cols.x)
244 + (delta_y * self.orientation.cols.y);
245 self.compute_transform();
246 }
247
248 /// Move camera so that the world-space origin will be centered on the lower
249 /// left pixel
250 pub fn move_origin_to_bottom_left (&mut self) {
251 self.position = [
252 (self.viewport_width / 2) as f32,
253 (self.viewport_height / 2) as f32
254 ].into();
255 self.compute_transform();
256 }
257
258 /// Multiply the current zoom by the given scale factor.
259 ///
260 /// # Panics
261 ///
262 /// Panics if scale factor is zero or negative:
263 ///
264 /// ```should_panic
265 /// # extern crate gl_utils;
266 /// # fn main () {
267 /// # use gl_utils::Camera2d;
268 /// let mut camera2d = Camera2d::new (320, 240);
269 /// camera2d.scale_zoom (-1.0); // panics
270 /// # }
271 /// ```
272 pub fn scale_zoom (&mut self, scale : f32) {
273 assert!(0.0 < scale);
274 self.zoom *= scale;
275 self.compute_ortho();
276 }
277
278 /// Returns the raw *world to view transform* and *ortho projection* matrix
279 /// data, suitable for use as shader uniforms.
280 #[inline]
281 pub fn view_ortho_mats (&self) -> ([[f32; 4]; 4], [[f32; 4]; 4]) {
282 ( self.transform_mat_world_to_view.into_col_arrays(),
283 self.projection_mat_ortho.into_col_arrays()
284 )
285 }
286
287 //
288 // private
289 //
290
291 #[inline]
292 fn compute_orientation (&mut self) {
293 self.orientation = math::Rotation2::from_angle (self.yaw);
294 self.compute_transform();
295 }
296 #[inline]
297 fn compute_transform (&mut self) {
298 self.transform_mat_world_to_view =
299 transform_mat_world_to_view (self.position, self.orientation);
300 }
301 /// Recomputes the ortho structure based on current viewport and zoom.
302 fn compute_ortho (&mut self) {
303 self.ortho = Self::ortho_from_viewport_zoom (
304 self.viewport_width, self.viewport_height, self.zoom);
305 self.compute_projection();
306 }
307 #[inline]
308 fn compute_projection (&mut self) {
309 self.projection_mat_ortho =
310 graphics::projection_mat_orthographic (&self.ortho);
311 }
312
313 /// Rounds the viewport to the next lower even resolution which will
314 /// cause 1px distortion but prevent mis-sampling of 2D textures at
315 /// non-power-of-two zoom levels
316 pub (crate) fn ortho_from_viewport_zoom (
317 viewport_width : u16, viewport_height : u16, zoom : f32
318 ) -> vek::FrustumPlanes <f32> {
319 let half_scaled_width = 0.5 * (
320 (viewport_width - viewport_width % 2) as f32 / zoom);
321 let half_scaled_height = 0.5 * (
322 (viewport_height - viewport_height % 2) as f32 / zoom);
323 vek::FrustumPlanes {
324 left: -half_scaled_width,
325 right: half_scaled_width,
326 bottom: -half_scaled_height,
327 top: half_scaled_height,
328 near: -1.0,
329 far: 1.0
330 }
331 }
332}
333
334/// Builds a 4x4 transformation matrix that will transform points in world
335/// space coordinates to view space coordinates based on the current 2D view
336/// position and orientation.
337///
338/// The Z coordinate of the position is always `0.0` and the view is always
339/// looking down the negative Z axis.
340// TODO: doctest ?
341pub fn transform_mat_world_to_view (
342 view_position : math::Point2 <f32>,
343 view_orientation : math::Rotation2 <f32>
344) -> math::Matrix4 <f32> {
345 let eye = view_position.0.with_z (0.0);
346 let center = eye - math::Vector3::unit_z();
347 let up = view_orientation.cols.y.with_z (0.0);
348 math::Matrix4::<f32>::look_at_rh (eye, center, up)
349}