From ad67712586cfcfe5236efda93b9e15aefdc9fcf5 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 21 Apr 2024 16:03:47 +0200 Subject: refactor(engine): remove LightSource position field --- engine/src/renderer/mod.rs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'engine/src/renderer') diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index afed816..cb7066f 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -92,7 +92,7 @@ fn initialize(window: Single) fn render( query: Query<(Mesh, TextureMap, ShaderProgram, Material, Transform)>, - light_source_query: Query<(LightSource,)>, + light_source_query: Query<(LightSource, Transform)>, camera_query: Query<(Camera,)>, window: Single, mut gl_objects: Local, @@ -110,10 +110,7 @@ fn render( return; }; - let light_source = light_source_query - .iter() - .next() - .map(|(light_source,)| light_source); + let light_source = light_source_query.iter().next(); let GlObjects { shader_programs: gl_shader_programs, @@ -134,7 +131,16 @@ fn render( window.size().expect("Failed to get window size"), ); - apply_light(&material, shader_program, light_source.as_deref(), &camera); + apply_light( + &material, + shader_program, + light_source + .as_ref() + .map(|(light_source, light_source_transform)| { + (&**light_source, &**light_source_transform) + }), + &camera, + ); for texture in &texture_list.list { let gl_texture = gl_textures @@ -334,19 +340,21 @@ fn apply_transformation_matrices( fn apply_light( material: &Material, gl_shader_program: &mut GlShaderProgram, - light_source: Option<&LightSource>, + light_source: Option<(&LightSource, &Transform)>, camera: &Camera, ) { gl_shader_program.set_uniform_vec_3fv( cstr!("light.position"), - &light_source.map_or_else(Vec3::default, |light_source| light_source.position), + &light_source.map_or_else(Vec3::default, |(_, light_source_transform)| { + light_source_transform.position + }), ); gl_shader_program.set_uniform_vec_3fv( cstr!("light.ambient"), &light_source - .map_or(Color::WHITE_F32, |light_source| { + .map_or(Color::WHITE_F32, |(light_source, _)| { light_source.ambient.clone() }) .into(), @@ -355,7 +363,7 @@ fn apply_light( gl_shader_program.set_uniform_vec_3fv( cstr!("light.diffuse"), &light_source - .map_or(Color::WHITE_F32, |light_source| { + .map_or(Color::WHITE_F32, |(light_source, _)| { light_source.diffuse.clone() }) .into(), @@ -364,7 +372,7 @@ fn apply_light( gl_shader_program.set_uniform_vec_3fv( cstr!("light.specular"), &light_source - .map_or(Color::WHITE_F32, |light_source| { + .map_or(Color::WHITE_F32, |(light_source, _)| { light_source.specular.clone() }) .into(), -- cgit v1.2.3-18-g5258