Tutorial 1e : More advanced colour
 
Introduction

In previous examples, we used the GrVertex structure only to specify vertex positions, but it can also be used to specify colour values. Before, we were only interested in using one colour at a time, so it made sense to use the constant colour, but now we'll use colour transitions, or "iteration".

Colour iteration is when each vertex of a shape has its own colour and the colours are blended together across the surface of the shape.

To use colour iteration, our program must:

  • Specify the colour for each vertex.
  • Tell Glide to iterate the colours between each vertex.
Specify the colour for each vertex

Before, we used only the x and y elements of the GrVertex structure. Now we're going to use the red, green, blue and alpha elements, shortened to r, g, b and a. Each can be in the range 0 - 255 so we still have the same range of colours available to us as we did with the constant colour.

To create a green vertex at position (100, 200) the code would be:

  GrVertex vertex;
  
  vertex.x = 100.0f;
  vertex.y = 200.0f;
  
  vertex.r =   0.0f;
  vertex.g = 255.0f;
  vertex.b =   0.0f;
  vertex.a =   0.0f;
Note: unlike with the constant colour value, when we provided each of the red, green, blue and alpha elements as 8-bit values, combined in one 32-bit value, the GrVertex structure requires that they be given as four separate floating point values.

Tell Glide to iterate the colours between each vertex

We now need to let Glide know where it should be taking its colour values from. As you'd expect, this is done using the grColorCombine function.

When we were using the constant colour value, we used the function as follows:

  grColorCombine(
    GR_COMBINE_FUNCTION_LOCAL,
    GR_COMBINE_FACTOR_NONE,
    GR_COMBINE_LOCAL_CONSTANT,
    GR_COMBINE_OTHER_NONE,
    FXFALSE );
Remember how the first parameter tells Glide to use the colour specified in the third parameter (the local value), which we specified as the constant colour? The only difference now is that we want to use iterated vertex colours instead of the constant colour value, so we specify GR_COMBINE_LOCAL_ITERATED for the third parameter, thus:
  grColorCombine(
    GR_COMBINE_FUNCTION_LOCAL,
    GR_COMBINE_FACTOR_NONE,
    GR_COMBINE_LOCAL_ITERATED,
    GR_COMBINE_OTHER_NONE,
    FXFALSE );
Note: we haven't changed either the combining method (first parameter) or the combining factor (second parameter). We don't need to tell the Voodoo card how the colours should be iterated - it does it all internally without any help from us.

Working example


Figure 9 : Smooth colour iteration.
Download the tutor_1e example code to see a working example that will draw a triangle with different colours for each vertex and some lines with different colours for each end, as shown in Figure 9.

Notice how the program is structured:

  1. After the grSstWinOpen function, we call grColorCombine as described earlier. At that stage, we haven't created any vertices.

  2. Before the shapes are drawn in the draw_lines and draw_triangle functions, different colours are specified for each vertex. The vertex colours are specified at the same time as the co-ordinates, although this isn't necessary - it could be done at any point prior to drawing the shape.

  3. When the shapes are drawn, Glide knows to take the colour values from the vertices and to use iteration to blend them together.

Next Tutorial | Main Page
 
This tutorial is ©1998 by Andrew Smith. No part of this tutorial may be reproduced without permission. If you want to reproduce any of this tutorial for non-commercial purposes then I'm not likely to try and stop you, but please ask me first.