41 lines
1.3 KiB
Rust
41 lines
1.3 KiB
Rust
use cgmath::{Point3, Vector3, Matrix4, Deg, InnerSpace, Zero};
|
|
|
|
|
|
pub struct Camera {
|
|
position: Point3<f32>,
|
|
proj_matrix: Matrix4<f32>,
|
|
view_matrix: Matrix4<f32>
|
|
}
|
|
|
|
impl Camera {
|
|
|
|
pub fn new(aspect: f32) -> Self {
|
|
let mut instance = Self {
|
|
position: (2.0,1.0,2.0).into(),
|
|
view_matrix: Matrix4::look_to_rh(
|
|
(0.0,0.0,0.0).into(),
|
|
Vector3{ x:0.66, y:0.0, z:0.66}.normalize(),
|
|
Vector3::unit_y()
|
|
),
|
|
proj_matrix: Matrix4::zero()
|
|
};
|
|
instance.set_aspect(aspect);
|
|
instance
|
|
}
|
|
|
|
pub fn set_aspect(&mut self, aspect: f32) { self.proj_matrix = cgmath::perspective(Deg(60.0), aspect, 0.1, 100.0); }
|
|
pub fn get_projection(&self) -> Matrix4<f32> { self.proj_matrix }
|
|
pub fn get_view(&self) -> Matrix4<f32> { self.view_matrix }
|
|
pub fn get_position(&self) -> Point3<f32> { self.position }
|
|
}
|
|
|
|
pub trait CameraTransform {
|
|
fn update(&mut self, position: Point3<f32>, forward: Vector3<f32>, up: Vector3<f32>);
|
|
}
|
|
|
|
impl CameraTransform for Camera {
|
|
fn update(&mut self, position: Point3<f32>, forward: Vector3<f32>, up: Vector3<f32>) {
|
|
self.position = position;
|
|
self.view_matrix = Matrix4::look_to_rh((0.0,0.0,0.0).into(), forward, up);
|
|
}
|
|
} |