Debian Codegraph @

src/extras/shaders/es2/gooch.frag

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