From 7756fdab0522c52b81f3bc70e2458a3b683f0a20 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 24 May 2024 19:18:32 +0200 Subject: refactor(engine): make PointLight have a position --- engine/src/lighting.rs | 2 ++ engine/src/renderer/opengl.rs | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/engine/src/lighting.rs b/engine/src/lighting.rs index 398161f..81dc475 100644 --- a/engine/src/lighting.rs +++ b/engine/src/lighting.rs @@ -10,6 +10,7 @@ builder! { #[non_exhaustive] pub struct PointLight { + pub position: Vec3, pub diffuse: Color, pub specular: Color, pub attenuation_params: AttenuationParams, @@ -29,6 +30,7 @@ impl Default for PointLight fn default() -> Self { Self { + position: Vec3::default(), diffuse: Color { red: 0.5, green: 0.5, blue: 0.5 }, specular: Color { red: 1.0, green: 1.0, blue: 1.0 }, attenuation_params: AttenuationParams::default(), diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs index 3fe3388..8cafe0d 100644 --- a/engine/src/renderer/opengl.rs +++ b/engine/src/renderer/opengl.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::ffi::{c_void, CString}; +use std::ops::Deref; use std::process::abort; use cstr::cstr; @@ -100,7 +101,7 @@ fn render( Option, Transform, )>, - point_light_query: Query<(PointLight, Transform)>, + point_light_query: Query<(PointLight,)>, directional_lights: Query<(DirectionalLight,)>, camera_query: Query<(Camera,)>, window: Single, @@ -120,11 +121,9 @@ fn render( return; }; - // TODO: Maybe find a way to not clone here? Cloning here is needed since a transform - // can be in both the object query and the light source query let point_lights = point_light_query .iter() - .map(|(point_light, transform)| ((*point_light).clone(), (*transform).clone())) + .map(|(point_light,)| point_light) .collect::>(); let directional_lights = directional_lights.iter().collect::>(); @@ -361,15 +360,16 @@ fn apply_transformation_matrices( gl_shader_program.set_uniform_matrix_4fv(cstr!("projection"), &projection); } -fn apply_light( +fn apply_light( material: &Material, material_flags: &MaterialFlags, global_light: &GlobalLight, gl_shader_program: &mut GlShaderProgram, - point_lights: &[(PointLight, Transform)], + point_lights: &[PointLightHolder], directional_lights: &[&DirectionalLight], camera: &Camera, -) +) where + PointLightHolder: Deref, { debug_assert!( point_lights.len() < 64, @@ -404,19 +404,17 @@ fn apply_light( directional_lights.len() as i32, ); - for (point_light_index, (point_light, point_light_transform)) in - point_lights.iter().enumerate() - { + for (point_light_index, point_light) in point_lights.iter().enumerate() { gl_shader_program.set_uniform_vec_3fv( &create_light_uniform_name("point_lights", point_light_index, "position"), - &point_light_transform.position, + &point_light.position, ); set_light_phong_uniforms( gl_shader_program, "point_lights", point_light_index, - point_light, + &**point_light, ); set_light_attenuation_uniforms( -- cgit v1.2.3-18-g5258