ch6_Cube.html
using your HTML5 Internet browser. You will see a page like the one shown in the following screenshot:In this exercise, we are going to compare constant versus per-vertex coloring. Let's talk about the page's widgets:
uAlpha
in the vertex shader. uAlpha
sets the alpha value for the vertex color. uUseLambert
. The code that calculates the Lambert coefficient can be found in the vertex shader included in the page:float lambertTerm = 1.0; if (uUseLambert){ //Transformed normal position vec3 normal = vec3(uNMatrix * vec4(aVertexNormal, 1.0)); //light direction: pointing at the origin vec3 lightDirection = normalize(-uLightPosition); //weighting factor lambertTerm = max(dot(normal,-lightDirection),0.20); }
uUseLambert
is false, then lambertTerm
keeps being 1.0
and then it will not affect the final diffuse term which is calculated later on:Id = uLightDiffuse * uMaterialDiffuse * lambertTerm;
Id
will have the Lambert coefficient factored in. uUseVertexColors
and the aVertexColor
attribute. This fragment is shown here:if (uUseVertexColor){ Id = uLightDiffuse * aVertexColor * lambertTerm; } else { Id = uLightDiffuse * uMaterialDiffuse * lambertTerm; }
Take a look at the file /models/simpleCube.js.
There, the eight vertices of the cube are defined in the vertices
array and there is an element in the scalars
array for every vertex. As you may expect, each one of these elements correspond to the respective vertex color, as shown in the following diagram:
/models/complexCube.js
, we can achieve independent face coloring. The following diagram explains how the vertices are organized in complexCube.js
. Also note that as the definition of colors occurs by vertex (as we are using the shader
attribute), we need to repeat each color four times, because each face has four vertices. This idea is depicted in the following diagram: 1.0
. For that, click-and-drag the slider to the left that appears at the bottom of the page. What do you see? Please notice that the object does not become transparent but instead it starts losing its color. To obtain transparency, we need to activate blending. We will discuss blending in depth later in this chapter. For now, uncomment these lines in the configure
function, in the source code://gl.disable(gl.DEPTH_TEST); //gl.enable(gl.BLEND); //gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
0.25
you will see something like the following screenshot:We have studied two different ways for coloring objects: constant coloring and per-vertex coloring. In both cases, the final color for each fragment is assigned by using the fragment shader gl_FragColor
variable.
We also saw how, by activating the calculation of the Lambert coefficient, we can obtain sensory depth information.
By repeating vertices in our object, we can obtain different coloring effects. For instance, we can color an object by faces instead of doing it by vertices.
3.136.97.64