328 CHAPTER 10: OpenGL ES 2, Shaders, and...^
varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main()
{
vec4 finalSpecular=vec4(0,0,0,1);
vec4 surfaceColor;
float halfBlue;
surfaceColor=texture2D( s_texture, v_texCoord );
halfBlue=0.5*surfaceColor[2]; //2
if(halfBlue>1.0) //3
halfBlue=1.0;
if((surfaceColor[0]<halfBlue) && (surfaceColor[1]<halfBlue)) //4
finalSpecular=specularColorVarying;
gl_FragColor = surfaceColor*colorVarying+colorVarying*finalSpecular; //5
}
The main task here is to determine which fragments represent sea and which do not. It’s
pretty easy: the blue stuff is water (powerful wet stuff, that water!), and everything that
isn’t, isn’t.
First in line 1, we pick up the specularColorVarying variable.
In line 2, we pick up the blue component and divide it by half,
clamping in line 3, since no color can actually go above full intensity.
Line 4 does the filtering. If the red and green components were both
less than half that of the blue, then it’s a pretty safe bet that we can
draw the specular glint over the water, instead of some place like
Chad.
The specular piece is now added to the fragment color in the last line,
after first multiplying it with the colorVarying, because that will
modulate it with everything else.
Figure 10-5 shows the results.