| 1 |
// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// Copyright (C) 2017 The Qt Company Ltd. |
| 3 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 |
|
| 5 |
#version 150 core |
| 6 |
|
| 7 |
uniform mat4 viewMatrix; |
| 8 |
|
| 9 |
uniform vec3 lightPosition; |
| 10 |
uniform vec3 lightIntensity; |
| 11 |
|
| 12 |
uniform vec3 ka; // Ambient reflectivity |
| 13 |
uniform vec3 ks; // Specular reflectivity |
| 14 |
uniform float shininess; // Specular shininess factor |
| 15 |
uniform float opacity; // Alpha channel |
| 16 |
|
| 17 |
uniform sampler2D diffuseTexture; |
| 18 |
|
| 19 |
uniform sampler2DShadow shadowMapTexture; |
| 20 |
|
| 21 |
in vec4 positionInLightSpace; |
| 22 |
|
| 23 |
in vec3 position; |
| 24 |
in vec3 normal; |
| 25 |
in vec2 texCoord; |
| 26 |
|
| 27 |
out vec4 fragColor; |
| 28 |
|
| 29 |
vec3 dModel(const in vec2 flipYTexCoord) |
| 30 |
{ |
| 31 |
// Calculate the vector from the light to the fragment |
| 32 |
vec3 s = normalize(vec3(viewMatrix * vec4(lightPosition, 1.0)) - position); |
| 33 |
|
| 34 |
// Calculate the vector from the fragment to the eye position |
| 35 |
// (origin since this is in "eye" or "camera" space) |
| 36 |
vec3 v = normalize(-position); |
| 37 |
|
| 38 |
// Reflect the light beam using the normal at this fragment |
| 39 |
vec3 r = reflect(-s, normal); |
| 40 |
|
| 41 |
// Calculate the diffuse component |
| 42 |
float diffuse = max(dot(s, normal), 0.0); |
| 43 |
|
| 44 |
// Calculate the specular component |
| 45 |
float specular = 0.0; |
| 46 |
if (dot(s, normal) > 0.0) |
| 47 |
specular = (shininess / (8.0 * 3.14)) * pow(max(dot(r, v), 0.0), shininess); |
| 48 |
|
| 49 |
// Lookup diffuse and specular factors |
| 50 |
vec3 diffuseColor = texture(diffuseTexture, flipYTexCoord).rgb; |
| 51 |
|
| 52 |
// Combine the ambient, diffuse and specular contributions |
| 53 |
return lightIntensity * ((ka + diffuse) * diffuseColor + specular * ks); |
| 54 |
} |
| 55 |
|
| 56 |
void main() |
| 57 |
{ |
| 58 |
vec2 flipYTexCoord = texCoord; |
| 59 |
flipYTexCoord.y = 1.0 - texCoord.y; |
| 60 |
|
| 61 |
float shadowMapSample = textureProj(shadowMapTexture, positionInLightSpace); |
| 62 |
|
| 63 |
vec3 result = lightIntensity * ka * texture(diffuseTexture, flipYTexCoord).rgb; |
| 64 |
if (shadowMapSample > 0) |
| 65 |
result += dModel(flipYTexCoord); |
| 66 |
|
| 67 |
float alpha = opacity * texture(diffuseTexture, flipYTexCoord).a; |
| 68 |
|
| 69 |
fragColor = vec4(result, alpha); |
| 70 |
} |