Game Development Reference
In-Depth Information
vec3 eyeVector=normalize(-vertexPos);
vec3 lightDirection = normalize(uLightingPosition);
float specular = 0.0;
float directionalLightWeighting = max(dot(normal, -
lightDirection), 0.0);
if(directionalLightWeighting>0.0)
{
vec3 halfDir = normalize(-lightDirection + eyeVector);
float specAngle = max(dot(halfDir, normal), 0.0);
specular = pow(specAngle, 4.0);
}
vec3 iColor = uAmbientColor*materialAmbientColor+
uDirectionalColor *materialDiffuseColor *
directionalLightWeighting+uSpecularColor*
materialSpecularColor*specular;
gl_FragColor = vec4(iColor, 1.0);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
uniform mat4 mVMatrix;
uniform mat4 pMatrix;
uniform mat4 nMatrix;
varying vec3 transformedNormal;
varying vec3 vertexPos;
void main(void) {
vec4 vertexPos4 = mVMatrix * vec4(aVertexPosition, 1.0);
vertexPos = vertexPos4.xyz;
transformedNormal = vec3(nMatrix * vec4(aVertexNormal,1.0));
gl_Position= pMatrix *vertexPos4;
}
</script>
As discussed earlier, in Phong shading, the normals are calculated after the object
is rasterized and calculations are performed for every pixel. Hence, we calculate
the vertex and the normal transformations in the vertex shader and then add
the transformed values to the fragment shader. Then, the color calculation of the
Lambert and Blinn terms are performed in the fragment shader.
 
Search WWH ::




Custom Search