summaryrefslogtreecommitdiff
path: root/advanced_lighting/7.bloom/shaders/bloom.frag
blob: 88decf8f269eac41ad919c30c0538d17c35d9a18 (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
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);
}