Pro OpenGL ES for iOS

(singke) #1

322 CHAPTER 10: OpenGL ES 2, Shaders, and...^


float nDotVP = max(0.0, dot(normalDirection, normalize(lightPosition)));

colorVarying = diffuseColor * nDotVP;

gl_Position = modelViewProjectionMatrix * position;
}
This is almost identical to Apple’s template, but we’ve added a couple of things:
„ Line 1 serves to pass an additional attribute, namely, the texture
coordinates for each vertex. This is then passed straight through to the
fragment shader via line 3 using the v_texCoord varying.
„ In line 2 is the new uniform you may recall in the view controller’s code
that holds the position of the light.
Listing 10-11 shows the fragment shader for the daylight side of the earth, while Listing
10-12 does the same but for the night side.
Listing 10-11. The Fragment Shader for the Daylight Side of the Earth

varying lowp vec4 colorVarying; //1

precision mediump float;
varying vec2 v_texCoord; //2
uniform sampler2D s_texture; //3

void main()
{
gl_FragColor = texture2D( s_texture, v_texCoord )*colorVarying; //4
}
You can see how simple these are for such beautiful results:
„ Line 1 picks up the varying variable, colorVarying, from the vertex
shader.
„ Line 2 does the same for the texture coordinates, followed by line 3
that has the texture. The sampler2D, as shown in line 3, is a built-in
uniform variable that points out which texture unit is being used.
„ Finally, in line 4, the built-in function texture2D extracts the value from
the texture referenced by s_texture at the coordinates of v_texCoord.
That value is then multiplied by colorVarying, the ‘‘real’’ color of the
fragment. The less the colorVarying is, the darker the color becomes.
Listing 10-12 shows how to do the night side of the earth.
Listing 10-12. Rendering the Night Side

varying lowp vec4 colorVarying;

precision mediump float;
varying vec2 v_texCoord;
uniform sampler2D s_texture;
Free download pdf