Lesson 14: Advanced Blending"Blending": A word that strikes fear into the hearts of the bravest warriors. Just mutter "blending" and many will cower in fear. Okay, not really. But now that you've learned a bit more, I'm going to go over some more advanced facets of blending. So there. TheoryAs you have previously learned, there are many factors that can be supplied to glBlendFunc(). Some factors can only be used as a source factor, some can only be used as a destination factor, and some can be used for either. So, without further ado, here are some informative tables for your learning pleasure explaining which factors can be used for what and what they mean - the information in these tables are, by the way, taken more or less directly from the OpenGL Red Book. For help deciphering these tables, look back to this paragraph from lesson 8: Anyhow, how are these factors used? Each blended pixel comes from the following equation: Sr, Sg, Sb, and Sa are the source factors specified in the blending function; Rd, Gd, Bd, and Ad are the color components of the destination pixel; and Dr, Dg, Db, and Da are the destination factors specified in the blending function.
As you can tell, there are a whole lot of different combinations of blending factors. Several of them end up looking similar or completely alike, and many of them have no real practical use. In fact, for the most part, there are only two blending modes that most games will ever use - translucent blending and additive blending. Translucent blending is represented by the factor pair (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). This type of blending is often used to render translucent objects, such as windows or water particles. Additive blending is represented by the factor pair (GL_SRC_ALPHA, GL_ONE). This type of blending is often used in particle engines where the particles have to be "glowing" - this effect can be used for sparks, fire, blasts of glowy magic, etc. It's called additive blending because, basically, the source color is added to whatever colour already exists, then clamped to 1.0. ImplementationThe following program allows you to cycle through every single blending mode available and see firsthand what they look like. There are three textures - a background checkerboard pattern which is drawn without blending, the layer above it, which acts as a sort of background and is drawn with translucency over the background, and the top layer, which is drawn with the blending function that you tell it to be drawn with. Since the example runs in 32 bit color, you should probably set your desktop color depth to 32 if it isn't set to that already. In case you didn't download the full issue, the three textures can be found here, here and here. Listing 14-1: agl14.cpp
Finally something long and interesting :) You'll notice that I actually commented this one, too. Anyhow, in the example program, you can use the arrow keys left and right to adjust the alpha of the top layer, the arrow keys up and down to adjust the alpha of the lower layer, 'S' to change the source blending factor, and 'D' to change the destination blending factor. You can just keep pressing 'S' to cycle through all the modes, because once the source factor loops, it increments the destination factor as well. Get it? :) Analyzing agl14.cppFirst, we declare three texture handles, their file names, a font to use, and then the blending modes and their names for both source and destination factors are listed. Then we set up everything in Init(). This code should look pretty familiar to you. In the main function, quit is used to determine whether or not to quit the main loop, src_f and dst_f are the factors to be used for the source and destination blending factors, layer_alpha is an array of the alpha values used for each layer, and key is used to store the current keypress. In the main loop, the color buffer is cleared, then the three layers are drawn. The if() construct inside the for() loop that draws the three layers is used to determine which blending mode to use for each layer. The bottom checkerboard layer is drawn with no blending, the middle layer is drawn with transparency, and the top layer is drawn with whatever blending mode has been selected. The reason the middle layer is drawn transparent is because some blending modes actually rely on the alpha level of the destination pixel. This is also why we are running the program in 32-bit color mode. Next, information about the selected blending modes is displayed, and we call glFlush() to flush the layers to the frame buffer. Then, the blending modes and the alpha levels of the top and middle layers are changed based on any keys that have been pressed. Simple, no? In the Next Lesson...In the next lesson, you will learn about camera movement, a way of translating and rotating a scene to make it look as if your perspective is changing.
|
||||||||||||||||||||||||||||||||||||