summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-05-23 19:37:47 +0200
committerHampusM <hampus@hampusmat.com>2024-05-23 20:09:54 +0200
commiteb270458de24c7346adc1ab0a4fe1c409a2008ae (patch)
tree166401361c9b3eb4f72e32cdb8fca8190f54daa1
parent42f4da1c9670e4e9829a822a50dc708794744ca1 (diff)
refactor(engine): seperate light properties in shader
-rw-r--r--engine/light.glsl35
-rw-r--r--engine/src/renderer/opengl.rs13
2 files changed, 32 insertions, 16 deletions
diff --git a/engine/light.glsl b/engine/light.glsl
index 6bc730a..9f51237 100644
--- a/engine/light.glsl
+++ b/engine/light.glsl
@@ -3,7 +3,8 @@
#ifndef LIGHT_GLSL
#define LIGHT_GLSL
-struct Material {
+struct Material
+{
vec3 ambient;
vec3 diffuse;
vec3 specular;
@@ -13,15 +14,26 @@ struct Material {
float shininess;
};
-struct PointLight {
- vec3 position;
+struct LightPhong
+{
vec3 diffuse;
vec3 specular;
+};
+
+struct AttenuationProperties
+{
float constant;
float linear;
float quadratic;
};
+struct PointLight
+{
+ LightPhong phong;
+ vec3 position;
+ AttenuationProperties attenuation_props;
+};
+
vec3 calc_ambient_light(in Material material, in vec2 texture_coords)
{
return vec3(texture(material.ambient_map, texture_coords)) * material.ambient;
@@ -29,7 +41,7 @@ vec3 calc_ambient_light(in Material material, in vec2 texture_coords)
vec3 calc_diffuse_light(
in Material material,
- in PointLight light,
+ in LightPhong light_phong,
in vec3 light_dir,
in vec3 norm,
in vec2 texture_coords
@@ -37,14 +49,14 @@ vec3 calc_diffuse_light(
{
float diff = max(dot(norm, light_dir), 0.0);
- return light.diffuse * (
+ return light_phong.diffuse * (
diff * (vec3(texture(material.diffuse_map, texture_coords)) * material.diffuse)
);
}
vec3 calc_specular_light(
in Material material,
- in PointLight light,
+ in LightPhong light_phong,
in vec3 light_dir,
in vec3 norm,
in vec3 view_pos,
@@ -59,7 +71,7 @@ vec3 calc_specular_light(
float spec =
pow(max(dot(view_direction, reflect_direction), 0.0), material.shininess);
- return light.specular * (
+ return light_phong.specular * (
spec * (vec3(texture(material.specular_map, texture_coords)) * material.specular)
);
}
@@ -68,8 +80,9 @@ float calc_attenuation(in PointLight point_light, in vec3 position)
{
float light_distance = length(point_light.position - position);
- return 1.0 / (point_light.constant + point_light.linear
- * light_distance + point_light.quadratic
+ return 1.0 / (point_light.attenuation_props.constant
+ + point_light.attenuation_props.linear
+ * light_distance + point_light.attenuation_props.quadratic
* pow(light_distance, 2));
}
@@ -87,7 +100,7 @@ vec3 calc_point_light(
vec3 diffuse_light = calc_diffuse_light(
material,
- light,
+ light.phong,
light_direction,
norm,
texture_coords
@@ -95,7 +108,7 @@ vec3 calc_point_light(
vec3 specular_light = calc_specular_light(
material,
- light,
+ light.phong,
light_direction,
norm,
view_pos,
diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs
index c97d8dd..89f920c 100644
--- a/engine/src/renderer/opengl.rs
+++ b/engine/src/renderer/opengl.rs
@@ -434,27 +434,30 @@ fn set_point_light_uniforms(
);
gl_shader_program.set_uniform_vec_3fv(
- &create_point_light_uniform_name(point_light_index, "diffuse"),
+ &create_point_light_uniform_name(point_light_index, "phong.diffuse"),
&point_light.diffuse.clone().into(),
);
gl_shader_program.set_uniform_vec_3fv(
- &create_point_light_uniform_name(point_light_index, "specular"),
+ &create_point_light_uniform_name(point_light_index, "phong.specular"),
&point_light.specular.clone().into(),
);
gl_shader_program.set_uniform_1fv(
- &create_point_light_uniform_name(point_light_index, "constant"),
+ &create_point_light_uniform_name(point_light_index, "attenuation_props.constant"),
point_light.attenuation_params.constant,
);
gl_shader_program.set_uniform_1fv(
- &create_point_light_uniform_name(point_light_index, "linear"),
+ &create_point_light_uniform_name(point_light_index, "attenuation_props.linear"),
point_light.attenuation_params.linear,
);
gl_shader_program.set_uniform_1fv(
- &create_point_light_uniform_name(point_light_index, "quadratic"),
+ &create_point_light_uniform_name(
+ point_light_index,
+ "attenuation_props.quadratic",
+ ),
point_light.attenuation_params.quadratic,
);
}