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:
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:
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:
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:
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:
Draw the first triangle
We draw the first triangle in exactly the same way as we did in the previous tutorials:
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:
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:
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:
We draw the second triangle in exactly the same way as we did the first one:
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. |