more project structure cleanup, removing spaghetti code, using traits
This commit is contained in:
+32
-29
@@ -1,38 +1,41 @@
|
||||
use cgmath::{Point3, Vector3, Matrix4, Deg, InnerSpace};
|
||||
use cgmath::{Point3, Vector3, Matrix4, Deg, InnerSpace, Zero};
|
||||
|
||||
|
||||
pub struct Camera {
|
||||
pub position: Point3<f32>,
|
||||
pub forward: Vector3<f32>,
|
||||
pub up: Vector3<f32>,
|
||||
aspect: f32,
|
||||
fovy: f32,
|
||||
znear: f32,
|
||||
zfar: f32,
|
||||
position: Point3<f32>,
|
||||
proj_matrix: Matrix4<f32>,
|
||||
view_matrix: Matrix4<f32>
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
|
||||
pub fn new(aspect: f32, fov: f32) -> Self {
|
||||
Self {
|
||||
position: (1.0, 2.0, 1.0).into(),
|
||||
forward: (0.66, -0.25, 0.66).into(),
|
||||
up: Vector3::unit_y(),
|
||||
aspect,
|
||||
fovy: fov,
|
||||
znear: 0.1,
|
||||
zfar: 100.0
|
||||
}
|
||||
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.aspect = aspect;
|
||||
}
|
||||
|
||||
pub fn proj_matrix(&self) -> Matrix4<f32> {
|
||||
return cgmath::perspective(Deg(self.fovy), self.aspect, self.znear, self.zfar);
|
||||
}
|
||||
|
||||
pub fn view_matrix(&self) -> Matrix4<f32> {
|
||||
return Matrix4::look_to_rh((0.0,0.0,0.0).into(), self.forward.normalize(), self.up);
|
||||
}
|
||||
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> { return self.proj_matrix; }
|
||||
pub fn get_view(&self) -> Matrix4<f32> { return self.view_matrix; }
|
||||
pub fn get_position(&self) -> Point3<f32> { return 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user