Debian Codegraph @

src/extras/shaders/rhi/gooch.frag

raw
1 #version 450
2
3 layout(location = 0) in vec3 worldPosition;
4 layout(location = 1) in vec3 worldNormal;
5
6 layout(location = 0) out vec4 fragColor;
7
8 layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
9 mat4 viewMatrix;
10 mat4 projectionMatrix;
11 mat4 uncorrectedProjectionMatrix;
12 mat4 clipCorrectionMatrix;
13 mat4 viewProjectionMatrix;
14 mat4 inverseViewMatrix;
15 mat4 inverseProjectionMatrix;
16 mat4 inverseViewProjectionMatrix;
17 mat4 viewportMatrix;
18 mat4 inverseViewportMatrix;
19 vec4 textureTransformMatrix;
20 vec3 eyePosition;
21 float aspectRatio;
22 float gamma;
23 float exposure;
24 float time;
25 };
26
27 layout(std140, binding = 1) uniform qt3d_command_uniforms {
28 mat4 modelMatrix;
29 mat4 inverseModelMatrix;
30 mat4 modelViewMatrix;
31 mat3 modelNormalMatrix;
32 mat4 inverseModelViewMatrix;
33 mat4 mvp;
34 mat4 inverseModelViewProjectionMatrix;
35 };
36
37 layout(std140, binding = 2) uniform qt3d_custom_uniforms {
38 vec3 kd; // Diffuse reflectivity
39 vec3 ks; // Specular reflectivity
40 vec3 kblue; // Cool color
41 vec3 kyellow; // Warm color
42 float alpha; // Fraction of diffuse added to kblue
43 float beta; // Fraction of diffuse added to kyellow
44 float shininess; // Specular shininess factor
45 };
46
47 const int MAX_LIGHTS = 8;
48 const int TYPE_POINT = 0;
49 const int TYPE_DIRECTIONAL = 1;
50 const int TYPE_SPOT = 2;
51
52 struct Light {
53 vec3 position;
54 int type;
55 vec3 color;
56 float intensity;
57 vec3 direction;
58 float constantAttenuation;
59 vec3 padding0;
60 float linearAttenuation;
61 vec3 padding1;
62 float quadraticAttenuation;
63 vec3 padding2;
64 float cutOffAngle;
65 };
66
67 layout(std140, binding = 3) uniform qt3d_light_uniforms {
68 uniform Light lights[MAX_LIGHTS];
69 uniform int lightCount;
70 uniform int envLightCount;
71 };
72
73
74 vec3 goochModel( const in vec3 pos, const in vec3 n )
75 {
76 // Based upon the original Gooch lighting model paper at:
77 // http://www.cs.northwestern.edu/~ago820/SIG98/abstract.html
78
79 // Calculate kcool and kwarm from equation (3)
80 vec3 kcool = clamp(kblue + alpha * kd, 0.0, 1.0);
81 vec3 kwarm = clamp(kyellow + beta * kd, 0.0, 1.0);
82
83 vec3 result = vec3(0.0);
84 for (int i = 0; i < lightCount; ++i) {
85 // Calculate the vector from the light to the fragment
86 vec3 s = normalize( vec3( lights[i].position ) - pos );
87
88 // Calculate the cos theta factor mapped onto the range [0,1]
89 float sDotNFactor = ( 1.0 + dot( s, n ) ) / 2.0;
90
91 // Calculate the tone by blending the kcool and kwarm contributions
92 // as per equation (2)
93 vec3 intensity = mix( kcool, kwarm, sDotNFactor );
94
95 // Calculate the vector from the fragment to the eye position
96 vec3 v = normalize( eyePosition - pos );
97
98 // Reflect the light beam using the normal at this fragment
99 vec3 r = reflect( -s, n );
100
101 // Calculate the specular component
102 float specular = 0.0;
103 if ( dot( s, n ) > 0.0 )
104 specular = pow( max( dot( r, v ), 0.0 ), shininess );
105
106 // Sum the blended tone and specular highlight
107 result += intensity + ks * specular;
108 }
109
110 return result;
111 }
112
113 void main()
114 {
115 fragColor = vec4( goochModel( worldPosition, normalize( worldNormal ) ), 1.0 );
116 }