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:
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:
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:
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:
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:
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:
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:
Working example
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. |