summaryrefslogtreecommitdiff
path: root/advanced_lighting/9.ssao/deferred.frag
blob: 5a3cc330f9573b12556f816afea5819677dfd607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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);
}