Added toyball and bumpmap tests using shaders from the OpenGL Shading Language (orang...
[mesa.git] / progs / glsl / CH11-toyball.frag.txt
1 //
2 // Fragment shader for procedurally generated toy ball
3 //
4 // Author: Bill Licea-Kane
5 //
6 // Copyright (c) 2002-2003 ATI Research
7 //
8 // See ATI-License.txt for license information
9 //
10
11 varying vec4 ECposition; // surface position in eye coordinates
12 varying vec4 ECballCenter; // ball center in eye coordinates
13
14 uniform vec4 LightDir; // light direction, should be normalized
15 uniform vec4 HVector; // reflection vector for infinite light source
16 uniform vec4 SpecularColor;
17 uniform vec4 Red, Yellow, Blue;
18
19 uniform vec4 HalfSpace0; // half-spaces used to define star pattern
20 uniform vec4 HalfSpace1;
21 uniform vec4 HalfSpace2;
22 uniform vec4 HalfSpace3;
23 uniform vec4 HalfSpace4;
24
25 uniform float InOrOutInit; // = -3
26 uniform float StripeWidth; // = 0.3
27 uniform float FWidth; // = 0.005
28
29 void main()
30 {
31 vec4 normal; // Analytically computed normal
32 vec4 p; // Point in shader space
33 vec4 surfColor; // Computed color of the surface
34 float intensity; // Computed light intensity
35 vec4 distance; // Computed distance values
36 float inorout; // Counter for computing star pattern
37
38 p.xyz = normalize(ECposition.xyz - ECballCenter.xyz); // Calculate p
39 p.w = 1.0;
40
41 inorout = InOrOutInit; // initialize inorout to -3
42
43 distance[0] = dot(p, HalfSpace0);
44 distance[1] = dot(p, HalfSpace1);
45 distance[2] = dot(p, HalfSpace2);
46 distance[3] = dot(p, HalfSpace3);
47
48 #if 1
49 distance = smoothstep(-FWidth, FWidth, distance);
50 inorout += dot(distance, vec4(1.0));
51
52 distance.x = dot(p, HalfSpace4);
53 distance.y = StripeWidth - abs(p.z);
54 distance = smoothstep(-FWidth, FWidth, distance);
55 inorout += distance.x;
56
57 inorout = clamp(inorout, 0.0, 1.0);
58
59 surfColor = mix(Yellow, Red, inorout);
60 surfColor = mix(surfColor, Blue, distance.y);
61
62 // normal = point on surface for sphere at (0,0,0)
63 normal = p;
64
65 // Per fragment diffuse lighting
66 intensity = 0.2; // ambient
67 intensity += 0.8 * clamp(dot(LightDir, normal), 0.0, 1.0);
68 surfColor *= intensity;
69
70 // Per fragment specular lighting
71 intensity = clamp(dot(HVector, normal), 0.0, 1.0);
72 intensity = pow(intensity, SpecularColor.a);
73 surfColor += SpecularColor * intensity;
74
75 gl_FragColor = surfColor;
76 #else
77 gl_FragColor = distance;
78 #endif
79 }