Tutorial 2b : Iterated alpha
 
Introduction

We're going to try something a little more advanced now, and we'll also learn about another of Glide's most important functions.

For this tutorial, we're going to draw two triangles. One will be red, rendered using the constant colour value, and the other will be rendered using iterated colours. In addition, we'll also be using iterated alpha scaling for both triangles.

In the previous tutorial we used the constant alpha value for scaling, but now we're going to use different alpha values for each vertex of the triangles and have Glide create a smooth transition, the same as it does with colours.

The steps involved are, initially:

  • Tell Glide to use iterated alpha values.
  • Tell Glide to scale colours based on the iterated alpha value.
Then, for the first triangle:
  • Tell Glide to use the constant colour value.
  • Specify the constant colour as red.
  • Specify the different alpha values for each vertex of the first triangle.
  • Draw the first triangle.
Finally, for the second triangle:
  • Tell Glide to use iterated colours.
  • Specify the different colour values for each vertex of the second triangle.
  • Specify the different alpha values for each vertex of the second triangle.
  • Draw the second triangle.
Configure the colour system

When we used the alpha value specified in the constant colour, all of our colour management could be handled by the grColorCombine function. Now that we want to use iterated alpha values, we must introduce the grAlphaCombine function.

You can think of grColorCombine as a function that deals primarily with colours but can also handle some basic alpha operations. For more advanced rendering, the grAlphaCombine function gives us access to much more powerful alpha operations.

The function takes five parameters. We want alpha values to be iterated between vertices, so we call the function as follows:

  grAlphaCombine(
    GR_COMBINE_FUNCTION_LOCAL_ALPHA,
    GR_COMBINE_FACTOR_NONE,
    GR_COMBINE_LOCAL_ITERATED,
    GR_COMBINE_OTHER_NONE,
    FXFALSE );
The first parameter, GR_COMBINE_FUNCTION_LOCAL_ALPHA, tells Glide to use the alpha value specified in the third parameter, which is local alpha. We specify GR_COMBINE_LOCAL_ITERATED for the third parameter because we want the alpha values to be iterated between vertices.

Note: the parameters for grAlphaCombine are analogous to those for grColorCombine. The first parameter tells Glide where to take the alpha value from. The second is the scaling factor that will be used if one is necessary. The third and fourth parameters are the local and other alpha sources.

We don't need to use a scaling factor so we specify GR_COMBINE_FACTOR_NONE for the second parameter, and we're not using the other alpha source so we specify GR_COMBINE_OTHER_NONE for the fourth parameter.

Tell Glide to scale colours based on the iterated alpha value, and to use the constant colour value

Telling Glide to use the constant colour value and to scale it based on iterated alpha values is done using the grColorCombine function. We call it as follows:

  grColorCombine(
    GR_COMBINE_FUNCTION_SCALE_OTHER,
    GR_COMBINE_FACTOR_LOCAL_ALPHA,
    GR_COMBINE_LOCAL_NONE,
    GR_COMBINE_OTHER_CONSTANT,
    FXFALSE );
The first parameter, GR_COMBINE_FUNCTION_SCALE_OTHER, means that the colour value we want to use is specified as the other colour, so for the fourth parameter we specify GR_COMBINE_OTHER_CONSTANT because we want to use the constant colour.

For the second parameter, the scaling factor, we use GR_COMBINE_FACTOR_LOCAL_ALPHA because we want Glide to scale colours based on an alpha value.

From looking at the second parameter, you would be forgiven for thinking that the alpha source would be specified as local, the third parameter, but that is not the case this time. When we use the GR_COMBINE_FACTOR_LOCAL_ALPHA setting, Glide uses the alpha source specified with the grAlphaCombine function, which we have already told to use iterated vertex alpha.

Specify the constant colour as red

The colours for both triangles that we're going to draw will be shaded by iterated alpha values, but the colour we want to use for the first one is solid red. As you would expect, this is done using the grConstantColorValue function:

  grConstantColorValue( colour_ARGB( 0, 255, 0, 0 ) );
Note: the constant colour's alpha value isn't being used, so we set it to zero.

Specify the different alpha values for each vertex of the first triangle

We define three vertices for the first triangle, using the x and y elements of the GrVertex structure. However, we want to specify an alpha value for each vertex, so we also use the alpha element, thus:

  GrVertex vertex;

  vertex.x = 100.0f;
  vertex.y = 200.0f;

  vertex.a = 255.0f;
Note: we're using the constant colour value, so we don't specify the GrVertex structure's red, green and blue values.

Draw the first triangle

We draw the first triangle in exactly the same way as we did in the previous tutorials:

  grDrawTriangle( &v1, &v2, &v3 );
Tell Glide to use iterated colours

For the second triangle, we still want to use iterated alpha values, but we also want to use iterated colour values instead of the constant colour. We do this with another call to grColorCombine, thus:

  grColorCombine(
    GR_COMBINE_FUNCTION_SCALE_OTHER,
    GR_COMBINE_FACTOR_LOCAL_ALPHA,
    GR_COMBINE_LOCAL_NONE,
    GR_COMBINE_OTHER_ITERATED,
    FXFALSE );
The only difference between this call to grColorCombine and the first call was that the fourth parameter, the other colour, has changed from GR_COMBINE_OTHER_CONSTANT to GR_COMBINE_OTHER_ITERATED, because we now want to use iterated colours.

Specify the different colour values for each vertex of the second triangle

For the second triangle, we need to specify different colours for each of the three vertices. We'll specify red for one, green for the second and blue for the third. This is done using the r, g and b elements of the GrVertex structure, so the red vertex would be specified as follows:

  GrVertex vertex;

  vertex.r = 255.0f;
  vertex.g =   0.0f;
  vertex.b =   0.0f;
Specify the different alpha values for each vertex of the second triangle

As with the first triangle, we need to define an alpha value for each vertex of the second triangle. This is done in exactly the same way:

  vertex.a = 255.0f;
Draw the second triangle

We draw the second triangle in exactly the same way as we did the first one:

  grDrawTriangle( &v1, &v2, &v3 );
Working example


Figure 2 : Scaling of constant and iterated colours using iterated alpha values.
Download the tutor_2b example code to see a working example that draws a red triangle on the left of the screen, smoothly shaded using iterated alpha values, and a triangle on the right rendered using iterated colours, smoothly shaded using iterated alpha values, as shown in Figure 2.

To compile the program, remove the example code from the previous tutorial and then insert the new one. You then compile the program as usual.

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.