Use of color in objects

The final color of pixel is assigned in the fragment shader by setting the ESSL special variable gl_FragColor. If all the fragments in the object have the same color we can say that the object has a constant color. Otherwise, the object has a per-vertex color.

Constant coloring

To obtain a constant color we store the desired color in a uniform that is passed to the fragment shader. This uniform is usually called the object's diffuse material property. We can also combine object normals and light source information to obtain a Lambert coefficient. We can use the Lambert coefficient to proportionally change the reflecting color depending on the angle on which the light hits the object.

As shown in the following diagram, we lose depth perception when we do not use information about the normals to obtain a Lambert coefficient. Please notice that we are using a diffusive lighting model.

Usually constant coloring is indicated for objects that are going to become assets in a 3D game.

Constant coloring

Per-vertex coloring

In medical and engineering visualization applications, it is common to find color maps that are associated to the vertices of the models that we are rendering. These maps assign each vertex a color depending on its scalar value. An example of this idea is the temperature charts where we can see cold temperatures as blue and hot temperatures as red overlaid on a map.

To implement per-vertex coloring, we need to define an attribute that stores the color for the vertex in the vertex shader:

attribute vec4 aVertexColor;

The next step is to assign the aVertexColor attribute to a varying so it can be carried into the fragment shader. Remember that varyings are automatically interpolated. Therefore, each fragment will have a color that is the weighted contribution of the vertices surrounding it.

If we want our color map to be sensitive to lighting conditions we can multiply each vertex color by the diffuse component of the light. The result is then assigned to the varying that will transfer the result to the fragment shader as mentioned before. The following diagram shows two different possibilities for this case. On the left the vertex color is multiplied by the diffuse term of the light source without any weighting due to the light source relative position; on the right, the Lambert coefficient generates the expected shadows giving information about the relative location of the light source.

Per-vertex coloring

Note

Here we are using a Vertex Buffer object that is mapped to the Vertex Shader attribute aVertexColor. We learned how to map VBOs in the section Associating Attributes to VBOs discussed in Chapter 2, Rendering Geometry.

Per-fragment coloring

We could also assign a random color to each pixel of the object we are rendering. However, ESSL does not have a pre-built random function. Although there are algorithms that can be used to generate pseudo-random numbers, the purpose and the usefulness of this technique go beyond the scope of this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.23.30