summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
Diffstat (limited to 'engine')
-rw-r--r--engine/src/camera.rs13
-rw-r--r--engine/src/projection.rs28
-rw-r--r--engine/src/rendering/shader/default.rs62
-rw-r--r--engine/src/transform.rs11
-rw-r--r--engine/src/ui/dear_imgui.rs11
-rw-r--r--engine/src/windowing/dpi.rs12
6 files changed, 74 insertions, 63 deletions
diff --git a/engine/src/camera.rs b/engine/src/camera.rs
index b697ee3..4a7768b 100644
--- a/engine/src/camera.rs
+++ b/engine/src/camera.rs
@@ -1,4 +1,5 @@
use crate::ecs::Component;
+use crate::matrix::Matrix;
use crate::projection::{Perspective, Projection};
use crate::reflection::Reflection;
use crate::vector::Vec3;
@@ -13,6 +14,18 @@ pub struct Camera
pub projection: Projection,
}
+impl Camera
+{
+ pub fn to_view_matrix(&self, camera_world_pos: Vec3<f32>) -> Matrix<f32, 4, 4>
+ {
+ let mut view = Matrix::new();
+
+ view.look_at(camera_world_pos, self.target, self.global_up);
+
+ view
+ }
+}
+
impl Default for Camera
{
fn default() -> Self
diff --git a/engine/src/projection.rs b/engine/src/projection.rs
index 747b8a8..e2d5cc1 100644
--- a/engine/src/projection.rs
+++ b/engine/src/projection.rs
@@ -3,6 +3,7 @@ use crate::data_types::dimens::Dimens;
use crate::matrix::{CellPos as MatrixCellPos, Matrix};
use crate::reflection::Reflection;
use crate::vector::Vec2;
+use crate::windowing::dpi::PhysicalSize;
#[derive(Debug, Clone, Reflection)]
#[non_exhaustive]
@@ -12,6 +13,26 @@ pub enum Projection
Orthographic(Orthographic),
}
+impl Projection
+{
+ pub fn to_matrix_rh(
+ &self,
+ window_size: PhysicalSize<u32>,
+ clip_volume: ClipVolume,
+ ) -> Matrix<f32, 4, 4>
+ {
+ match self {
+ Projection::Perspective(perspective_proj) => perspective_proj.to_matrix_rh(
+ window_size.width as f32 / window_size.height as f32,
+ clip_volume,
+ ),
+ Projection::Orthographic(orthographic_proj) => {
+ orthographic_proj.to_matrix_rh(window_size, clip_volume)
+ }
+ }
+ }
+}
+
/// Perspective projection parameters.
#[derive(Debug, Clone, Reflection)]
pub struct Perspective
@@ -92,7 +113,7 @@ impl Orthographic
/// Creates a orthographic projection matrix using right-handed coordinates.
pub fn to_matrix_rh(
&self,
- window_size: Dimens<f32>,
+ window_size: PhysicalSize<u32>,
clip_volume: ClipVolume,
) -> Matrix<f32, 4, 4>
{
@@ -100,7 +121,10 @@ impl Orthographic
let size = match self.size {
OrthographicSize::FixedSize(fixed_size) => fixed_size,
- OrthographicSize::WindowSize => window_size,
+ OrthographicSize::WindowSize => Dimens {
+ width: window_size.width as f32,
+ height: window_size.height as f32,
+ },
};
let origin_x = size.width * self.viewport_origin.x;
diff --git a/engine/src/rendering/shader/default.rs b/engine/src/rendering/shader/default.rs
index 737916f..4c59a1b 100644
--- a/engine/src/rendering/shader/default.rs
+++ b/engine/src/rendering/shader/default.rs
@@ -3,7 +3,6 @@ use std::sync::LazyLock;
use crate::asset::{Assets, Label as AssetLabel};
use crate::camera::{Active as ActiveCamera, Camera};
-use crate::data_types::dimens::Dimens;
use crate::draw_flags::NoDraw;
use crate::ecs::actions::Actions;
use crate::ecs::pair::ChildOf;
@@ -16,9 +15,8 @@ use crate::lighting::{
PointLight,
};
use crate::material::{Flags as MaterialFlags, Material};
-use crate::matrix::Matrix;
use crate::model::{MaterialSearchResult, Model};
-use crate::projection::{ClipVolume as ProjectionClipVolume, Projection};
+use crate::projection::ClipVolume as ProjectionClipVolume;
use crate::rendering::shader::cursor::{
BindingTextureKind as ShaderBindingTextureKind,
BindingValue as ShaderBindingValue,
@@ -38,8 +36,6 @@ use crate::rendering::{
use crate::scene::{Active as ActiveScene, Scene};
use crate::texture::WHITE_1X1_ASSET_LABEL as TEXTURE_WHITE_1X1_ASSET_LABEL;
use crate::transform::{Scale, Transform, WorldPosition};
-use crate::vector::Vec3;
-use crate::windowing::dpi::PhysicalSize;
use crate::windowing::window::Window;
pub static ASSET_LABEL: LazyLock<AssetLabel> = LazyLock::new(|| AssetLabel {
@@ -147,10 +143,11 @@ pub fn enqueue_set_shader_bindings(
.unwrap(),
);
- let model_matrix = create_model_matrix(Transform {
+ let model_matrix = Transform {
position: world_pos.as_deref().cloned().unwrap_or_default().position,
scale: scale.as_deref().cloned().unwrap_or_default().scale,
- });
+ }
+ .to_matrix();
let inverted_model_matrix = model_matrix.inverse();
@@ -193,7 +190,7 @@ pub fn enqueue_set_shader_bindings(
let lighting_shader_cursor = shader_cursor.field("Uniforms").field("lighting");
let material_shader_cursor = lighting_shader_cursor.field("material");
- let view = create_view_matrix(&camera, camera_world_pos.position);
+ let view = camera.to_view_matrix(camera_world_pos.position);
shader_bindings.bindings.extend([
model_3d_shader_cursor
@@ -244,7 +241,13 @@ pub fn enqueue_set_shader_bindings(
shader_bindings.surface_specific_bindings.push((
window_surface_spec.id,
model_3d_shader_cursor.field("projection").binding(
- create_projection_matrix(&camera, &window.inner_size).into(),
+ camera
+ .projection
+ .to_matrix_rh(
+ window.inner_size,
+ ProjectionClipVolume::NegOneToOne,
+ )
+ .into(),
)?,
));
}
@@ -384,47 +387,6 @@ pub fn enqueue_set_shader_bindings(
Ok(())
}
-fn create_model_matrix(transform: Transform) -> Matrix<f32, 4, 4>
-{
- let mut matrix = Matrix::new_identity();
-
- matrix.translate(&transform.position);
- matrix.scale(&transform.scale);
-
- matrix
-}
-
-fn create_view_matrix(camera: &Camera, camera_world_pos: Vec3<f32>) -> Matrix<f32, 4, 4>
-{
- let mut view = Matrix::new();
-
- // tracing::debug!("Camera target: {:?}", camera.target);
-
- view.look_at(camera_world_pos, camera.target, camera.global_up);
-
- view
-}
-
-fn create_projection_matrix(
- camera: &Camera,
- window_size: &PhysicalSize<u32>,
-) -> Matrix<f32, 4, 4>
-{
- match &camera.projection {
- Projection::Perspective(perspective_proj) => perspective_proj.to_matrix_rh(
- window_size.width as f32 / window_size.height as f32,
- ProjectionClipVolume::NegOneToOne,
- ),
- Projection::Orthographic(orthographic_proj) => orthographic_proj.to_matrix_rh(
- Dimens {
- width: window_size.width as f32,
- height: window_size.height as f32,
- },
- ProjectionClipVolume::NegOneToOne,
- ),
- }
-}
-
type RenderableEntity<'a> = (
&'a Model,
Traverse<
diff --git a/engine/src/transform.rs b/engine/src/transform.rs
index 7f89807..3b1c041 100644
--- a/engine/src/transform.rs
+++ b/engine/src/transform.rs
@@ -1,5 +1,6 @@
use crate::builder;
use crate::ecs::Component;
+use crate::matrix::Matrix;
use crate::reflection::Reflection;
use crate::vector::Vec3;
@@ -20,6 +21,16 @@ impl Transform
{
Builder::default()
}
+
+ pub fn to_matrix(&self) -> Matrix<f32, 4, 4>
+ {
+ let mut matrix = Matrix::new_identity();
+
+ matrix.translate(&self.position);
+ matrix.scale(&self.scale);
+
+ matrix
+ }
}
impl Default for Transform
diff --git a/engine/src/ui/dear_imgui.rs b/engine/src/ui/dear_imgui.rs
index 212f24c..bcdefa1 100644
--- a/engine/src/ui/dear_imgui.rs
+++ b/engine/src/ui/dear_imgui.rs
@@ -705,13 +705,14 @@ fn add_drawing_render_pass(
.near(1.0)
.far(-1.0)
.viewport_origin(Vec2 { x: 0.0, y: 1.0 })
- .size(crate::projection::OrthographicSize::WindowSize)
+ .size(crate::projection::OrthographicSize::FixedSize(Dimens {
+ width: display_width,
+ height: -display_height,
+ }))
.build()
.to_matrix_rh(
- Dimens {
- width: display_width,
- height: -display_height,
- },
+ // Window size can be 0x0 since it will not be used
+ PhysicalSize::default(),
ProjectionClipVolume::NegOneToOne,
)
.into(),
diff --git a/engine/src/windowing/dpi.rs b/engine/src/windowing/dpi.rs
index d628709..6cd2806 100644
--- a/engine/src/windowing/dpi.rs
+++ b/engine/src/windowing/dpi.rs
@@ -48,7 +48,7 @@ macro_rules! gen_enum_from_to_impls {
};
}
-#[derive(Debug, Default, Clone, PartialEq, Reflection)]
+#[derive(Debug, Default, Clone, Copy, PartialEq, Reflection)]
#[reflection(impl_with_generics(<u32>, <i32>, <f32>, <f64>))]
pub struct PhysicalPosition<Pixel>
{
@@ -90,7 +90,7 @@ impl<Pixel> PhysicalPosition<Pixel>
gen_struct_from_to_impls!(PhysicalPosition, fields = (x, y));
-#[derive(Debug, Default, Clone, PartialEq, Reflection)]
+#[derive(Debug, Default, Clone, Copy, PartialEq, Reflection)]
#[reflection(impl_with_generics(<f64>))]
pub struct LogicalPosition<Pixel>
{
@@ -115,7 +115,7 @@ impl<Pixel> LogicalPosition<Pixel>
gen_struct_from_to_impls!(LogicalPosition, fields = (x, y));
-#[derive(Debug, Clone, PartialEq, Reflection)]
+#[derive(Debug, Clone, Copy, PartialEq, Reflection)]
pub enum Position
{
Physical(PhysicalPosition<i32>),
@@ -124,7 +124,7 @@ pub enum Position
gen_enum_from_to_impls!(Position, variants = (Physical, Logical));
-#[derive(Debug, Default, Clone, PartialEq, Reflection)]
+#[derive(Debug, Default, Clone, Copy, PartialEq, Reflection)]
#[reflection(impl_with_generics(<u32>))]
pub struct PhysicalSize<Pixel>
{
@@ -166,7 +166,7 @@ impl<Pixel> PhysicalSize<Pixel>
gen_struct_from_to_impls!(PhysicalSize, fields = (width, height));
-#[derive(Debug, Default, Clone, PartialEq, Reflection)]
+#[derive(Debug, Default, Clone, Copy, PartialEq, Reflection)]
#[reflection(impl_with_generics(<f64>))]
pub struct LogicalSize<Pixel>
{
@@ -191,7 +191,7 @@ impl<Pixel> LogicalSize<Pixel>
gen_struct_from_to_impls!(LogicalSize, fields = (width, height));
-#[derive(Debug, Clone, PartialEq, Reflection)]
+#[derive(Debug, Clone, Copy, PartialEq, Reflection)]
pub enum Size
{
Physical(PhysicalSize<u32>),