Pro OpenGL ES for Android by Mike Smithwick & Mayank Verma

Pro OpenGL ES for Android by Mike Smithwick & Mayank Verma

Author:Mike Smithwick & Mayank Verma
Language: eng
Format: epub
Publisher: Apress®
Published: 2011-01-23T16:00:00+00:00


Blending Functions

To change the default blending, we must resort to using glBlendFunc(), which comes with two parameters. The first tells just what to do with the source, and the second, the destination. To picture what goes on, note that all that ultimately happens is that each of the RGBA source components is added, subtracted, or whatever, with each of the destination components. That is, the source's red channel is mixed with the destination's red channel, the source's green with the destination's green, and so on. This is usually expressed the following way: call the source RGBA values Rs, Gs, Bs, and As, and call the destination values Rd, Gd, Bd, and Ad. But we also need both source and destination blending factors, expressed as Sr, Sg, Sb, Sa, and Dr, Dg, Db, and Da. (It's not as complicated as it seems, really). And here's the formula for the final composite color:

(R,G,B) = ((Rs* Sr) + (Rd* Dr),(Gs* Sg) + (Gd*Dg),(Bs* Sb) + (Bd*Db))

In other words, multiply the source color by its blending factor and add it to the destination color multiplied by its blending factor.

One of the most common forms of blending is to overlay a translucent face on top of stuff that has already been drawn—that is, the destination. As before, that can be a simulated windowpane, a heads-up display for a flight simulator, or other graphics that just might look nicer when mixed with the existing imagery. (The latter is used a lot in Distant Suns for a number of the elements such as the constellation names, the outlines, and so on.) Depending on the purpose, you may want the overlay to be nearly opaque, using an alpha approaching 1.0, or very tenuous, with an alpha approaching 0.0.

In this basic blending task, the source's colors are first multiplied by the alpha value, its blending factor. So, if the source red is maxed out at 1.0 and the alpha is 0.75, the result is derived by simply multiplying 1.0 by 0.75. The same would be used for both green and blue. On the other hand, the destination colors are multiplied by 1.0 minus the source's alpha. Why? That effectively yields a composite color that can never exceed the maximum value of 1.0; otherwise, all sorts of color distortion could happen. Or imagine it this way: the source's alpha value is the proportion of the color “width” of 1.0 that the source is permitted to fill. The leftover space then becomes 1.0 minus the source's alpha. The larger the alpha, the greater the proportion of the source color that can be used, with an increasingly smaller proportion reserved for the destination color. So, as the alpha approaches 1.0, the greater the amount of the source color is copied to the frame buffer, replacing the destination color.

NOTE: In these examples, normalized color values are used because they make it much easier to follow the process instead of using unsigned bytes, which would express the colors from 0 to 255.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.