| 1 |
// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 |
|
| 4 |
#pragma include light.inc.frag |
| 5 |
|
| 6 |
void adsModel(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3 vview, const in FP float shininess, |
| 7 |
out FP vec3 ambientColor, |
| 8 |
out FP vec3 diffuseColor, out FP vec3 specularColor) |
| 9 |
{ |
| 10 |
ambientColor = vec3(0.0); |
| 11 |
diffuseColor = vec3(0.0); |
| 12 |
specularColor = vec3(0.0); |
| 13 |
|
| 14 |
FP vec3 n = normalize( vnormal ); |
| 15 |
|
| 16 |
FP vec3 s; |
| 17 |
Light light; |
| 18 |
for (int i = 0; i < lightCount; ++i) { |
| 19 |
if (i == 0) |
| 20 |
light = lights[0]; |
| 21 |
else if (i == 1) |
| 22 |
light = lights[1]; |
| 23 |
else if (i == 2) |
| 24 |
light = lights[2]; |
| 25 |
else if (i == 3) |
| 26 |
light = lights[3]; |
| 27 |
else if (i == 4) |
| 28 |
light = lights[4]; |
| 29 |
else if (i == 5) |
| 30 |
light = lights[5]; |
| 31 |
else if (i == 6) |
| 32 |
light = lights[6]; |
| 33 |
else if (i == 7) |
| 34 |
light = lights[7]; |
| 35 |
|
| 36 |
FP float att = 1.0; |
| 37 |
if ( light.type != TYPE_DIRECTIONAL ) { |
| 38 |
s = light.position - vpos; |
| 39 |
if (light.constantAttenuation != 0.0 |
| 40 |
|| light.linearAttenuation != 0.0 |
| 41 |
|| light.quadraticAttenuation != 0.0) { |
| 42 |
FP float dist = length(s); |
| 43 |
att = 1.0 / (light.constantAttenuation + light.linearAttenuation * dist + light.quadraticAttenuation * dist * dist); |
| 44 |
} |
| 45 |
s = normalize( s ); |
| 46 |
if ( light.type == TYPE_SPOT ) { |
| 47 |
if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle) |
| 48 |
att = 0.0; |
| 49 |
} |
| 50 |
} else { |
| 51 |
s = normalize( -light.direction ); |
| 52 |
} |
| 53 |
|
| 54 |
FP float diffuse = max( dot( s, n ), 0.0 ); |
| 55 |
|
| 56 |
FP float specular = 0.0; |
| 57 |
if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) { |
| 58 |
FP vec3 r = reflect( -s, n ); |
| 59 |
FP float normFactor = ( shininess + 2.0 ) / 2.0; |
| 60 |
specular = normFactor * pow( max( dot( r, vview ), 0.0 ), shininess ); |
| 61 |
} |
| 62 |
|
| 63 |
ambientColor += att * light.intensity * 1.0 * light.color; |
| 64 |
diffuseColor += att * light.intensity * diffuse * light.color; |
| 65 |
specularColor += att * light.intensity * specular * light.color; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
FP vec4 phongFunction(const in FP vec4 ambient, |
| 70 |
const in FP vec4 diffuse, |
| 71 |
const in FP vec4 specular, |
| 72 |
const in FP float shininess, |
| 73 |
const in FP vec3 worldPosition, |
| 74 |
const in FP vec3 worldView, |
| 75 |
const in FP vec3 worldNormal) |
| 76 |
{ |
| 77 |
// Calculate the lighting model, keeping the specular component separate |
| 78 |
FP vec3 ambientColor, diffuseColor, specularColor; |
| 79 |
adsModel(worldPosition, worldNormal, worldView, shininess, ambientColor, diffuseColor, specularColor); |
| 80 |
|
| 81 |
// Combine ambient, diffuse and specular components for final fragment color |
| 82 |
FP vec3 color = ambientColor * ambient.rgb |
| 83 |
+ diffuseColor * diffuse.rgb |
| 84 |
+ specularColor * specular.rgb; |
| 85 |
|
| 86 |
return vec4(color, diffuse.a); |
| 87 |
} |