diff options
author | HampusM <hampus@hampusmat.com> | 2024-06-20 20:25:07 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-06-20 20:25:07 +0200 |
commit | 3ff91ac64308141968fdcbf3b3c09a01b0c60c97 (patch) | |
tree | b0c94c78009c5abdd16f88bedc98a41381fa6b1b /engine | |
parent | e945f53395be9e0cd1e3a6894bc5e7fa08330298 (diff) |
refactor(engine): replace Transform with Position & Scale structs
Diffstat (limited to 'engine')
-rw-r--r-- | engine/src/renderer/opengl.rs | 38 | ||||
-rw-r--r-- | engine/src/transform.rs | 33 |
2 files changed, 51 insertions, 20 deletions
diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs index 8c416f0..35f4938 100644 --- a/engine/src/renderer/opengl.rs +++ b/engine/src/renderer/opengl.rs @@ -41,8 +41,8 @@ use crate::opengl::{clear_buffers, enable, BufferClearMask, Capability}; use crate::projection::{new_perspective_matrix, Projection}; use crate::shader::Program as ShaderProgram; use crate::texture::{Id as TextureId, Texture}; -use crate::transform::Transform; -use crate::vector::Vec2; +use crate::transform::{Position, Scale}; +use crate::vector::{Vec2, Vec3}; use crate::vertex::{AttributeComponentType, Vertex}; use crate::window::Window; @@ -100,7 +100,8 @@ fn render( ShaderProgram, Material, Option<MaterialFlags>, - Transform, + Option<Position>, + Option<Scale>, )>, point_light_query: Query<(PointLight,)>, directional_lights: Query<(DirectionalLight,)>, @@ -136,7 +137,7 @@ fn render( clear_buffers(BufferClearMask::COLOR | BufferClearMask::DEPTH); - for (mesh, shader_program, material, material_flags, transform) in &query { + for (mesh, shader_program, material, material_flags, position, scale) in &query { let material_flags = material_flags .map(|material_flags| material_flags.clone()) .unwrap_or_default(); @@ -146,7 +147,10 @@ fn render( .or_insert_with(|| create_gl_shader_program(&shader_program).unwrap()); apply_transformation_matrices( - &transform, + Transformation { + position: position.map(|pos| *pos).unwrap_or_default().position, + scale: scale.map(|scale| *scale).unwrap_or_default().scale, + }, shader_program, &camera, window.size().expect("Failed to get window size"), @@ -338,13 +342,16 @@ impl Renderable } fn apply_transformation_matrices( - transform: &Transform, + transformation: Transformation, gl_shader_program: &mut GlShaderProgram, camera: &Camera, window_size: Dimens<u32>, ) { - gl_shader_program.set_uniform_matrix_4fv(cstr!("model"), &transform.as_matrix()); + gl_shader_program.set_uniform_matrix_4fv( + cstr!("model"), + &create_transformation_matrix(transformation), + ); let view = create_view(camera); @@ -622,3 +629,20 @@ struct IndexInfo _buffer: Buffer<u32>, cnt: u32, } + +#[derive(Debug)] +struct Transformation +{ + position: Vec3<f32>, + scale: Vec3<f32>, +} + +fn create_transformation_matrix(transformation: Transformation) -> Matrix<f32, 4, 4> +{ + let mut matrix = Matrix::new_identity(); + + matrix.translate(&transformation.position); + matrix.scale(&transformation.scale); + + matrix +} diff --git a/engine/src/transform.rs b/engine/src/transform.rs index ad67d4c..5e5e296 100644 --- a/engine/src/transform.rs +++ b/engine/src/transform.rs @@ -1,35 +1,42 @@ use ecs::Component; -use crate::matrix::Matrix; use crate::vector::Vec3; -#[derive(Debug, Clone, Component)] -pub struct Transform +/// A position in 3D space. +#[derive(Debug, Default, Clone, Copy, Component)] +pub struct Position { pub position: Vec3<f32>, - pub scale: Vec3<f32>, } -impl Transform +impl From<Vec3<f32>> for Position { - pub(crate) fn as_matrix(&self) -> Matrix<f32, 4, 4> + fn from(position: Vec3<f32>) -> Self { - let mut matrix = Matrix::new_identity(); - - matrix.translate(&self.position); + Self { position } + } +} - matrix.scale(&self.scale); +/// Scaling of a 3D object. +#[derive(Debug, Clone, Copy, Component)] +pub struct Scale +{ + pub scale: Vec3<f32>, +} - matrix +impl From<Vec3<f32>> for Scale +{ + fn from(scale: Vec3<f32>) -> Self + { + Self { scale } } } -impl Default for Transform +impl Default for Scale { fn default() -> Self { Self { - position: Vec3::default(), scale: Vec3 { x: 1.0, y: 1.0, z: 1.0 }, } } |