From f82ceb5587954802352e00ffacd229559a2ca4df Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 12 May 2024 19:02:35 +0200 Subject: feat(engine): add global material ambient color offsetting --- engine/src/lighting.rs | 22 +++++++++++++++++- engine/src/renderer/mod.rs | 7 ++++-- engine/src/util.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) (limited to 'engine') diff --git a/engine/src/lighting.rs b/engine/src/lighting.rs index e0608ef..6944ee5 100644 --- a/engine/src/lighting.rs +++ b/engine/src/lighting.rs @@ -1,6 +1,7 @@ -use ecs::Component; +use ecs::{Component, Sole}; use crate::color::Color; +use crate::util::builder; #[derive(Debug, Clone, Component)] pub struct LightSource @@ -21,3 +22,22 @@ impl Default for LightSource } } } + +builder! { +/// Global light properties. +#[builder(name = GlobalLightBuilder, derives = (Debug, Clone, Default))] +#[derive(Debug, Clone, Default, Sole)] +#[non_exhaustive] +pub struct GlobalLight +{ + pub ambient_offset: Color, +} +} + +impl GlobalLight +{ + pub fn builder() -> GlobalLightBuilder + { + GlobalLightBuilder::default() + } +} diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index e2a567a..f9082fc 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -12,7 +12,7 @@ use crate::camera::Camera; use crate::color::Color; use crate::data_types::dimens::Dimens; use crate::event::{Present as PresentEvent, Start as StartEvent}; -use crate::lighting::LightSource; +use crate::lighting::{GlobalLight, LightSource}; use crate::material::Material; use crate::matrix::Matrix; use crate::mesh::Mesh; @@ -95,6 +95,7 @@ fn render( light_source_query: Query<(LightSource, Transform)>, camera_query: Query<(Camera,)>, window: Single, + global_light: Single, mut gl_objects: Local, ) { @@ -138,6 +139,7 @@ fn render( apply_light( &material, + &global_light, shader_program, light_source .as_ref() @@ -344,6 +346,7 @@ fn apply_transformation_matrices( fn apply_light( material: &Material, + global_light: &GlobalLight, gl_shader_program: &mut GlShaderProgram, light_source: Option<(&LightSource, &Transform)>, camera: &Camera, @@ -385,7 +388,7 @@ fn apply_light( gl_shader_program.set_uniform_vec_3fv( cstr!("material.ambient"), - &material.ambient().clone().into(), + &(material.ambient().clone() + global_light.ambient_offset.clone()).into(), ); gl_shader_program.set_uniform_vec_3fv( diff --git a/engine/src/util.rs b/engine/src/util.rs index b1a98ca..a7d5794 100644 --- a/engine/src/util.rs +++ b/engine/src/util.rs @@ -22,3 +22,60 @@ macro_rules! or { } pub(crate) use or; + +macro_rules! builder { + ( + $(#[doc = $doc: literal])* + #[builder( + name = $builder_name: ident + $(, derives = ($($builder_derive: ident),+))? + )] + $(#[$attr: meta])* + $visibility: vis struct $name: ident + { + $( + $(#[$field_attr: meta])* + $field_visibility: vis $field: ident: $field_type: ty, + )* + } + ) => { + $(#[doc = $doc])* + $(#[$attr])* + $visibility struct $name + { + $( + $(#[$field_attr])* + $field_visibility $field: $field_type, + )* + } + + $(#[derive($($builder_derive),+)])? + $visibility struct $builder_name + { + $( + $field: $field_type, + )* + } + + impl $builder_name + { + $( + $visibility fn $field(mut self, $field: $field_type) -> Self + { + self.$field = $field; + self + } + )* + + $visibility fn build(self) -> $name { + $name { + $( + $field: self.$field, + )* + } + } + } + }; +} + +pub(crate) use builder; -- cgit v1.2.3-18-g5258