urce source source source source source source source source source source
This example shows how to use an alpha component in images. The
tree image is texture mapped onto a quad. If the image has an alpha
channel and opengl is set into the proper state we can make unwanted portions of
the image transparent.
Have a look at the tree image in an art package to see how the alpha channel works. Usually you can view the separate channels individually - the red, green and blue channels you will be familiar with. You'll notice an extra channel, this is the alpha channel -- areas we want to see are coloured white in this channel.
Press  and  to toggle the transparency on and off.
Alpha channels can also be used to control the level of blending - I might cover this is more detail later (I'm planning to add a HUD to my solar system program)
There isn't anything new at the start of our initialisation function. Read the previous examples to recap on the vertex array specifics.
init ( void )
 tgaGetColorEXT ( );
 glEnable ( GL_TEXTURE_2D );
 glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
 glGenTextures ( MAX_TEXTURES, tex_id );
 glBindTexture ( GL_TEXTURE_2D, tex_id[TREE_IMAGE] );
 tgaLoadImage ( "tree.tga", &tree, TGA_FREE );
 glEnable ( GL_DEPTH_TEST );
 glEnableClientState ( GL_VERTEX_ARRAY );
 glEnableClientState ( GL_TEXTURE_COORD_ARRAY );
 glClearColor ( 0.0, 0.5, 1.0, 1.0 );
 glVertexPointer ( 3, GL_FLOAT, 0, &vertices );
 glTexCoordPointer ( 2, GL_FLOAT, 0, &tex_coords );
Line 20 sets the alpha function, the 'cutoff' threshold can be adjusted to your requirements, read the manual pages for a full description of the arguments. Line 21 enables the alpha test.
 glAlphaFunc ( GL_GREATER, 0.3 );
 glEnable ( GL_ALPHA_TEST );
it! All the other functions remain the same. You'll notice that
there are some differences with keyboard interaction. We enable and
disable alpha testing here.
Website and content, Paul Groves