summaryrefslogtreecommitdiff
path: root/advanced_lighting/9.ssao/deferred.frag
diff options
context:
space:
mode:
Diffstat (limited to 'advanced_lighting/9.ssao/deferred.frag')
-rw-r--r--advanced_lighting/9.ssao/deferred.frag44
1 files changed, 44 insertions, 0 deletions
diff --git a/advanced_lighting/9.ssao/deferred.frag b/advanced_lighting/9.ssao/deferred.frag
new file mode 100644
index 0000000..5a3cc33
--- /dev/null
+++ b/advanced_lighting/9.ssao/deferred.frag
@@ -0,0 +1,44 @@
+#version 330 core
+
+in vert_t {
+ vec2 tex_coords;
+} vert;
+
+out vec4 frag_color;
+
+uniform sampler2D positions;
+uniform sampler2D normals;
+uniform sampler2D albedo;
+uniform sampler2D ssao;
+
+uniform vec3 view_position;
+
+struct light_t {
+ vec3 position;
+ vec3 color;
+};
+
+uniform light_t light;
+
+const float linear = 0.7f;
+const float quadratic = 1.8f;
+
+void main()
+{
+ vec3 position = texture(positions, vert.tex_coords).xyz;
+ vec3 normal = texture(normals, vert.tex_coords).xyz;
+ vec3 color = texture(albedo, vert.tex_coords).rgb;
+ float occlusion = texture(ssao, vert.tex_coords).r;
+
+ vec3 ambient = 0.1 * color * occlusion;
+ vec3 result = ambient;
+ vec3 view_dir = normalize(-position);
+ vec3 light_dir = normalize(light.position - position);
+ vec3 diffuse = max(dot(normal, light_dir), 0.0) * color * light.color;
+ vec3 halfway_dir = normalize(view_dir + light_dir);
+ vec3 specular = pow(max(dot(halfway_dir, normal), 0.0), 16.0) * vec3(0.5);
+ float distance = length(light.position - position);
+ float attenuation = 1.0 / (1.0 + linear * distance + quadratic * distance * distance);
+ result += (diffuse + specular) * attenuation;
+ frag_color = vec4(result, 1.0);
+}