132001f49Smrg//
232001f49Smrg// Fragment shader for procedurally generated toy ball
332001f49Smrg//
432001f49Smrg// Author: Bill Licea-Kane
532001f49Smrg//
632001f49Smrg// Copyright (c) 2002-2003 ATI Research 
732001f49Smrg//
832001f49Smrg// See ATI-License.txt for license information
932001f49Smrg//
1032001f49Smrg
1132001f49Smrgvarying vec4 ECposition;   // surface position in eye coordinates
1232001f49Smrgvarying vec4 ECballCenter; // ball center in eye coordinates
1332001f49Smrg
1432001f49Smrguniform vec4  LightDir;     // light direction, should be normalized
1532001f49Smrguniform vec4  HVector;      // reflection vector for infinite light source
1632001f49Smrguniform vec4  SpecularColor;
1732001f49Smrguniform vec4  Red, Yellow, Blue;
1832001f49Smrg
1932001f49Smrguniform vec4  HalfSpace0;   // half-spaces used to define star pattern
2032001f49Smrguniform vec4  HalfSpace1;
2132001f49Smrguniform vec4  HalfSpace2;
2232001f49Smrguniform vec4  HalfSpace3;
2332001f49Smrguniform vec4  HalfSpace4;
2432001f49Smrg
2532001f49Smrguniform float InOrOutInit;  // = -3
2632001f49Smrguniform float StripeWidth;  // = 0.3
2732001f49Smrguniform float FWidth;       // = 0.005
2832001f49Smrg
2932001f49Smrgvoid main()
3032001f49Smrg{
3132001f49Smrg    vec4  normal;              // Analytically computed normal
3232001f49Smrg    vec4  p;                   // Point in shader space
3332001f49Smrg    vec4  surfColor;           // Computed color of the surface
3432001f49Smrg    float intensity;           // Computed light intensity
3532001f49Smrg    vec4  distance;            // Computed distance values
3632001f49Smrg    float inorout;             // Counter for computing star pattern
3732001f49Smrg
3832001f49Smrg    p.xyz = normalize(ECposition.xyz - ECballCenter.xyz);    // Calculate p
3932001f49Smrg    p.w   = 1.0;
4032001f49Smrg
4132001f49Smrg    inorout = InOrOutInit;     // initialize inorout to -3
4232001f49Smrg
4332001f49Smrg    distance[0] = dot(p, HalfSpace0);
4432001f49Smrg    distance[1] = dot(p, HalfSpace1);
4532001f49Smrg    distance[2] = dot(p, HalfSpace2);
4632001f49Smrg    distance[3] = dot(p, HalfSpace3);
4732001f49Smrg
4832001f49Smrg    distance = smoothstep(-FWidth, FWidth, distance);
4932001f49Smrg    inorout += dot(distance, vec4(1.0));
5032001f49Smrg
5132001f49Smrg    distance.x = dot(p, HalfSpace4);
5232001f49Smrg    distance.y = StripeWidth - abs(p.z);
5332001f49Smrg    distance = smoothstep(-FWidth, FWidth, distance);
5432001f49Smrg    inorout += distance.x;
5532001f49Smrg
5632001f49Smrg    inorout = clamp(inorout, 0.0, 1.0);
5732001f49Smrg
5832001f49Smrg    surfColor = mix(Yellow, Red, inorout);
5932001f49Smrg    surfColor = mix(surfColor, Blue, distance.y);
6032001f49Smrg
6132001f49Smrg    // normal = point on surface for sphere at (0,0,0)
6232001f49Smrg    normal = p;
6332001f49Smrg
6432001f49Smrg    // Per fragment diffuse lighting
6532001f49Smrg    intensity  = 0.2; // ambient
6632001f49Smrg    intensity += 0.8 * clamp(dot(LightDir, normal), 0.0, 1.0);
6732001f49Smrg    surfColor *= intensity;
6832001f49Smrg
6932001f49Smrg    // Per fragment specular lighting
7032001f49Smrg    intensity  = clamp(dot(HVector, normal), 0.0, 1.0);
7132001f49Smrg    intensity  = pow(intensity, SpecularColor.a);
7232001f49Smrg    surfColor += SpecularColor * intensity;
7332001f49Smrg
7432001f49Smrg    gl_FragColor = surfColor;
7532001f49Smrg}
76