summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-04-24 20:43:18 +0200
committerHampusM <hampus@hampusmat.com>2024-04-24 20:43:18 +0200
commit33f7772ddddf2a1c2bfefc50ef39f123df8af3e4 (patch)
treec142293d3ebdcbbaf36220fb073873c95575de9d
parentd06be3fdaf66eb38e6c54fe2b7407d25692f8edc (diff)
feat(engine): add colors to materials
-rw-r--r--engine/light.glsl21
-rw-r--r--engine/src/material.rs55
-rw-r--r--engine/src/renderer/mod.rs34
3 files changed, 101 insertions, 9 deletions
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<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,
);