Debian Codegraph @

tests/manual/planets-qml/shaders/gl3/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 150 core
6
7 uniform mat4 viewMatrix;
8
9 uniform vec3 lightPosition;
10 uniform vec3 lightIntensity;
11
12 uniform vec3 ka; // Ambient reflectivity
13 uniform float shininess; // Specular shininess factor
14 uniform float opacity; // Alpha channel
15
16 uniform sampler2D diffuseTexture;
17 uniform sampler2D specularTexture;
18
19 in vec3 position;
20 in vec3 normal;
21 in vec2 texCoord;
22
23 out vec4 fragColor;
24
25 vec3 dsModel(const in vec2 flipYTexCoord)
26 {
27 // Calculate the vector from the light to the fragment
28 vec3 s = normalize(vec3(viewMatrix * vec4(lightPosition, 1.0)) - position);
29
30 // Calculate the vector from the fragment to the eye position
31 // (origin since this is in "eye" or "camera" space)
32 vec3 v = normalize(-position);
33
34 // Reflect the light beam using the normal at this fragment
35 vec3 r = reflect(-s, normal);
36
37 // Calculate the diffuse component
38 float diffuse = max(dot(s, normal), 0.0);
39
40 // Calculate the specular component
41 float specular = 0.0;
42 if (dot(s, normal) > 0.0)
43 specular = (shininess / (8.0 * 3.14)) * pow(max(dot(r, v), 0.0), shininess);
44
45 // Lookup diffuse and specular factors
46 vec3 diffuseColor = texture(diffuseTexture, flipYTexCoord).rgb;
47 vec3 specularColor = texture(specularTexture, flipYTexCoord).rgb;
48
49 // Combine the ambient, diffuse and specular contributions
50 return lightIntensity * ((ka + diffuse) * diffuseColor + specular * specularColor);
51 }
52
53 void main()
54 {
55 vec2 flipYTexCoord = texCoord;
56 flipYTexCoord.y = 1.0 - texCoord.y;
57
58 vec3 result = lightIntensity * ka * texture(diffuseTexture, flipYTexCoord).rgb;
59 result += dsModel(flipYTexCoord);
60
61 fragColor = vec4(result, opacity * texture(diffuseTexture, flipYTexCoord).a);
62 }