From 33f7772ddddf2a1c2bfefc50ef39f123df8af3e4 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 24 Apr 2024 20:43:18 +0200 Subject: feat(engine): add colors to materials --- engine/light.glsl | 21 +++++++++++++----- engine/src/material.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++ engine/src/renderer/mod.rs | 34 +++++++++++++++++++++++++--- 3 files changed, 101 insertions(+), 9 deletions(-) (limited to 'engine') diff --git a/engine/light.glsl b/engine/light.glsl index 3ba5ba0..46105e9 100644 --- a/engine/light.glsl +++ b/engine/light.glsl @@ -4,9 +4,12 @@ #define LIGHT_GLSL struct Material { - sampler2D ambient; - sampler2D diffuse; - sampler2D specular; + vec3 ambient; + vec3 diffuse; + vec3 specular; + sampler2D ambient_map; + sampler2D diffuse_map; + sampler2D specular_map; float shininess; }; @@ -23,14 +26,18 @@ uniform Light light; vec3 calc_ambient_light(in vec2 texture_coords) { - return light.ambient * vec3(texture(material.ambient, texture_coords)); + return light.ambient * ( + vec3(texture(material.ambient_map, texture_coords)) * material.ambient + ); } vec3 calc_diffuse_light(in vec3 light_dir, in vec3 norm, in vec2 texture_coords) { float diff = max(dot(norm, light_dir), 0.0); - return light.diffuse * (diff * vec3(texture(material.diffuse, texture_coords))); + return light.diffuse * ( + diff * (vec3(texture(material.diffuse_map, texture_coords)) * material.diffuse) + ); } vec3 calc_specular_light( @@ -48,7 +55,9 @@ vec3 calc_specular_light( float spec = pow(max(dot(view_direction, reflect_direction), 0.0), material.shininess); - return light.specular * (spec * vec3(texture(material.specular, texture_coords))); + return light.specular * ( + spec * (vec3(texture(material.specular_map, texture_coords)) * material.specular) + ); } #endif 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>, + diffuse: Option>, + specular: Option>, ambient_map: TextureId, diffuse_map: TextureId, specular_map: TextureId, @@ -13,6 +17,24 @@ pub struct Material impl Material { + #[must_use] + pub fn ambient(&self) -> Option<&Color> + { + self.ambient.as_ref() + } + + #[must_use] + pub fn diffuse(&self) -> Option<&Color> + { + self.diffuse.as_ref() + } + + #[must_use] + pub fn specular(&self) -> Option<&Color> + { + self.specular.as_ref() + } + #[must_use] pub fn ambient_map(&self) -> &TextureId { @@ -42,6 +64,9 @@ impl Material #[derive(Debug, Clone)] pub struct Builder { + ambient: Option>, + diffuse: Option>, + specular: Option>, ambient_map: Option, diffuse_map: Option, specular_map: Option, @@ -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, @@ -61,6 +89,30 @@ impl Builder } } + #[must_use] + pub fn ambient(mut self, ambient: Color) -> Self + { + self.ambient = Some(ambient); + + self + } + + #[must_use] + pub fn diffuse(mut self, diffuse: Color) -> Self + { + self.diffuse = Some(diffuse); + + self + } + + #[must_use] + pub fn specular(mut self, specular: Color) -> Self + { + self.specular = Some(specular); + + self + } + #[must_use] pub fn ambient_map(mut self, ambient_map: TextureId) -> Self { @@ -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, ); -- cgit v1.2.3-18-g5258