summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-05-23 20:22:08 +0200
committerHampusM <hampus@hampusmat.com>2024-05-23 20:53:25 +0200
commit638c775707240d365432ed910a88a3cbf545a130 (patch)
treef8656d0ff8c7074764fab353c81b02f449c6e3fe /engine
parenteb270458de24c7346adc1ab0a4fe1c409a2008ae (diff)
refactor(engine): pass vertex data as struct to calc_point_light
Diffstat (limited to 'engine')
-rw-r--r--engine/fragment.glsl13
-rw-r--r--engine/light.glsl18
-rw-r--r--engine/vertex_data.glsl11
3 files changed, 29 insertions, 13 deletions
diff --git a/engine/fragment.glsl b/engine/fragment.glsl
index 6e852f3..afeef25 100644
--- a/engine/fragment.glsl
+++ b/engine/fragment.glsl
@@ -1,6 +1,7 @@
#version 330 core
#preinclude "light.glsl"
+#preinclude "vertex_data.glsl"
#define MAX_POINT_LIGHT_CNT 64
@@ -19,16 +20,20 @@ uniform int point_light_cnt;
void main()
{
+ VertexData vertex_data;
+
+ vertex_data.texture_coords = in_texture_coords;
+ vertex_data.world_space_pos = in_frag_pos;
+ vertex_data.world_space_normal = in_normal;
+
vec3 ambient_light = calc_ambient_light(material, in_texture_coords);
vec3 point_light_sum = vec3(0.0, 0.0, 0.0);
- for (int pl_index= 0; pl_index < point_light_cnt; pl_index++) {
+ for (int pl_index = 0; pl_index < point_light_cnt; pl_index++) {
point_light_sum += calc_point_light(
point_lights[pl_index],
- in_frag_pos,
- in_normal,
- in_texture_coords,
+ vertex_data,
view_pos,
material
);
diff --git a/engine/light.glsl b/engine/light.glsl
index 9f51237..92d3ec3 100644
--- a/engine/light.glsl
+++ b/engine/light.glsl
@@ -3,6 +3,8 @@
#ifndef LIGHT_GLSL
#define LIGHT_GLSL
+#preinclude "vertex_data.glsl"
+
struct Material
{
vec3 ambient;
@@ -88,22 +90,20 @@ float calc_attenuation(in PointLight point_light, in vec3 position)
vec3 calc_point_light(
in PointLight light,
- in vec3 frag_pos,
- in vec3 normal,
- in vec2 texture_coords,
+ in VertexData vertex_data,
in vec3 view_pos,
in Material material
)
{
- vec3 light_direction = normalize(light.position - frag_pos);
- vec3 norm = normalize(normal);
+ vec3 light_direction = normalize(light.position - vertex_data.world_space_pos);
+ vec3 norm = normalize(vertex_data.world_space_normal);
vec3 diffuse_light = calc_diffuse_light(
material,
light.phong,
light_direction,
norm,
- texture_coords
+ vertex_data.texture_coords
);
vec3 specular_light = calc_specular_light(
@@ -112,11 +112,11 @@ vec3 calc_point_light(
light_direction,
norm,
view_pos,
- frag_pos,
- texture_coords
+ vertex_data.world_space_pos,
+ vertex_data.texture_coords
);
- float attenuation = calc_attenuation(light, frag_pos);
+ float attenuation = calc_attenuation(light, vertex_data.world_space_pos);
diffuse_light *= attenuation;
specular_light *= attenuation;
diff --git a/engine/vertex_data.glsl b/engine/vertex_data.glsl
new file mode 100644
index 0000000..486d445
--- /dev/null
+++ b/engine/vertex_data.glsl
@@ -0,0 +1,11 @@
+#ifndef VERTEX_DATA_GLSL
+#define VERTEX_DATA_GLSL
+
+struct VertexData
+{
+ vec2 texture_coords;
+ vec3 world_space_pos;
+ vec3 world_space_normal;
+};
+
+#endif