summaryrefslogtreecommitdiff
path: root/advanced_lighting/2.gamma_correction/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'advanced_lighting/2.gamma_correction/shaders')
-rw-r--r--advanced_lighting/2.gamma_correction/shaders/blinn_phong.fs58
-rw-r--r--advanced_lighting/2.gamma_correction/shaders/blinn_phong.vs23
-rw-r--r--advanced_lighting/2.gamma_correction/shaders/light.fs9
-rw-r--r--advanced_lighting/2.gamma_correction/shaders/light.vs12
4 files changed, 102 insertions, 0 deletions
diff --git a/advanced_lighting/2.gamma_correction/shaders/blinn_phong.fs b/advanced_lighting/2.gamma_correction/shaders/blinn_phong.fs
new file mode 100644
index 0000000..35e701c
--- /dev/null
+++ b/advanced_lighting/2.gamma_correction/shaders/blinn_phong.fs
@@ -0,0 +1,58 @@
+#version 330 core
+
+in VS_OUT {
+ vec3 frag_pos;
+ vec3 normal;
+ vec2 tex_coords;
+} fs_in;
+
+out vec4 frag_color;
+
+uniform sampler2D texture0;
+uniform vec3 light_positions[4];
+uniform vec3 light_colors[4];
+uniform vec3 view_pos;
+uniform bool gamma_correction;
+
+vec3
+blinn_phong(vec3 light_dir, vec3 view_dir, vec3 normal,
+ vec3 light_pos, vec3 light_color)
+{
+ float diff = max(dot(normal, light_dir), 0.0);
+ vec3 diffuse = diff*light_color;
+
+ vec3 halfway_dir = normalize(light_dir+view_dir);
+ float spec = pow(max(dot(normal, halfway_dir), 0.0), 16.0);
+ vec3 specular = spec*light_color;
+
+ float distance = length(light_pos-fs_in.frag_pos);
+ float attenuation = 1.0/(gamma_correction ? distance*distance : distance);
+
+ return((diffuse+specular)*attenuation);
+}
+
+void
+main(void)
+{
+ vec3 view_dir = normalize(view_pos-fs_in.frag_pos);
+ vec3 normal = normalize(fs_in.normal);
+ vec3 texture_color = texture(texture0, fs_in.tex_coords).rgb;
+
+ vec3 lighting = vec3(0.0);
+ for (int i = 0; i < 4; i++)
+ {
+ vec3 light_pos = light_positions[i];
+ vec3 light_color = light_colors[i];
+ vec3 light_dir = normalize(light_pos-fs_in.frag_pos);
+ lighting += blinn_phong(light_dir, view_dir, normal,
+ light_pos, light_color);
+ }
+
+ texture_color *= lighting;
+
+ float gamma = 2.2;
+ if (gamma_correction)
+ texture_color = pow(texture_color, vec3(1.0/gamma));
+
+ frag_color = vec4(texture_color, 1.0);
+}
diff --git a/advanced_lighting/2.gamma_correction/shaders/blinn_phong.vs b/advanced_lighting/2.gamma_correction/shaders/blinn_phong.vs
new file mode 100644
index 0000000..4d1b792
--- /dev/null
+++ b/advanced_lighting/2.gamma_correction/shaders/blinn_phong.vs
@@ -0,0 +1,23 @@
+#version 330 core
+layout(location = 0) in vec3 apos;
+layout(location = 1) in vec3 anormal;
+layout(location = 2) in vec2 atex_coords;
+
+out VS_OUT {
+ vec3 frag_pos;
+ vec3 normal;
+ vec2 tex_coords;
+} vs_out;
+
+uniform mat4 projection;
+uniform mat4 view;
+uniform mat4 model;
+
+void
+main(void)
+{
+ vs_out.frag_pos = vec3(model*vec4(apos, 1.0));
+ vs_out.normal = mat3(transpose(inverse(model)))*anormal;
+ vs_out.tex_coords = atex_coords;
+ gl_Position = projection*view*model*vec4(apos, 1.0);
+}
diff --git a/advanced_lighting/2.gamma_correction/shaders/light.fs b/advanced_lighting/2.gamma_correction/shaders/light.fs
new file mode 100644
index 0000000..bd00d82
--- /dev/null
+++ b/advanced_lighting/2.gamma_correction/shaders/light.fs
@@ -0,0 +1,9 @@
+#version 330 core
+
+out vec4 frag_color;
+
+void
+main(void)
+{
+ frag_color = vec4(1.0);
+}
diff --git a/advanced_lighting/2.gamma_correction/shaders/light.vs b/advanced_lighting/2.gamma_correction/shaders/light.vs
new file mode 100644
index 0000000..ade669b
--- /dev/null
+++ b/advanced_lighting/2.gamma_correction/shaders/light.vs
@@ -0,0 +1,12 @@
+#version 330 core
+layout(location = 0) in vec3 apos;
+
+uniform mat4 projection;
+uniform mat4 view;
+uniform mat4 model;
+
+void
+main(void)
+{
+ gl_Position = projection*view*model*vec4(apos, 1.0);
+}