diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
commit | 0353181306702c40ad0fe482b5c2b159b46794a4 (patch) | |
tree | 33acc6a9e8ea4705884cf93b78cf869008f71832 /stockton-render/src/camera.rs | |
parent | 664f0b0777ba96298b29f0c753d52a81cbb233f1 (diff) |
refactor(all): rename some crates
Diffstat (limited to 'stockton-render/src/camera.rs')
-rw-r--r-- | stockton-render/src/camera.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/stockton-render/src/camera.rs b/stockton-render/src/camera.rs new file mode 100644 index 0000000..dcc9d93 --- /dev/null +++ b/stockton-render/src/camera.rs @@ -0,0 +1,59 @@ +//! Things related to converting 3D world space to 2D screen space + +use legion::maybe_changed; +use na::{look_at_lh, perspective_lh_zo}; +use stockton_types::{ + components::{CameraSettings, CameraVPMatrix, Transform}, + Vector3, +}; + +use stockton_skeleton::{draw_passes::DrawPass, Renderer}; + +fn euler_to_direction(euler: &Vector3) -> Vector3 { + let pitch = euler.x; + let yaw = euler.y; + let _roll = euler.z; // TODO: Support camera roll + + Vector3::new( + yaw.sin() * pitch.cos(), + pitch.sin(), + yaw.cos() * pitch.cos(), + ) +} + +#[system(for_each)] +#[filter(maybe_changed::<Transform>() | maybe_changed::<CameraSettings>())] +pub fn calc_vp_matrix<DP: DrawPass + 'static>( + transform: &Transform, + settings: &CameraSettings, + matrix: &mut CameraVPMatrix, + #[resource] renderer: &Renderer<DP>, +) { + // Get look direction from euler angles + let direction = euler_to_direction(&transform.rotation); + + // Converts world space to camera space + let view_matrix = look_at_lh( + &transform.position, + &(transform.position + direction), + &Vector3::new(0.0, 1.0, 0.0), + ); + + // Converts camera space to screen space + let projection_matrix = { + let mut temp = perspective_lh_zo( + renderer.get_aspect_ratio(), + settings.fov, + settings.near, + settings.far, + ); + + // Vulkan's co-ord system is different from OpenGLs + temp[(1, 1)] *= -1.0; + + temp + }; + + // Chain them together into a single matrix + matrix.vp_matrix = projection_matrix * view_matrix; +} |