Debian Codegraph @

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