diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:24 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:24 +0100 |
commit | 4f068467c4954fb79e6ce297ae1ac0fdd2bdf16a (patch) | |
tree | 4b3ec44d068f71ab80e8c85a6515a8862407ac56 /stockton-render/src | |
parent | 5e6396ed225be9a9991705de10174b3cf085f8f0 (diff) |
WIP refactor(skeleton): type state for context
also some api improvements
closes #2
related: #7
Diffstat (limited to 'stockton-render/src')
-rw-r--r-- | stockton-render/src/camera.rs | 62 | ||||
-rw-r--r-- | stockton-render/src/level.rs | 77 | ||||
-rw-r--r-- | stockton-render/src/lib.rs | 1 | ||||
-rw-r--r-- | stockton-render/src/ui.rs | 9 | ||||
-rw-r--r-- | stockton-render/src/window.rs | 7 |
5 files changed, 68 insertions, 88 deletions
diff --git a/stockton-render/src/camera.rs b/stockton-render/src/camera.rs deleted file mode 100644 index 6abc183..0000000 --- a/stockton-render/src/camera.rs +++ /dev/null @@ -1,62 +0,0 @@ -//! 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, Singular}, - 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<Singular> + '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; -} diff --git a/stockton-render/src/level.rs b/stockton-render/src/level.rs index 3851cee..29ab7c5 100644 --- a/stockton-render/src/level.rs +++ b/stockton-render/src/level.rs @@ -22,18 +22,10 @@ use stockton_skeleton::{ types::*, }; use stockton_types::{ - components::{CameraSettings, CameraVPMatrix, Transform}, + components::{CameraSettings, Transform}, *, }; -use std::{ - array::IntoIter, - convert::TryInto, - iter::{empty, once}, - marker::PhantomData, - sync::{Arc, RwLock}, -}; - use anyhow::{Context, Result}; use hal::{ buffer::SubRange, @@ -48,7 +40,15 @@ use hal::{ }, }; use legion::{Entity, IntoQuery}; +use na::{look_at_lh, perspective_lh_zo}; use shaderc::ShaderKind; +use std::{ + array::IntoIter, + convert::TryInto, + iter::{empty, once}, + marker::PhantomData, + sync::{Arc, RwLock}, +}; use thiserror::Error; /// The Vertexes that go to the shader @@ -90,10 +90,43 @@ where .record_commit_cmds(cmd_buffer)?; // Get level & camera - let mut query = <(&Transform, &CameraSettings, &CameraVPMatrix)>::query(); - let (camera_transform, camera_settings, camera_vp) = query + let mut query = <(&Transform, &CameraSettings)>::query(); + let (camera_transform, camera_settings) = query .get(&session.world, self.active_camera) .context("Couldn't find camera components")?; + + let camera_vp = { + let aspect_ratio = + self.pipeline.render_area.w as f32 / self.pipeline.render_area.h as f32; + + // Get look direction from euler angles + let direction = euler_to_direction(&camera_transform.rotation); + + // Converts world space to camera space + let view_matrix = look_at_lh( + &camera_transform.position, + &(camera_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( + aspect_ratio, + camera_settings.fov, + camera_settings.near, + camera_settings.far, + ); + + // Vulkan's co-ord system is different from OpenGLs + temp[(1, 1)] *= -1.0; + + temp + }; + + // Chain them together into a single matrix + projection_matrix * view_matrix + }; let map_lock: Arc<RwLock<M>> = session.resources.get::<Arc<RwLock<M>>>().unwrap().clone(); let map = map_lock.read().map_err(|_| LockPoisoned::Map)?; @@ -131,7 +164,7 @@ where cmd_buffer.bind_graphics_pipeline(&self.pipeline.pipeline); // VP Matrix - let vp = &*(camera_vp.vp_matrix.data.as_slice() as *const [f32] as *const [u32]); + let vp = &*(camera_vp.data.as_slice() as *const [f32] as *const [u32]); cmd_buffer.push_graphics_constants( &self.pipeline.pipeline_layout, @@ -238,7 +271,7 @@ where fn deactivate(self, context: &mut RenderingContext) -> Result<()> { self.draw_buffers.deactivate(context); unsafe { - let mut device = context.device().write().map_err(|_| LockPoisoned::Device)?; + let mut device = context.lock_device()?; self.pipeline.deactivate(&mut device); for fb in self.framebuffers.dissolve() { device.destroy_framebuffer(fb); @@ -402,7 +435,7 @@ where let draw_buffers = DrawBuffers::from_context(context).context("Error creating draw buffers")?; let (pipeline, framebuffers) = { - let mut device = context.device().write().map_err(|_| LockPoisoned::Device)?; + let mut device = context.lock_device()?; let pipeline = spec .build( &mut device, @@ -457,8 +490,8 @@ where }) } - fn find_aux_queues<'c>( - adapter: &'c Adapter, + fn find_aux_queues( + adapter: &Adapter, queue_negotiator: &mut QueueFamilyNegotiator, ) -> Result<()> { queue_negotiator.find(adapter, &TexLoadQueue, 1)?; @@ -473,3 +506,15 @@ pub enum LevelError { #[error("Referential Integrity broken")] BadReference, } + +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(), + ) +} diff --git a/stockton-render/src/lib.rs b/stockton-render/src/lib.rs index e3e5bf8..34f5117 100644 --- a/stockton-render/src/lib.rs +++ b/stockton-render/src/lib.rs @@ -3,7 +3,6 @@ extern crate legion; extern crate gfx_hal as hal; extern crate nalgebra_glm as na; -pub mod camera; pub mod level; pub mod ui; pub mod window; diff --git a/stockton-render/src/ui.rs b/stockton-render/src/ui.rs index f688f42..474c1dd 100644 --- a/stockton-render/src/ui.rs +++ b/stockton-render/src/ui.rs @@ -9,7 +9,6 @@ use stockton_skeleton::{ }, context::RenderingContext, draw_passes::{util::TargetSpecificResources, DrawPass, IntoDrawPass, PassPosition}, - error::LockPoisoned, mem::{DataPool, StagingPool, TexturesPool}, queue_negotiator::QueueFamilyNegotiator, texture::{ @@ -201,7 +200,7 @@ impl<'a, P: PassPosition> DrawPass<P> for UiDrawPass<'a> { self.draw_buffers.deactivate(context); unsafe { - let mut device = context.device().write().map_err(|_| LockPoisoned::Device)?; + let mut device = context.lock_device()?; self.pipeline.deactivate(&mut device); for fb in self.framebuffers.dissolve() { device.destroy_framebuffer(fb); @@ -307,7 +306,7 @@ impl<'a, P: PassPosition> IntoDrawPass<UiDrawPass<'a>, P> for () { DrawBuffers::from_context(context).context("Error creating draw buffers")?; let (pipeline, framebuffers) = { - let mut device = context.device().write().map_err(|_| LockPoisoned::Device)?; + let mut device = context.lock_device()?; let pipeline = spec .build( @@ -339,8 +338,8 @@ impl<'a, P: PassPosition> IntoDrawPass<UiDrawPass<'a>, P> for () { }) } - fn find_aux_queues<'c>( - adapter: &'c Adapter, + fn find_aux_queues( + adapter: &Adapter, queue_negotiator: &mut QueueFamilyNegotiator, ) -> Result<()> { queue_negotiator.find(adapter, &TexLoadQueue, 1)?; diff --git a/stockton-render/src/window.rs b/stockton-render/src/window.rs index e6cf7fb..429a3c2 100644 --- a/stockton-render/src/window.rs +++ b/stockton-render/src/window.rs @@ -216,7 +216,7 @@ impl WindowFlow { #[system] /// A system to process the window events sent to renderer by the winit event loop. -pub fn _process_window_events<T: 'static + InputManager, DP: 'static + DrawPass<Singular>>( +pub fn _process_window_events<T: 'static + InputManager>( #[resource] window_channel: &mut WindowFlow, #[resource] manager: &mut T, #[resource] mouse: &mut Mouse, @@ -273,7 +273,6 @@ pub fn _process_window_events<T: 'static + InputManager, DP: 'static + DrawPass< manager.handle_frame(&actions_buf[0..actions_buf_cursor]); } -pub fn process_window_events_system<T: 'static + InputManager, DP: 'static + DrawPass<Singular>>( -) -> impl Runnable { - _process_window_events_system::<T, DP>(Vec::with_capacity(4)) +pub fn process_window_events_system<T: 'static + InputManager>() -> impl Runnable { + _process_window_events_system::<T>(Vec::with_capacity(4)) } |