From d4b61dd34b06119e87c8932ab7718d432dbc6a4f Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 22 May 2024 20:39:26 +0200 Subject: feat(engine): add point light attenuation --- engine/src/lighting.rs | 23 +++++++++++++++++++++++ engine/src/renderer/mod.rs | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+) (limited to 'engine/src') diff --git a/engine/src/lighting.rs b/engine/src/lighting.rs index bfc2d9b..ffa2645 100644 --- a/engine/src/lighting.rs +++ b/engine/src/lighting.rs @@ -11,6 +11,7 @@ pub struct PointLight { pub diffuse: Color, pub specular: Color, + pub attenuation_params: AttenuationParams, } } @@ -29,6 +30,7 @@ impl Default for PointLight Self { 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(), } } } @@ -41,6 +43,27 @@ impl Default for PointLightBuilder } } +/// Parameters for light [attenuation](https://en.wikipedia.org/wiki/Attenuation). +#[derive(Debug, Clone)] +pub struct AttenuationParams +{ + pub constant: f32, + pub linear: f32, + pub quadratic: f32, +} + +impl Default for AttenuationParams +{ + fn default() -> Self + { + Self { + constant: 1.0, + linear: 0.0, + quadratic: 0.0, + } + } +} + builder! { /// Global light properties. #[builder(name = GlobalLightBuilder, derives = (Debug, Clone, Default))] diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index e736165..ad587b5 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -389,6 +389,27 @@ fn apply_light( .into(), ); + gl_shader_program.set_uniform_1fv( + cstr!("light.constant"), + point_light.map_or(1.0, |(light_source, _)| { + light_source.attenuation_params.constant + }), + ); + + gl_shader_program.set_uniform_1fv( + cstr!("light.linear"), + point_light.map_or(0.0, |(light_source, _)| { + light_source.attenuation_params.linear + }), + ); + + gl_shader_program.set_uniform_1fv( + cstr!("light.quadratic"), + point_light.map_or(0.0, |(light_source, _)| { + light_source.attenuation_params.quadratic + }), + ); + gl_shader_program.set_uniform_vec_3fv( cstr!("material.ambient"), &if material_flags.use_ambient_color { -- cgit v1.2.3-18-g5258