| 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 vec3 worldPos, |
| 7 |
const in vec3 worldNormal, |
| 8 |
const in vec3 worldView, |
| 9 |
const in float shininess, |
| 10 |
out vec3 ambientColor, |
| 11 |
out vec3 diffuseColor, |
| 12 |
out vec3 specularColor) |
| 13 |
{ |
| 14 |
ambientColor = vec3(0.0); |
| 15 |
diffuseColor = vec3(0.0); |
| 16 |
specularColor = vec3(0.0); |
| 17 |
|
| 18 |
// We perform all work in world space |
| 19 |
vec3 n = normalize(worldNormal); |
| 20 |
vec3 s = vec3(0.0); |
| 21 |
|
| 22 |
for (int i = 0; i < lightCount; ++i) { |
| 23 |
float att = 1.0; |
| 24 |
float sDotN = 0.0; |
| 25 |
|
| 26 |
if (lights[i].type != TYPE_DIRECTIONAL) { |
| 27 |
// Point and Spot lights |
| 28 |
|
| 29 |
// Light position is already in world space |
| 30 |
vec3 sUnnormalized = lights[i].position - worldPos; |
| 31 |
s = normalize(sUnnormalized); // Light direction |
| 32 |
|
| 33 |
// Calculate the attenuation factor |
| 34 |
sDotN = dot(s, n); |
| 35 |
if (sDotN > 0.0) { |
| 36 |
if (lights[i].constantAttenuation != 0.0 |
| 37 |
|| lights[i].linearAttenuation != 0.0 |
| 38 |
|| lights[i].quadraticAttenuation != 0.0) { |
| 39 |
float dist = length(sUnnormalized); |
| 40 |
att = 1.0 / (lights[i].constantAttenuation + |
| 41 |
lights[i].linearAttenuation * dist + |
| 42 |
lights[i].quadraticAttenuation * dist * dist); |
| 43 |
} |
| 44 |
|
| 45 |
// The light direction is in world space already |
| 46 |
if (lights[i].type == TYPE_SPOT) { |
| 47 |
// Check if fragment is inside or outside of the spot light cone |
| 48 |
if (degrees(acos(dot(-s, lights[i].direction))) > lights[i].cutOffAngle) |
| 49 |
sDotN = 0.0; |
| 50 |
} |
| 51 |
} |
| 52 |
} else { |
| 53 |
// Directional lights |
| 54 |
// The light direction is in world space already |
| 55 |
s = normalize(-lights[i].direction); |
| 56 |
sDotN = dot(s, n); |
| 57 |
} |
| 58 |
|
| 59 |
// Calculate the diffuse factor |
| 60 |
float diffuse = max(sDotN, 0.0); |
| 61 |
|
| 62 |
// Calculate the specular factor |
| 63 |
float specular = 0.0; |
| 64 |
if (diffuse > 0.0 && shininess > 0.0) { |
| 65 |
float normFactor = (shininess + 2.0) / 2.0; |
| 66 |
vec3 r = reflect(-s, n); // Reflection direction in world space |
| 67 |
specular = normFactor * pow(max(dot(r, worldView), 0.0), shininess); |
| 68 |
} |
| 69 |
|
| 70 |
// Accumulate the ambient, diffuse and specular contributions |
| 71 |
ambientColor += att * lights[i].intensity * 1.0 * lights[i].color; |
| 72 |
diffuseColor += att * lights[i].intensity * diffuse * lights[i].color; |
| 73 |
specularColor += att * lights[i].intensity * specular * lights[i].color; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
vec4 phongFunction(const in vec4 ambient, |
| 78 |
const in vec4 diffuse, |
| 79 |
const in vec4 specular, |
| 80 |
const in float shininess, |
| 81 |
const in vec3 worldPosition, |
| 82 |
const in vec3 worldView, |
| 83 |
const in vec3 worldNormal) |
| 84 |
{ |
| 85 |
// Calculate the lighting model, keeping the specular component separate |
| 86 |
vec3 ambientColor, diffuseColor, specularColor; |
| 87 |
adsModel(worldPosition, worldNormal, worldView, shininess, ambientColor, diffuseColor, specularColor); |
| 88 |
|
| 89 |
// Combine ambient, diffuse and specular components for final fragment color |
| 90 |
vec3 color = ambientColor * ambient.rgb |
| 91 |
+ diffuseColor * diffuse.rgb |
| 92 |
+ specularColor * specular.rgb; |
| 93 |
|
| 94 |
return vec4(color, diffuse.a); |
| 95 |
} |