Game Development Reference
In-Depth Information
2.
Calculating the dot product of the S value and the Vertex Normal value. If this
value is less than or equal to zero, the value of the specular color amount is
0. Otherwise, raise this value to the power of the light shininess level. The
result is the final specular light value for that vertex.
The code in Listing 4-44 implements the following steps in order to find the specular value for the vertex.
1.
The EyeVec or EyeDir from the vertex to the viewer or eye position is
determined by subtracting the vertex position in world coordinates from the
Eye or Viewer position in world coordinates.
vec3 EyeDir = uEyePosition - WcVertexPos;
2.
The S vector is determined by adding the light vector or WcLightDir and the
eye vector or EyeDir using vector addition.
vec3 S = WcLightDir + EyeDir;
3.
The S vector is converted from world coordinates to eye coordinates by
multiplying it by the ViewMatrix.
vec3 EcS = normalize(vec3(uViewMatrix * vec4(S,1)));
4.
The specular lighting for the vertex is calculated by taking the dot product
between S and the Vertex Normal, taking the greater value between 0 and
the dot product to filter out negative results and raising the result to the
power of uLightShininess. uLightShininess is input to the vertex shader as
a uniform float variable that is defined by the programmer.
vSpecular = pow(max(dot(EcS, EcNormal), 0.0), uLightShininess);
Listing 4-44. Calculating the Specular Term in the Vertex Shader
// S = LightVector + EyeVector
// N = Vertex Normal
// max (S dot N, 0) ^ Shininess
vec3 EyeDir = uEyePosition - WcVertexPos;
vec3 S = WcLightDir + EyeDir;
vec3 EcS = normalize(vec3(uViewMatrix * vec4(S,1)));
vSpecular = pow(max(dot(EcS, EcNormal), 0.0), uLightShininess);
The vSpecular variable is then passed into the fragment shader as a varying variable:
varying float vSpecular;
Lighting in the Fragment Shader
I will now discuss the lighting in the fragment shader. Lighting will be divided into ambient, diffuse,
and specular components.
 
Search WWH ::




Custom Search