Game Development Reference
In-Depth Information
Implementing Gouraud shading on a Lambertian
reflection model
Open the 02-Loading-Model-Gouraud-Lambert.html file in your favorite text
editor. We have covered only directional lights in our implementation for now and
will cover positional lights in Chapter 3 , Loading the Game Scene .
The shader code for the Gouraud shading for the Lambertian reflection model is
as follows:
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec3 vColor;
void main(void) {
gl_FragColor = vec4(vColor, 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;
uniform vec3 uAmbientColor;
uniform vec3 uLightingDirection;
uniform vec3 uDirectionalColor;
uniform vec3 materialDiffuseColor;
uniform vec3 materialAmbientColor;
varying vec3 vColor;
void main(void) {
gl_Position = pMatrix * mVMatrix * vec4(aVertexPosition, 1.0);
vec3 transformedNormal = vec3(nMatrix *
vec4(aVertexNormal,1.0));
vec3 normal=normalize(transformedNormal);
vec3 unitLightDirection = normalize(uLightingDirection);
float lambertTerm = max(dot(normal, -unitLightDirection), 0.0);
vColor = uAmbientColor*materialAmbientColor+
materialDiffuseColor * uDirectionalColor * lambertTerm;
}
</script>
 
Search WWH ::




Custom Search