Debian Codegraph @

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