summaryrefslogtreecommitdiff
path: root/advanced_lighting/7.bloom/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'advanced_lighting/7.bloom/shaders')
-rw-r--r--advanced_lighting/7.bloom/shaders/bloom.frag45
-rw-r--r--advanced_lighting/7.bloom/shaders/bloom.vert24
-rw-r--r--advanced_lighting/7.bloom/shaders/blur.frag29
-rw-r--r--advanced_lighting/7.bloom/shaders/blur.vert14
-rw-r--r--advanced_lighting/7.bloom/shaders/final.frag25
-rw-r--r--advanced_lighting/7.bloom/shaders/final.vert14
-rw-r--r--advanced_lighting/7.bloom/shaders/light.frag22
-rw-r--r--advanced_lighting/7.bloom/shaders/test_blur.frag28
-rw-r--r--advanced_lighting/7.bloom/shaders/test_blur.vert14
9 files changed, 215 insertions, 0 deletions
diff --git a/advanced_lighting/7.bloom/shaders/bloom.frag b/advanced_lighting/7.bloom/shaders/bloom.frag
new file mode 100644
index 0000000..88decf8
--- /dev/null
+++ b/advanced_lighting/7.bloom/shaders/bloom.frag
@@ -0,0 +1,45 @@
+#version 330 core
+
+in VS_OUT {
+ vec3 position;
+ vec3 normal;
+ vec2 tex_coords;
+} vs_out;
+
+layout(location = 0) out vec4 frag_color;
+layout(location = 1) out vec4 bright_color;
+
+struct light_t {
+ vec3 position;
+ vec3 color;
+};
+
+uniform light_t lights[3];
+uniform sampler2D diffuse_texture;
+uniform vec3 view_pos;
+
+const int light_count = 3;
+
+void main(void)
+{
+ vec3 color = texture(diffuse_texture, vs_out.tex_coords).rgb;
+ vec3 ambient = 0.01 * color;
+ vec3 lighting = vec3(0.0);
+ vec3 view_dir = normalize(view_pos - vs_out.position);
+ for (int i = 0; i < light_count; i++) {
+ vec3 light_dir = normalize(lights[i].position - vs_out.position);
+ float diff = max(dot(light_dir, vs_out.normal), 0.0);
+ vec3 result = lights[i].color * diff * color;
+ float distance = length(vs_out.position - lights[i].position);
+ result *= 1.0 / (distance * distance);
+ lighting += result;
+ }
+ vec3 result = ambient + lighting;
+ float brightness = dot(result, vec3(0.2126, 0.7152, 0.0722));
+ if (brightness > 1.0) {
+ bright_color = vec4(result, 1.0);
+ } else {
+ bright_color = vec4(0.0, 0.0, 0.0, 1.0);
+ }
+ frag_color = vec4(result, 1.0);
+}
diff --git a/advanced_lighting/7.bloom/shaders/bloom.vert b/advanced_lighting/7.bloom/shaders/bloom.vert
new file mode 100644
index 0000000..0c9dc02
--- /dev/null
+++ b/advanced_lighting/7.bloom/shaders/bloom.vert
@@ -0,0 +1,24 @@
+#version 330 core
+
+layout (location = 0) in vec3 position;
+layout (location = 1) in vec3 normal;
+layout (location = 2) in vec2 tex_coords;
+
+out VS_OUT {
+ vec3 position;
+ vec3 normal;
+ vec2 tex_coords;
+} vs_out;
+
+uniform mat4 projection;
+uniform mat4 view;
+uniform mat4 model;
+
+void main()
+{
+ vs_out.position = vec3(model * vec4(position, 1.0));
+ mat3 normal_matrix = transpose(inverse(mat3(model)));
+ vs_out.normal = normalize(normal_matrix * normal);
+ vs_out.tex_coords = tex_coords;
+ gl_Position = projection * view * model * vec4(position, 1.0);
+}
diff --git a/advanced_lighting/7.bloom/shaders/blur.frag b/advanced_lighting/7.bloom/shaders/blur.frag
new file mode 100644
index 0000000..67b7e43
--- /dev/null
+++ b/advanced_lighting/7.bloom/shaders/blur.frag
@@ -0,0 +1,29 @@
+#version 330 core
+
+in VS_OUT {
+ vec2 tex_coords;
+} vs_out;
+
+out vec4 frag_color;
+
+uniform sampler2D image;
+uniform bool horizontal;
+uniform float weights[5] = float[](0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162);
+
+void main(void)
+{
+ vec2 tex_offset = 1.0 / textureSize(image, 0);
+ vec3 result = texture(image, vs_out.tex_coords).rgb * weights[0];
+ if (horizontal) {
+ for (int i = 1; i < 5; i++) {
+ result += texture(image, vs_out.tex_coords + vec2(tex_offset.x * i, 0.0)).rgb * weights[i];
+ result += texture(image, vs_out.tex_coords - vec2(tex_offset.x * i, 0.0)).rgb * weights[i];
+ }
+ } else {
+ for (int i = 1; i < 5; i++) {
+ result += texture(image, vs_out.tex_coords + vec2(0.0, tex_offset.y * i)).rgb * weights[i];
+ result += texture(image, vs_out.tex_coords - vec2(0.0, tex_offset.y * i)).rgb * weights[i];
+ }
+ }
+ frag_color = vec4(result, 1.0);
+}
diff --git a/advanced_lighting/7.bloom/shaders/blur.vert b/advanced_lighting/7.bloom/shaders/blur.vert
new file mode 100644
index 0000000..e4ced24
--- /dev/null
+++ b/advanced_lighting/7.bloom/shaders/blur.vert
@@ -0,0 +1,14 @@
+#version 330 core
+
+layout (location = 0) in vec3 position;
+layout (location = 2) in vec2 tex_coords;
+
+out VS_OUT {
+ vec2 tex_coords;
+} vs_out;
+
+void main(void)
+{
+ vs_out.tex_coords = tex_coords;
+ gl_Position = vec4(position, 1.0);
+}
diff --git a/advanced_lighting/7.bloom/shaders/final.frag b/advanced_lighting/7.bloom/shaders/final.frag
new file mode 100644
index 0000000..fe9f5d7
--- /dev/null
+++ b/advanced_lighting/7.bloom/shaders/final.frag
@@ -0,0 +1,25 @@
+#version 330 core
+
+in VS_OUT {
+ vec2 tex_coords;
+} vs_out;
+
+out vec4 frag_color;
+
+uniform sampler2D scene;
+uniform sampler2D blur;
+uniform bool bloom;
+uniform float exposure;
+
+void main()
+{
+ const float gamma = 2.2;
+ vec3 scene_color = texture(scene, vs_out.tex_coords).rgb;
+ vec3 blur_color = texture(blur, vs_out.tex_coords).rgb;
+ if (bloom) {
+ scene_color += blur_color;
+ }
+ vec3 result = vec3(1.0) - exp(-scene_color * exposure);
+ result = pow(result, vec3(1.0 / gamma));
+ frag_color = vec4(result, 1.0);
+}
diff --git a/advanced_lighting/7.bloom/shaders/final.vert b/advanced_lighting/7.bloom/shaders/final.vert
new file mode 100644
index 0000000..f6a4e38
--- /dev/null
+++ b/advanced_lighting/7.bloom/shaders/final.vert
@@ -0,0 +1,14 @@
+#version 330 core
+
+layout (location = 0) in vec3 position;
+layout (location = 2) in vec2 tex_coords;
+
+out VS_OUT {
+ vec2 tex_coords;
+} vs_out;
+
+void main()
+{
+ vs_out.tex_coords = tex_coords;
+ gl_Position = vec4(position, 1.0);
+}
diff --git a/advanced_lighting/7.bloom/shaders/light.frag b/advanced_lighting/7.bloom/shaders/light.frag
new file mode 100644
index 0000000..72a2874
--- /dev/null
+++ b/advanced_lighting/7.bloom/shaders/light.frag
@@ -0,0 +1,22 @@
+#version 330 core
+
+in VS_OUT {
+ vec3 position;
+ vec3 normal;
+ vec2 tex_coords;
+} vs_out;
+
+layout(location = 0) out vec4 frag_color;
+layout(location = 1) out vec4 bright_color;
+
+uniform vec3 light_color;
+
+void main()
+{
+ frag_color = vec4(light_color, 1.0);
+ float brightness = dot(vec3(frag_color), vec3(0.2126, 0.7152, 0.0722));
+ if (brightness > 1.0)
+ bright_color = vec4(vec3(frag_color), 1.0);
+ else
+ bright_color = vec4(0.0, 0.0, 0.0, 1.0);
+}
diff --git a/advanced_lighting/7.bloom/shaders/test_blur.frag b/advanced_lighting/7.bloom/shaders/test_blur.frag
new file mode 100644
index 0000000..8d7ee9a
--- /dev/null
+++ b/advanced_lighting/7.bloom/shaders/test_blur.frag
@@ -0,0 +1,28 @@
+#version 330 core
+
+in vert_t {
+ vec2 tex_coords;
+} vert;
+
+out vec4 frag_color;
+
+uniform sampler2D colorbuffer;
+
+uniform float weights[5] = float[](0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162);
+
+void main()
+{
+ vec2 texture_offset = 1.0 / textureSize(colorbuffer, 0);
+ vec3 result = texture(colorbuffer, vert.tex_coords).rgb * weights[0];
+ for (int x = 1; x < weights.length(); x++) {
+ vec2 offset = vec2(texture_offset.x * x, 0.0f);
+ result += texture(colorbuffer, vert.tex_coords + offset).rgb * weights[x];
+ result += texture(colorbuffer, vert.tex_coords - offset).rgb * weights[x];
+ }
+ for (int y = 1; y < weights.length(); y++) {
+ vec2 offset = vec2(0.0f, texture_offset.y * y);
+ result += texture(colorbuffer, vert.tex_coords + offset).rgb * weights[y];
+ result += texture(colorbuffer, vert.tex_coords - offset).rgb * weights[y];
+ }
+ frag_color = vec4(result, 1.0);
+}
diff --git a/advanced_lighting/7.bloom/shaders/test_blur.vert b/advanced_lighting/7.bloom/shaders/test_blur.vert
new file mode 100644
index 0000000..eed3be3
--- /dev/null
+++ b/advanced_lighting/7.bloom/shaders/test_blur.vert
@@ -0,0 +1,14 @@
+#version 330 core
+
+layout(location = 0) in vec3 position;
+layout(location = 2) in vec2 tex_coords;
+
+out vert_t {
+ vec2 tex_coords;
+} vert;
+
+void main()
+{
+ vert.tex_coords = tex_coords;
+ gl_Position = vec4(position, 1.0);
+}