From 3ff91ac64308141968fdcbf3b3c09a01b0c60c97 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 20 Jun 2024 20:25:07 +0200 Subject: refactor(engine): replace Transform with Position & Scale structs --- engine/src/renderer/opengl.rs | 38 +++++++++++++++++++++++++++++++------- engine/src/transform.rs | 33 ++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 20 deletions(-) (limited to 'engine/src') 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, - Transform, + Option, + Option, )>, 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, ) { - 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, cnt: u32, } + +#[derive(Debug)] +struct Transformation +{ + position: Vec3, + scale: Vec3, +} + +fn create_transformation_matrix(transformation: Transformation) -> Matrix +{ + 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, - pub scale: Vec3, } -impl Transform +impl From> for Position { - pub(crate) fn as_matrix(&self) -> Matrix + fn from(position: Vec3) -> 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, +} - matrix +impl From> for Scale +{ + fn from(scale: Vec3) -> 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 }, } } -- cgit v1.2.3-18-g5258