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