aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortcmal <me@aria.rip>2024-08-25 17:44:23 +0100
committertcmal <me@aria.rip>2024-08-25 17:44:23 +0100
commitccf0074b08ce835cf22e7d46153d1cb3f3d06d32 (patch)
treecce0a39b4d1e3f272f071d5b9f2de9a403d396b4
parent6fd563b20772ad0ad279f6461179978766a77948 (diff)
fix(render): get aspect ratio from renderer
-rw-r--r--examples/render-quad/src/main.rs43
-rw-r--r--stockton-render/src/draw/camera.rs15
-rw-r--r--stockton-render/src/lib.rs5
-rw-r--r--stockton-types/src/session.rs10
4 files changed, 36 insertions, 37 deletions
diff --git a/examples/render-quad/src/main.rs b/examples/render-quad/src/main.rs
index 9c3748d..3dbb824 100644
--- a/examples/render-quad/src/main.rs
+++ b/examples/render-quad/src/main.rs
@@ -149,31 +149,26 @@ fn try_main<'a>() -> Result<()> {
MovementInputsManager::new(actions)
};
- let ratio = window.inner_size().width as f32 / window.inner_size().height as f32;
-
// Load everything into the session
- let mut session = Session::new(
- |resources| {
- resources.insert(map.clone());
- resources.insert(manager);
- resources.insert(Timing::default());
- resources.insert(Mouse::default());
- resources.insert(ui);
- },
- move |schedule| {
- schedule
- .add_system(update_deltatime_system())
- .add_system(process_window_events_system::<
- MovementInputsManager,
- Dp<'static>,
- >())
- .flush()
- .add_system(hello_world_system())
- .add_system(flycam_move_system::<MovementInputsManager>())
- .flush()
- .add_thread_local(calc_vp_matrix_system(ratio));
- },
- );
+ let mut session = Session::new(move |schedule| {
+ schedule
+ .add_system(update_deltatime_system())
+ .add_system(process_window_events_system::<
+ MovementInputsManager,
+ Dp<'static>,
+ >())
+ .flush()
+ .add_system(hello_world_system())
+ .add_system(flycam_move_system::<MovementInputsManager>())
+ .flush()
+ .add_thread_local(calc_vp_matrix_system::<Dp<'static>>());
+ });
+
+ session.resources.insert(map.clone());
+ session.resources.insert(manager);
+ session.resources.insert(Timing::default());
+ session.resources.insert(Mouse::default());
+ session.resources.insert(ui);
// Add our player entity
let player = session.world.push((
diff --git a/stockton-render/src/draw/camera.rs b/stockton-render/src/draw/camera.rs
index 495e6a2..62784de 100644
--- a/stockton-render/src/draw/camera.rs
+++ b/stockton-render/src/draw/camera.rs
@@ -7,9 +7,9 @@ use stockton_types::{
Vector3,
};
+use crate::Renderer;
-
-
+use super::DrawPass;
fn euler_to_direction(euler: &Vector3) -> Vector3 {
let pitch = euler.x;
@@ -25,11 +25,11 @@ fn euler_to_direction(euler: &Vector3) -> Vector3 {
#[system(for_each)]
#[filter(maybe_changed::<Transform>() | maybe_changed::<CameraSettings>())]
-pub fn calc_vp_matrix(
+pub fn calc_vp_matrix<DP: DrawPass + 'static>(
transform: &Transform,
settings: &CameraSettings,
matrix: &mut CameraVPMatrix,
- #[state] ratio: &mut f32,
+ #[resource] renderer: &Renderer<DP>,
) {
// Get look direction from euler angles
let direction = euler_to_direction(&transform.rotation);
@@ -43,7 +43,12 @@ pub fn calc_vp_matrix(
// Converts camera space to screen space
let projection_matrix = {
- let mut temp = perspective_lh_zo(*ratio, settings.fov, settings.near, settings.far);
+ 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;
diff --git a/stockton-render/src/lib.rs b/stockton-render/src/lib.rs
index 6ab7740..37acf27 100644
--- a/stockton-render/src/lib.rs
+++ b/stockton-render/src/lib.rs
@@ -80,6 +80,11 @@ impl<DP: DrawPass> Renderer<DP> {
Ok(())
}
+ pub fn get_aspect_ratio(&self) -> f32 {
+ let e = self.context.target_chain.properties.extent;
+ e.width as f32 / e.height as f32
+ }
+
fn resize(&mut self) -> Result<()> {
unsafe { self.context.handle_surface_change() }
}
diff --git a/stockton-types/src/session.rs b/stockton-types/src/session.rs
index f3d4e50..5b91739 100644
--- a/stockton-types/src/session.rs
+++ b/stockton-types/src/session.rs
@@ -11,17 +11,11 @@ pub struct Session {
}
impl Session {
- /// Create a new world from a level.
/// The level can be any format, as long as it has the required features of a bsp.
- pub fn new<R: FnOnce(&mut Resources), S: FnOnce(&mut Builder)>(
- add_resources: R,
- add_systems: S,
- ) -> Session {
+ pub fn new<S: FnOnce(&mut Builder)>(add_systems: S) -> Session {
let world = World::default();
- let mut resources = Resources::default();
- add_resources(&mut resources);
-
+ let resources = Resources::default();
let mut schedule = Schedule::builder();
add_systems(&mut schedule);
let schedule = schedule.build();