15141
|
1 |
use integral_geometry::{Point, Rect, Size};
|
|
2 |
|
|
3 |
#[derive(Debug)]
|
|
4 |
pub struct Camera {
|
|
5 |
pub position: Point,
|
|
6 |
pub zoom: f32,
|
|
7 |
size: Size,
|
|
8 |
}
|
|
9 |
|
|
10 |
impl Camera {
|
|
11 |
pub fn new() -> Self {
|
|
12 |
Self::with_size(Size::new(1024, 768))
|
|
13 |
}
|
|
14 |
|
|
15 |
pub fn with_size(size: Size) -> Self {
|
|
16 |
Self {
|
|
17 |
position: Point::ZERO,
|
|
18 |
zoom: 1.0,
|
|
19 |
size,
|
|
20 |
}
|
|
21 |
}
|
|
22 |
|
|
23 |
pub fn viewport(&self) -> Rect {
|
|
24 |
#[inline]
|
|
25 |
fn scale(value: usize, zoom: f32) -> i32 {
|
|
26 |
(value as f32 / zoom / 2.0) as i32
|
|
27 |
}
|
|
28 |
let half_width = scale(self.size.width, self.zoom);
|
|
29 |
let half_height = scale(self.size.height, self.zoom);
|
|
30 |
Rect::from_box(
|
|
31 |
self.position.x - half_width,
|
|
32 |
self.position.x + half_width,
|
|
33 |
self.position.y - half_height,
|
|
34 |
self.position.y + half_height,
|
|
35 |
)
|
|
36 |
}
|
|
37 |
|
|
38 |
pub fn projection(&self) -> [f32; 16] {
|
|
39 |
let viewport = self.viewport();
|
|
40 |
let left = viewport.left() as f32;
|
|
41 |
let width = viewport.width() as f32;
|
|
42 |
let height = viewport.height() as f32;
|
|
43 |
let top = viewport.top() as f32;
|
|
44 |
|
|
45 |
[
|
|
46 |
2f32 / width,
|
|
47 |
0f32,
|
|
48 |
0f32,
|
|
49 |
0f32,
|
|
50 |
0f32,
|
|
51 |
2f32 / -height,
|
|
52 |
0f32,
|
|
53 |
0f32,
|
|
54 |
0f32,
|
|
55 |
0f32,
|
|
56 |
0.5f32,
|
|
57 |
0f32,
|
|
58 |
-(2.0 * left + width) / width,
|
|
59 |
(2.0 * top + height) / height,
|
|
60 |
0.5f32,
|
|
61 |
1f32,
|
|
62 |
]
|
|
63 |
}
|
|
64 |
}
|