Debian Codegraph @

tests/manual/deferred-renderer-qml/final_es2.frag

raw
1 #version 110
2
3 uniform sampler2D color;
4 uniform sampler2D position;
5 uniform sampler2D normal;
6 uniform vec2 winSize;
7
8 const int MAX_LIGHTS = 8;
9 const int TYPE_POINT = 0;
10 const int TYPE_DIRECTIONAL = 1;
11 const int TYPE_SPOT = 2;
12
13 struct Light {
14 int type;
15 vec3 position;
16 vec3 color;
17 float intensity;
18 vec3 direction;
19 float constantAttenuation;
20 float linearAttenuation;
21 float quadraticAttenuation;
22 float cutOffAngle;
23 };
24
25 uniform Light lights[MAX_LIGHTS];
26 uniform int lightCount;
27
28 void main()
29 {
30 vec2 texCoord = gl_FragCoord.xy / winSize;
31 vec4 col = texture2D(color, texCoord);
32 vec3 pos = texture2D(position, texCoord).xyz;
33 vec3 norm = texture2D(normal, texCoord).xyz;
34
35 vec3 lightColor = vec3(0.0);
36 vec3 s;
37
38 for (int i = 0; i < lightCount; ++i) {
39 float att = 1.0;
40 if ( lights[i].type != TYPE_DIRECTIONAL ) {
41 s = lights[i].position - pos;
42 if (lights[i].constantAttenuation != 0.0
43 || lights[i].linearAttenuation != 0.0
44 || lights[i].quadraticAttenuation != 0.0) {
45 float dist = length(s);
46 att = 1.0 / (lights[i].constantAttenuation + lights[i].linearAttenuation * dist + lights[i].quadraticAttenuation * dist * dist);
47 }
48 s = normalize( s );
49 if ( lights[i].type == TYPE_SPOT ) {
50 if ( degrees(acos(dot(-s, normalize(lights[i].direction))) ) > lights[i].cutOffAngle)
51 att = 0.0;
52 }
53 } else {
54 s = normalize( -lights[i].direction );
55 }
56
57 float diffuse = max( dot( s, norm ), 0.0 );
58
59 lightColor += att * lights[i].intensity * diffuse * lights[i].color;
60 }
61 gl_FragColor = vec4(col.rgb * lightColor, col.a);
62 }