| 1 |
// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#version 150 core |
| 5 |
|
| 6 |
uniform mat4 viewMatrix; |
| 7 |
|
| 8 |
uniform vec3 lightPosition; |
| 9 |
uniform vec3 lightIntensity; |
| 10 |
|
| 11 |
uniform vec3 ka; // Ambient reflectivity |
| 12 |
uniform vec3 kd; // Diffuse reflectivity |
| 13 |
uniform vec3 ks; // Specular reflectivity |
| 14 |
uniform float shininess; // Specular shininess factor |
| 15 |
|
| 16 |
uniform sampler2DShadow shadowMapTexture; |
| 17 |
|
| 18 |
in vec4 positionInLightSpace; |
| 19 |
|
| 20 |
in vec3 position; |
| 21 |
in vec3 normal; |
| 22 |
|
| 23 |
out vec4 fragColor; |
| 24 |
|
| 25 |
vec3 dsModel(const in vec3 pos, const in vec3 n) |
| 26 |
{ |
| 27 |
// Calculate the vector from the light to the fragment |
| 28 |
vec3 s = normalize(vec3(viewMatrix * vec4(lightPosition, 1.0)) - pos); |
| 29 |
|
| 30 |
// Calculate the vector from the fragment to the eye position |
| 31 |
// (origin since this is in "eye" or "camera" space) |
| 32 |
vec3 v = normalize(-pos); |
| 33 |
|
| 34 |
// Reflect the light beam using the normal at this fragment |
| 35 |
vec3 r = reflect(-s, n); |
| 36 |
|
| 37 |
// Calculate the diffuse component |
| 38 |
float diffuse = max(dot(s, n), 0.0); |
| 39 |
|
| 40 |
// Calculate the specular component |
| 41 |
float specular = 0.0; |
| 42 |
if (dot(s, n) > 0.0) |
| 43 |
specular = pow(max(dot(r, v), 0.0), shininess); |
| 44 |
|
| 45 |
// Combine the diffuse and specular contributions (ambient is taken into account by the caller) |
| 46 |
return lightIntensity * (kd * diffuse + ks * specular); |
| 47 |
} |
| 48 |
|
| 49 |
void main() |
| 50 |
{ |
| 51 |
float shadowMapSample = textureProj(shadowMapTexture, positionInLightSpace); |
| 52 |
|
| 53 |
vec3 ambient = lightIntensity * ka; |
| 54 |
|
| 55 |
vec3 result = ambient; |
| 56 |
if (shadowMapSample > 0) |
| 57 |
result += dsModel(position, normalize(normal)); |
| 58 |
|
| 59 |
fragColor = vec4(result, 1.0); |
| 60 |
} |