Debian Codegraph @

tests/manual/planets-qml/shaders/gl3/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 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 vec3 ks; // Specular reflectivity
14 uniform float shininess; // Specular shininess factor
15 uniform float opacity; // Alpha channel
16
17 uniform sampler2D diffuseTexture;
18
19 in vec3 position;
20 in vec3 normal;
21 in vec2 texCoord;
22
23 out vec4 fragColor;
24
25 vec3 dModel(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
48 // Combine the ambient, diffuse and specular contributions
49 return lightIntensity * ((ka + diffuse) * diffuseColor + specular * ks);
50 }
51
52 void main()
53 {
54 vec2 flipYTexCoord = texCoord;
55 flipYTexCoord.y = 1.0 - texCoord.y;
56
57 vec3 result = lightIntensity * ka * texture(diffuseTexture, flipYTexCoord).rgb;
58 result += dModel(flipYTexCoord);
59
60 float alpha = opacity * texture(diffuseTexture, flipYTexCoord).a;
61
62 fragColor = vec4(result, alpha);
63 }