Debian Codegraph @

src/extras/shaders/gl3/gooch.frag

raw
1 #version 150 core
2
3 // TODO: Replace with a struct
4 uniform vec3 kd; // Diffuse reflectivity
5 uniform vec3 ks; // Specular reflectivity
6 uniform vec3 kblue; // Cool color
7 uniform vec3 kyellow; // Warm color
8 uniform float alpha; // Fraction of diffuse added to kblue
9 uniform float beta; // Fraction of diffuse added to kyellow
10 uniform float shininess; // Specular shininess factor
11
12 uniform vec3 eyePosition;
13
14 in vec3 worldPosition;
15 in vec3 worldNormal;
16
17 out vec4 fragColor;
18
19 #pragma include light.inc.frag
20
21 vec3 goochModel( const in vec3 pos, const in vec3 n )
22 {
23 // Based upon the original Gooch lighting model paper at:
24 // http://www.cs.northwestern.edu/~ago820/SIG98/abstract.html
25
26 // Calculate kcool and kwarm from equation (3)
27 vec3 kcool = clamp(kblue + alpha * kd, 0.0, 1.0);
28 vec3 kwarm = clamp(kyellow + beta * kd, 0.0, 1.0);
29
30 vec3 result = vec3(0.0);
31 for (int i = 0; i < lightCount; ++i) {
32 // Calculate the vector from the light to the fragment
33 vec3 s = normalize( vec3( lights[i].position ) - pos );
34
35 // Calculate the cos theta factor mapped onto the range [0,1]
36 float sDotNFactor = ( 1.0 + dot( s, n ) ) / 2.0;
37
38 // Calculate the tone by blending the kcool and kwarm contributions
39 // as per equation (2)
40 vec3 intensity = mix( kcool, kwarm, sDotNFactor );
41
42 // Calculate the vector from the fragment to the eye position
43 vec3 v = normalize( eyePosition - pos );
44
45 // Reflect the light beam using the normal at this fragment
46 vec3 r = reflect( -s, n );
47
48 // Calculate the specular component
49 float specular = 0.0;
50 if ( dot( s, n ) > 0.0 )
51 specular = pow( max( dot( r, v ), 0.0 ), shininess );
52
53 // Sum the blended tone and specular highlight
54 result += intensity + ks * specular;
55 }
56
57 return result;
58 }
59
60 void main()
61 {
62 fragColor = vec4( goochModel( worldPosition, normalize( worldNormal ) ), 1.0 );
63 }