Tutorial 2a : Introducing alpha
 
Introduction

As we've seen in the earlier tutorials, Glide thinks of a colour as having four properties - red, green, blue and alpha. So far we've only used the red, green and blue, but now we're going to look at alpha.

We can think of a colour's alpha value as being just an extra piece of information that we use to help the Voodoo card do something to the colour before it goes to the rendering buffer. As simple as that may sound, it is actually the principle at the heart of Glide's most powerful visual effects.

To see an alpha value in action, let's draw two triangles - one bright red, one dark red. We'll configure Glide's colour system so that the colour of the triangles will be taken from the constant colour, but will be modified by the constant colour's alpha value.

The steps involved are:

  • Configure the colour system.
  • Set the constant colour to be red with maximum alpha.
  • Draw the first triangle.
  • Set the constant colour to be red with half alpha.
  • Draw the second triangle.
Configure the colour system

We want Glide to take all of the colour and alpha information from the constant colour, as specified using the grConstantColorValue function. To create the colour, we'll be using the colour_ARGB function that we created in an earlier tutorial.

What is important is the way that the colour information is modified by the alpha value. We want the alpha value to scale the colour, with 0 representing maximum darkness and 255 representing maximum brightness. An alpha value of 127 will scale the specified colour to roughly 50%.

We still use the grColorCombine function, as follows:

  grColorCombine(
    GR_COMBINE_FUNCTION_SCALE_OTHER,
    GR_COMBINE_FACTOR_OTHER_ALPHA,
    GR_COMBINE_LOCAL_NONE,
    GR_COMBINE_OTHER_CONSTANT,
    FXFALSE );
The first parameter, GR_COMBINE_FUNCTION_SCALE_OTHER, tells Glide that it should take the colour value from the source specified as other, the fourth parameter. We set the fourth parameter to GR_COMBINE_OTHER_CONSTANT which means that the constant colour will be used.

The first parameter also tells Glide that the colour should be scaled.

The second parameter specifies what the scaling factor should be. We use GR_COMBINE_FACTOR_OTHER_ALPHA because we want Glide to take the alpha value from the source specified in other, meaning that the alpha value is being taken from the same source as the colour information - the constant colour.

Note: there is no need to tell Glide where the colour scaling factor is defined. Colour scaling factors are always specified in the second parameter of the grColorCombine function.

Let's look at this process as a flowchart. Suppose we've got the constant colour set to be red with an alpha value of 127, roughly half of the maximum brightness. The process would work as follows:

"grColorCombine" flow chart

As you can see, we use the constant colour (red) as the only colour source. After it has been combined with the other colour (which is non-existent) the result is still red, but it is then scaled using the constant colour's alpha value, and it becomes dark red. It then remains unmodified throughout the rest of the process, and the end result is dark red.

Set the constant colour to be red with maximum alpha

We want the first triangle to be bright red, so we set the constant colour to be bright red with the maximum alpha value of 255, meaning that the colour won't be darkened at all. We do this as follows:

  grConstantColorValue( colour_ARGB( 255, 255, 0, 0 ) );
Note: remember that the first parameter passed to the colour_ARGB function is the alpha value, with the next three being red, green and blue.

Draw the first triangle

As you would expect, we define a set of three vertices using just the x and y elements in each, and then use the grDrawTriangle function to draw the triangle:

  GrVertex v1, v2, v3;

   [ ... define vertices ... ]

  grDrawTriangle( &v1, &v2, &v3 );
The triangle will be drawn using the unmodified constant colour, red.

Set the constant colour to be red with half alpha

We want the second triangle to be dark red, but we still set the constant colour to be bright red. This time, though, we set the alpha value to be 127 (approximately half of the maximum 255), meaning that the red colour will be scaled by roughly half, darkening it:

  grConstantColorValue( colour_ARGB( 127, 255, 0, 0 ) );
Draw the second triangle

Finally, we define another set of three vertices for the second triangle, still using just the x and y elements in each, and then use the grDrawTriangle function to draw the triangle:

  GrVertex v1, v2, v3;

   [ ... define vertices ... ]

  grDrawTriangle( &v1, &v2, &v3 );
The triangle will be drawn using the constant colour, red, but it will have been darkened by roughly half because of the constant alpha value.

Working example


Figure 1 : Scaling of the constant colour using the constant alpha value.
Download the tutor_2a example code to see a working example that draws a bright red triangle on the left of the screen and a dark red triangle on the right, as shown in Figure 1.

To compile the program, either create a new project workspace for this tutorial or 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.