Debian Codegraph @

tests/manual/planets-qml/shaders/rhi/planetDShadow.frag

raw
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 450 core
6 layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
7 mat4 viewMatrix;
8 mat4 projectionMatrix;
9 mat4 uncorrectedProjectionMatrix;
10 mat4 clipCorrectionMatrix;
11 mat4 viewProjectionMatrix;
12 mat4 inverseViewMatrix;
13 mat4 inverseProjectionMatrix;
14 mat4 inverseViewProjectionMatrix;
15 mat4 viewportMatrix;
16 mat4 inverseViewportMatrix;
17 vec4 textureTransformMatrix;
18 vec3 eyePosition;
19 float aspectRatio;
20 float gamma;
21 float exposure;
22 float time;
23 };
24
25 layout(std140, binding = 2) uniform extras_uniforms {
26 float texCoordScale;
27 vec3 ka; // Ambient reflectivity
28 vec3 ks; // Specular reflectivity
29 float shininess; // Specular shininess factor
30 float opacity; // Alpha channel
31 vec3 lightPosition;
32 vec3 lightIntensity;
33 };
34
35 layout(binding=3) uniform sampler2D diffuseTexture;
36 layout(binding=4) uniform sampler2DShadow shadowMapTexture;
37
38 layout(location = 3) in vec4 positionInLightSpace;
39
40 layout(location = 0) in vec3 position;
41 layout(location = 1) in vec3 normal;
42 layout(location = 2) in vec2 texCoord;
43
44 layout(location = 0)out vec4 fragColor;
45
46 vec3 dModel(const in vec2 flipYTexCoord)
47 {
48 // Calculate the vector from the light to the fragment
49 vec3 s = normalize(vec3(viewMatrix * vec4(lightPosition, 1.0)) - position);
50
51 // Calculate the vector from the fragment to the eye position
52 // (origin since this is in "eye" or "camera" space)
53 vec3 v = normalize(-position);
54
55 // Reflect the light beam using the normal at this fragment
56 vec3 r = reflect(-s, normal);
57
58 // Calculate the diffuse component
59 float diffuse = max(dot(s, normal), 0.0);
60
61 // Calculate the specular component
62 float specular = 0.0;
63 if (dot(s, normal) > 0.0)
64 specular = (shininess / (8.0 * 3.14)) * pow(max(dot(r, v), 0.0), shininess);
65
66 // Lookup diffuse and specular factors
67 vec3 diffuseColor = texture(diffuseTexture, flipYTexCoord).rgb;
68
69 // Combine the ambient, diffuse and specular contributions
70 return lightIntensity * ((ka + diffuse) * diffuseColor + specular * ks);
71 }
72
73 void main()
74 {
75 vec2 flipYTexCoord = texCoord;
76 flipYTexCoord.y = 1.0 - texCoord.y;
77
78 float shadowMapSample = textureProj(shadowMapTexture, positionInLightSpace);
79
80 vec3 result = lightIntensity * ka * texture(diffuseTexture, flipYTexCoord).rgb;
81 if (shadowMapSample > 0)
82 result += dModel(flipYTexCoord);
83
84 float alpha = opacity * texture(diffuseTexture, flipYTexCoord).a;
85
86 fragColor = vec4(result, alpha);
87 }