Debian Codegraph @

tests/manual/planets-qml/shaders/rhi/planetDB.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
7 layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
8 mat4 viewMatrix;
9 mat4 projectionMatrix;
10 mat4 uncorrectedProjectionMatrix;
11 mat4 clipCorrectionMatrix;
12 mat4 viewProjectionMatrix;
13 mat4 inverseViewMatrix;
14 mat4 inverseProjectionMatrix;
15 mat4 inverseViewProjectionMatrix;
16 mat4 viewportMatrix;
17 mat4 inverseViewportMatrix;
18 vec4 textureTransformMatrix;
19 vec3 eyePosition;
20 float aspectRatio;
21 float gamma;
22 float exposure;
23 float time;
24 };
25
26 layout(std140, binding = 2) uniform extras_uniforms {
27 float texCoordScale;
28 vec3 ka; // Ambient reflectivity
29 vec3 ks; // Specular reflectivity
30 float shininess; // Specular shininess factor
31 float opacity; // Alpha channel
32 vec3 lightPosition;
33 vec3 lightIntensity;
34 };
35
36 layout(binding=3) uniform sampler2D diffuseTexture;
37 layout(binding=4) uniform sampler2D normalTexture;
38
39 layout(location = 0) in vec3 lightDir;
40 layout(location = 1) in vec3 viewDir;
41 layout(location = 2) in vec2 texCoord;
42
43 layout(location = 0) out vec4 fragColor;
44
45 void dbModel(const in vec3 norm, const in vec2 flipYTexCoord, out vec3 ambientAndDiff, out vec3 spec)
46 {
47 // Reflection of light direction about normal
48 vec3 r = reflect(-lightDir, norm);
49
50 vec3 diffuseColor = texture(diffuseTexture, flipYTexCoord).rgb;
51
52 // Calculate the ambient contribution
53 vec3 ambient = lightIntensity * ka * diffuseColor;
54
55 // Calculate the diffuse contribution
56 float sDotN = max(dot(lightDir, norm), 0.0);
57 vec3 diffuse = lightIntensity * diffuseColor * sDotN;
58
59 // Sum the ambient and diffuse contributions
60 ambientAndDiff = ambient + diffuse;
61
62 // Calculate the specular highlight contribution
63 spec = vec3(0.0);
64 if (sDotN > 0.0)
65 spec = (lightIntensity * ks) * pow(max(dot(r, viewDir), 0.0), shininess);
66 }
67
68 void main()
69 {
70 vec2 flipYTexCoord = texCoord;
71 flipYTexCoord.y = 1.0 - texCoord.y;
72
73 // Sample the textures at the interpolated texCoords
74 vec4 normal = 2.0 * texture(normalTexture, flipYTexCoord) - vec4(1.0);
75
76 vec3 result = lightIntensity * ka * texture(diffuseTexture, flipYTexCoord).rgb;
77
78 // Calculate the lighting model, keeping the specular component separate
79 vec3 ambientAndDiff, spec;
80 dbModel(normalize(normal.xyz), flipYTexCoord, ambientAndDiff, spec);
81 result = ambientAndDiff + spec;
82
83 // Combine spec with ambient+diffuse for final fragment color
84 fragColor = vec4(result, opacity);
85 }