| 1 |
// Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#version 450 |
| 5 |
|
| 6 |
layout(location = 0) in vec4 positionInLightSpace; |
| 7 |
layout(location = 1) in vec3 position; |
| 8 |
layout(location = 2) in vec3 normal; |
| 9 |
|
| 10 |
layout(location = 0) out vec4 fragColor; |
| 11 |
|
| 12 |
layout(std140, binding = 0) uniform qt3d_render_view_uniforms { |
| 13 |
mat4 viewMatrix; |
| 14 |
mat4 projectionMatrix; |
| 15 |
mat4 viewProjectionMatrix; |
| 16 |
mat4 inverseViewMatrix; |
| 17 |
mat4 inverseProjectionMatrix; |
| 18 |
mat4 inverseViewProjectionMatrix; |
| 19 |
mat4 viewportMatrix; |
| 20 |
mat4 inverseViewportMatrix; |
| 21 |
vec4 textureTransformMatrix; |
| 22 |
vec3 eyePosition; |
| 23 |
float aspectRatio; |
| 24 |
float gamma; |
| 25 |
float exposure; |
| 26 |
float time; |
| 27 |
}; |
| 28 |
|
| 29 |
layout(std140, binding = 1) uniform qt3d_command_uniforms { |
| 30 |
mat4 modelMatrix; |
| 31 |
mat4 inverseModelMatrix; |
| 32 |
mat4 modelView; |
| 33 |
mat3 modelNormalMatrix; |
| 34 |
mat4 inverseModelViewMatrix; |
| 35 |
mat4 mvp; |
| 36 |
mat4 inverseModelViewProjectionMatrix; |
| 37 |
mat3 modelViewNormal; |
| 38 |
}; |
| 39 |
|
| 40 |
layout(std140, binding = 2) uniform qt3d_custom_uniforms { |
| 41 |
mat4 lightViewProjection; |
| 42 |
vec3 lightPosition; |
| 43 |
vec3 lightIntensity; |
| 44 |
|
| 45 |
vec3 ka; // Ambient reflectivity |
| 46 |
vec3 kd; // Diffuse reflectivity |
| 47 |
vec3 ks; // Specular reflectivity |
| 48 |
float shininess; // Specular shininess factor |
| 49 |
}; |
| 50 |
|
| 51 |
layout(binding = 3) uniform sampler2DShadow shadowMapTexture; |
| 52 |
|
| 53 |
|
| 54 |
vec3 dsModel(const in vec3 pos, const in vec3 n) |
| 55 |
{ |
| 56 |
// Calculate the vector from the light to the fragment |
| 57 |
vec3 s = normalize(vec3(viewMatrix * vec4(lightPosition, 1.0)) - pos); |
| 58 |
|
| 59 |
// Calculate the vector from the fragment to the eye position |
| 60 |
// (origin since this is in "eye" or "camera" space) |
| 61 |
vec3 v = normalize(-pos); |
| 62 |
|
| 63 |
// Reflect the light beam using the normal at this fragment |
| 64 |
vec3 r = reflect(-s, n); |
| 65 |
|
| 66 |
// Calculate the diffuse component |
| 67 |
float diffuse = max(dot(s, n), 0.0); |
| 68 |
|
| 69 |
// Calculate the specular component |
| 70 |
float specular = 0.0; |
| 71 |
if (dot(s, n) > 0.0) |
| 72 |
specular = pow(max(dot(r, v), 0.0), shininess); |
| 73 |
|
| 74 |
// Combine the diffuse and specular contributions (ambient is taken into account by the caller) |
| 75 |
return lightIntensity * (kd * diffuse + ks * specular); |
| 76 |
} |
| 77 |
|
| 78 |
void main() |
| 79 |
{ |
| 80 |
float shadowMapSample = textureProj(shadowMapTexture, positionInLightSpace); |
| 81 |
|
| 82 |
vec3 ambient = lightIntensity * ka; |
| 83 |
|
| 84 |
vec3 result = ambient; |
| 85 |
if (shadowMapSample > 0) |
| 86 |
result += dsModel(position, normalize(normal)); |
| 87 |
|
| 88 |
fragColor = vec4(result, 1.0); |
| 89 |
} |