|
| 1 | +#version 330 core |
| 2 | +out vec4 finalColor; |
| 3 | + |
| 4 | +in vec2 texCoord; |
| 5 | +in vec2 texCoord2; |
| 6 | + |
| 7 | +uniform sampler2D gPosition; |
| 8 | +uniform sampler2D gNormal; |
| 9 | +uniform sampler2D gAlbedoSpec; |
| 10 | + |
| 11 | +struct Light { |
| 12 | + int enabled; |
| 13 | + int type; // Unused in this demo. |
| 14 | + vec3 position; |
| 15 | + vec3 target; // Unused in this demo. |
| 16 | + vec4 color; |
| 17 | +}; |
| 18 | + |
| 19 | +const int NR_LIGHTS = 4; |
| 20 | +uniform Light lights[NR_LIGHTS]; |
| 21 | +uniform vec3 viewPosition; |
| 22 | + |
| 23 | +const float QUADRATIC = 0.032; |
| 24 | +const float LINEAR = 0.09; |
| 25 | + |
| 26 | +void main() { |
| 27 | + vec3 fragPosition = texture(gPosition, texCoord).rgb; |
| 28 | + vec3 normal = texture(gNormal, texCoord).rgb; |
| 29 | + vec3 albedo = texture(gAlbedoSpec, texCoord).rgb; |
| 30 | + float specular = texture(gAlbedoSpec, texCoord).a; |
| 31 | + |
| 32 | + vec3 ambient = albedo * vec3(0.1f); |
| 33 | + vec3 viewDirection = normalize(viewPosition - fragPosition); |
| 34 | + |
| 35 | + for(int i = 0; i < NR_LIGHTS; ++i) |
| 36 | + { |
| 37 | + if(lights[i].enabled == 0) continue; |
| 38 | + vec3 lightDirection = lights[i].position - fragPosition; |
| 39 | + vec3 diffuse = max(dot(normal, lightDirection), 0.0) * albedo * lights[i].color.xyz; |
| 40 | + |
| 41 | + vec3 halfwayDirection = normalize(lightDirection + viewDirection); |
| 42 | + float spec = pow(max(dot(normal, halfwayDirection), 0.0), 32.0); |
| 43 | + vec3 specular = specular * spec * lights[i].color.xyz; |
| 44 | + |
| 45 | + // Attenuation |
| 46 | + float distance = length(lights[i].position - fragPosition); |
| 47 | + float attenuation = 1.0 / (1.0 + LINEAR * distance + QUADRATIC * distance * distance); |
| 48 | + diffuse *= attenuation; |
| 49 | + specular *= attenuation; |
| 50 | + ambient += diffuse + specular; |
| 51 | + } |
| 52 | + |
| 53 | + finalColor = vec4(ambient, 1.0); |
| 54 | +} |
| 55 | + |
0 commit comments