Add 2d camera

This commit is contained in:
SeanOMik 2023-09-14 22:33:11 -04:00
parent bd21e62cba
commit 2e08a5a784
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
2 changed files with 40 additions and 20 deletions

View File

@ -20,7 +20,13 @@ impl Default for CameraComponent {
} }
impl CameraComponent { impl CameraComponent {
pub fn new() -> Self { pub fn new_3d() -> Self {
Self::default() Self::default()
} }
pub fn new_2d() -> Self {
let mut s = Self::default();
s.mode = CameraProjectionMode::Orthographic;
s
}
} }

View File

@ -40,6 +40,7 @@ impl Projection {
pub struct RenderCamera { pub struct RenderCamera {
view_proj: glam::Mat4, view_proj: glam::Mat4,
size: PhysicalSize<u32>,
aspect: f32, aspect: f32,
znear: f32, znear: f32,
zfar: f32, zfar: f32,
@ -50,6 +51,7 @@ impl RenderCamera {
Self { Self {
view_proj: glam::Mat4::IDENTITY, view_proj: glam::Mat4::IDENTITY,
size,
aspect: size.width as f32 / size.height as f32, aspect: size.width as f32 / size.height as f32,
znear: 0.1, znear: 0.1,
zfar: 100.0, zfar: 100.0,
@ -61,14 +63,12 @@ impl RenderCamera {
} }
pub fn update_view_projection(&mut self, camera: &CameraComponent) -> &glam::Mat4 { pub fn update_view_projection(&mut self, camera: &CameraComponent) -> &glam::Mat4 {
match camera.mode {
CameraProjectionMode::Perspective => {
let position = camera.transform.translation; let position = camera.transform.translation;
let target = camera.transform.rotation * glam::Vec3::new(0.0, 0.0, -1.0); let target = camera.transform.rotation * glam::Vec3::new(0.0, 0.0, -1.0);
let target = target.normalize(); let target = target.normalize();
/* debug!("Camera rotation: {:?}", rotation);
debug!("Camera position: {:?}", position);
debug!("Camera target: {:?}", target); */
let view = glam::Mat4::look_to_rh( let view = glam::Mat4::look_to_rh(
position, position,
target, target,
@ -79,9 +79,23 @@ impl RenderCamera {
self.view_proj = OPENGL_TO_WGPU_MATRIX * proj * view; self.view_proj = OPENGL_TO_WGPU_MATRIX * proj * view;
&self.view_proj &self.view_proj
} },
CameraProjectionMode::Orthographic => {
let position = camera.transform.translation;
let target = camera.transform.rotation * glam::Vec3::new(0.0, 0.0, -1.0);
let target = target.normalize();
pub fn view_proj(&self) -> &glam::Mat4 { let ratio_size_per_depth = ((camera.fov.to_radians() / 2.0) * 2.0).atan();
let distance = (target - position).length();
let size_y = ratio_size_per_depth * distance;
let size_x = ratio_size_per_depth * distance * self.aspect;
let proj = glam::Mat4::orthographic_rh_gl(-size_x, size_x, -size_y, size_y, self.znear, self.zfar);
self.view_proj = OPENGL_TO_WGPU_MATRIX * proj;
&self.view_proj &self.view_proj
},
}
} }
} }