| 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 |
// 0 |
| 17 |
if (lightCount < 1) |
| 18 |
return; |
| 19 |
FP vec3 s; |
| 20 |
FP float att = 1.0; |
| 21 |
if ( lights[0].type != TYPE_DIRECTIONAL ) { |
| 22 |
s = lights[0].position - vpos; |
| 23 |
if (lights[0].constantAttenuation != 0.0 |
| 24 |
|| lights[0].linearAttenuation != 0.0 |
| 25 |
|| lights[0].quadraticAttenuation != 0.0) { |
| 26 |
FP float dist = length(s); |
| 27 |
att = 1.0 / (lights[0].constantAttenuation + lights[0].linearAttenuation * dist + lights[0].quadraticAttenuation * dist * dist); |
| 28 |
} |
| 29 |
s = normalize( s ); |
| 30 |
if ( lights[0].type == TYPE_SPOT ) { |
| 31 |
if ( degrees(acos(dot(-s, normalize(lights[0].direction))) ) > lights[0].cutOffAngle) |
| 32 |
att = 0.0; |
| 33 |
} |
| 34 |
} else { |
| 35 |
s = normalize( -lights[0].direction ); |
| 36 |
} |
| 37 |
|
| 38 |
FP float diffuse = max( dot( s, n ), 0.0 ); |
| 39 |
|
| 40 |
FP float specular = 0.0; |
| 41 |
if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) { |
| 42 |
FP vec3 r = reflect( -s, n ); |
| 43 |
FP float normFactor = ( shininess + 2.0 ) / 2.0; |
| 44 |
specular = normFactor * pow( max( dot( r, vview ), 0.0 ), shininess ); |
| 45 |
} |
| 46 |
|
| 47 |
diffuseColor += att * lights[0].intensity * diffuse * lights[0].color; |
| 48 |
specularColor += att * specular; |
| 49 |
|
| 50 |
// 1 |
| 51 |
if (lightCount < 2) |
| 52 |
return; |
| 53 |
att = 1.0; |
| 54 |
if ( lights[1].type != TYPE_DIRECTIONAL ) { |
| 55 |
s = lights[1].position - vpos; |
| 56 |
if (lights[1].constantAttenuation != 0.0 |
| 57 |
|| lights[1].linearAttenuation != 0.0 |
| 58 |
|| lights[1].quadraticAttenuation != 0.0) { |
| 59 |
FP float dist = length(s); |
| 60 |
att = 1.0 / (lights[1].constantAttenuation + lights[1].linearAttenuation * dist + lights[1].quadraticAttenuation * dist * dist); |
| 61 |
} |
| 62 |
s = normalize( s ); |
| 63 |
if ( lights[1].type == TYPE_SPOT ) { |
| 64 |
if ( degrees(acos(dot(-s, normalize(lights[1].direction))) ) > lights[1].cutOffAngle) |
| 65 |
att = 0.0; |
| 66 |
} |
| 67 |
} else { |
| 68 |
s = normalize( -lights[1].direction ); |
| 69 |
} |
| 70 |
|
| 71 |
diffuse = max( dot( s, n ), 0.0 ); |
| 72 |
|
| 73 |
specular = 0.0; |
| 74 |
if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) { |
| 75 |
FP vec3 r = reflect( -s, n ); |
| 76 |
FP float normFactor = ( shininess + 2.0 ) / 2.0; |
| 77 |
specular = normFactor * pow( max( dot( r, vview ), 0.0 ), shininess ); |
| 78 |
} |
| 79 |
|
| 80 |
ambientColor += att * lights[1].intensity * 1.0 * lights[1].color; |
| 81 |
diffuseColor += att * lights[1].intensity * diffuse * lights[1].color; |
| 82 |
specularColor += att * specular; |
| 83 |
} |
| 84 |
|
| 85 |
FP vec4 phongFunction(const in FP vec4 ambient, |
| 86 |
const in FP vec4 diffuse, |
| 87 |
const in FP vec4 specular, |
| 88 |
const in FP float shininess, |
| 89 |
const in FP vec3 worldPosition, |
| 90 |
const in FP vec3 worldView, |
| 91 |
const in FP vec3 worldNormal) |
| 92 |
{ |
| 93 |
// Calculate the lighting model, keeping the specular component separate |
| 94 |
FP vec3 ambientColor, diffuseColor, specularColor; |
| 95 |
adsModel(worldPosition, worldNormal, worldView, shininess, ambientColor, diffuseColor, specularColor); |
| 96 |
|
| 97 |
// Combine ambient, diffuse and specular components for final fragment color |
| 98 |
FP vec3 color = ambientColor * ambient.rgb |
| 99 |
+ diffuseColor * diffuse.rgb |
| 100 |
+ specularColor * specular.rgb; |
| 101 |
|
| 102 |
return vec4(color, diffuse.a); |
| 103 |
} |