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/src/renderer/opengl.rs | |
parent | e945f53395be9e0cd1e3a6894bc5e7fa08330298 (diff) |
refactor(engine): replace Transform with Position & Scale structs
Diffstat (limited to 'engine/src/renderer/opengl.rs')
-rw-r--r-- | engine/src/renderer/opengl.rs | 38 |
1 files changed, 31 insertions, 7 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 +} |