use cgmath::*;
pub enum CameraType {
Perspective {
fov: Deg<f32>,
aspect: f32,
near: f32,
far: f32,
},
Orthographic {
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
},
}
pub struct Camera {
pub position: Point3<f32>,
pub target: Point3<f32>,
pub up: Vector3<f32>,
camera_type: CameraType,
}
impl Camera {
pub fn new(
position: Point3<f32>,
target: Point3<f32>,
up: Vector3<f32>,
camera_type: CameraType,
) -> Self {
Self {
position,
target,
up,
camera_type,
}
}
pub fn view_matrix(&self) -> Matrix4<f32> {
Matrix4::look_at_rh(self.position, self.target, self.up)
}
pub fn projection_matrix(&self) -> Matrix4<f32> {
match &self.camera_type {
CameraType::Perspective {
fov,
aspect,
near,
far,
} => perspective(*fov, *aspect, *near, *far).into(),
CameraType::Orthographic {
left,
right,
bottom,
top,
near,
far,
} => ortho(*left, *right, *bottom, *top, *near, *far).into(),
}
}
}