diff options
Diffstat (limited to 'engine/src')
-rw-r--r-- | engine/src/material.rs | 55 | ||||
-rw-r--r-- | engine/src/renderer/mod.rs | 34 |
2 files changed, 86 insertions, 3 deletions
diff --git a/engine/src/material.rs b/engine/src/material.rs index 7d8c5db..c292af4 100644 --- a/engine/src/material.rs +++ b/engine/src/material.rs @@ -1,10 +1,14 @@ use ecs::Component; +use crate::color::Color; use crate::texture::Id as TextureId; #[derive(Debug, Clone, Component)] pub struct Material { + ambient: Option<Color<f32>>, + diffuse: Option<Color<f32>>, + specular: Option<Color<f32>>, ambient_map: TextureId, diffuse_map: TextureId, specular_map: TextureId, @@ -14,6 +18,24 @@ pub struct Material impl Material { #[must_use] + pub fn ambient(&self) -> Option<&Color<f32>> + { + self.ambient.as_ref() + } + + #[must_use] + pub fn diffuse(&self) -> Option<&Color<f32>> + { + self.diffuse.as_ref() + } + + #[must_use] + pub fn specular(&self) -> Option<&Color<f32>> + { + self.specular.as_ref() + } + + #[must_use] pub fn ambient_map(&self) -> &TextureId { &self.ambient_map @@ -42,6 +64,9 @@ impl Material #[derive(Debug, Clone)] pub struct Builder { + ambient: Option<Color<f32>>, + diffuse: Option<Color<f32>>, + specular: Option<Color<f32>>, ambient_map: Option<TextureId>, diffuse_map: Option<TextureId>, specular_map: Option<TextureId>, @@ -54,6 +79,9 @@ impl Builder pub fn new() -> Self { Self { + ambient: None, + diffuse: None, + specular: None, ambient_map: None, diffuse_map: None, specular_map: None, @@ -62,6 +90,30 @@ impl Builder } #[must_use] + pub fn ambient(mut self, ambient: Color<f32>) -> Self + { + self.ambient = Some(ambient); + + self + } + + #[must_use] + pub fn diffuse(mut self, diffuse: Color<f32>) -> Self + { + self.diffuse = Some(diffuse); + + self + } + + #[must_use] + pub fn specular(mut self, specular: Color<f32>) -> Self + { + self.specular = Some(specular); + + self + } + + #[must_use] pub fn ambient_map(mut self, ambient_map: TextureId) -> Self { self.ambient_map = Some(ambient_map); @@ -101,6 +153,9 @@ impl Builder pub fn build(&self) -> Material { Material { + ambient: self.ambient.clone(), + diffuse: self.diffuse.clone(), + specular: self.specular.clone(), ambient_map: self.ambient_map.unwrap(), diffuse_map: self.diffuse_map.unwrap(), specular_map: self.specular_map.unwrap(), diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 5125da8..af91257 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -383,21 +383,49 @@ fn apply_light( .into(), ); + gl_shader_program.set_uniform_vec_3fv( + cstr!("material.ambient"), + &material + .ambient() + .cloned() + .unwrap_or(Color { red: 1.0, green: 1.0, blue: 1.0 }) + .into(), + ); + + gl_shader_program.set_uniform_vec_3fv( + cstr!("material.diffuse"), + &material + .diffuse() + .cloned() + .unwrap_or(Color { red: 1.0, green: 1.0, blue: 1.0 }) + .into(), + ); + + #[allow(clippy::cast_possible_wrap)] + gl_shader_program.set_uniform_vec_3fv( + cstr!("material.specular"), + &material + .specular() + .cloned() + .unwrap_or(Color { red: 1.0, green: 1.0, blue: 1.0 }) + .into(), + ); + #[allow(clippy::cast_possible_wrap)] gl_shader_program.set_uniform_1i( - cstr!("material.ambient"), + cstr!("material.ambient_map"), material.ambient_map().into_inner() as i32, ); #[allow(clippy::cast_possible_wrap)] gl_shader_program.set_uniform_1i( - cstr!("material.diffuse"), + cstr!("material.diffuse_map"), material.diffuse_map().into_inner() as i32, ); #[allow(clippy::cast_possible_wrap)] gl_shader_program.set_uniform_1i( - cstr!("material.specular"), + cstr!("material.specular_map"), material.specular_map().into_inner() as i32, ); |