Pro OpenGL ES for iOS

(singke) #1

CHAPTER 10: OpenGL ES 2, Shaders, and... (^327)
vec4 diffuseColor = vec4(1.0, 1.0, 1.0, 1.0);
lightDirection=normalize(lightPosition);
float nDotVP = max(0.0, dot(normalDirection,lightDirection));
float nDotVPReflection = dot(reflect(-lightDirection,normalDirection),eyeNormal); //3
specular = pow(max(0.0,nDotVPReflection),shininess).75; //4
specularColorVarying=vec4(specular,specular,specular,0.0); //5
colorVarying = diffuseColor
nDotVP;
gl_Position = modelViewProjectionMatrix * position;
}
Here’s what is going on:
„ Line 1 declares a varying variable to hand the specular illumination off
to the fragment shader.
„ Next, in line 2, we get a normalized normal transformed by the
normalmatrix (yup, still sounds funny), which is needed to get the
proper specular value.
„ We now need to get the dot product of the reflection of the light and
the normalized normal multiplied normally by the normalmatrix in an
normal fashion. See line 3. Notice the use of the reflect() method,
which is another one of the niceties in the shader language. Reflect
generates a reflected vector based on the negative light direction and
the local normal. That is then dotted with the eyeNormal.
„ In Line 4 that dot value is taken and used to generate the actual
specular component. You will also see our old friend shininess, and
just as in version 1 of OpenGS ES, the higher the value, the narrower
the reflection.
„ Since we can consider the sun’s color just to be white, the
specular color in line 5 can be made to have all its components
set to the same value.
Now the fragment shader can be used to refine things even further, as shown in Listing
10-15.
Listing 10-15. The Fragment Shader That Handles the Specular Reflection
precision mediump float;
varying lowp vec4 colorVarying;
varying lowp vec4 specularColorVarying; //1
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;
uniform vec3 lightPosition;

Free download pdf