use super::Camera;
use crate::math::{Matrix4, Vector3};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SensorFit {
#[default]
Auto,
Horizontal,
Vertical,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ProjectionKind {
#[default]
Perspective,
Equirectangular,
FisheyeEquidistant,
MirrorBall,
}
#[derive(Debug, Clone)]
pub struct PerspectiveCamera {
pub position: Vector3,
pub target: Vector3,
pub up: Vector3,
pub fov: f32, pub aspect: f32,
pub near: f32,
pub far: f32,
pub focus_distance: f32,
pub aperture: f32,
pub f_stop: f32,
pub aperture_blades: u32,
pub anamorphic_ratio: f32,
pub shift_x: f32,
pub shift_y: f32,
pub sensor_fit: SensorFit,
pub projection: ProjectionKind,
pub projection_override: Option<[f32; 16]>,
}
impl PerspectiveCamera {
pub fn new(fov_deg: f32, aspect: f32, near: f32, far: f32) -> Self {
Self {
position: Vector3::new(0.0, 0.0, 5.0),
target: Vector3::ZERO,
up: Vector3::UP,
fov: fov_deg.to_radians(),
aspect,
near,
far,
focus_distance: 0.0,
aperture: 0.0,
f_stop: 0.0,
aperture_blades: 0,
anamorphic_ratio: 1.0,
shift_x: 0.0,
shift_y: 0.0,
sensor_fit: SensorFit::Auto,
projection: ProjectionKind::Perspective,
projection_override: None,
}
}
pub fn look_at(&mut self, target: Vector3) -> &mut Self {
self.target = target;
self
}
}
impl Camera for PerspectiveCamera {
fn view_matrix(&self) -> Matrix4 {
Matrix4::look_at(self.position, self.target, self.up)
}
fn projection_matrix(&self) -> Matrix4 {
if let Some(m) = self.projection_override {
return Matrix4 { elements: m };
}
if self.shift_x.abs() > 1e-8 || self.shift_y.abs() > 1e-8 {
Matrix4::perspective_with_shift(
self.fov,
self.aspect,
self.near,
self.far,
self.shift_x,
self.shift_y,
)
} else {
Matrix4::perspective(self.fov, self.aspect, self.near, self.far)
}
}
fn position(&self) -> Vector3 {
self.position
}
fn set_aspect(&mut self, aspect: f32) {
self.aspect = aspect;
}
fn near_far(&self) -> (f32, f32) {
(self.near, self.far)
}
}