1use glam::{Mat4, Vec3};
4
5#[derive(Clone, Copy, Debug)]
6pub struct Camera {
7 pub position: Vec3,
8 pub target: Vec3,
9 pub up: Vec3,
10 pub fov: f32,
11 pub near: f32,
12 pub far: f32,
13}
14
15impl Default for Camera {
16 fn default() -> Self {
17 Self {
18 position: Vec3::new(0.0, 0.0, 5.0),
19 target: Vec3::ZERO,
20 up: Vec3::Y,
21 fov: 45.0_f32.to_radians(),
22 near: 0.1,
23 far: 100.0,
24 }
25 }
26}
27
28impl Camera {
29 pub fn new() -> Self {
30 Self::default()
31 }
32
33 pub fn view_matrix(&self) -> Mat4 {
34 Mat4::look_at_rh(self.position, self.target, self.up)
35 }
36
37 pub fn projection_matrix(&self, aspect_ratio: f32) -> Mat4 {
38 Mat4::perspective_rh(self.fov, aspect_ratio, self.near, self.far)
39 }
40
41 pub fn view_projection_matrix(&self, aspect_ratio: f32) -> Mat4 {
42 self.projection_matrix(aspect_ratio) * self.view_matrix()
43 }
44
45 pub fn forward(&self) -> Vec3 {
46 (self.target - self.position).normalize_or_zero()
47 }
48}