In today's games, whether 2D or 3D, alpha-blending technology is usually used in pursuit of transparent light and shade effects. Next, we will talk about Alpha-Blending Technology and briefly introduce the details of alpha-blending technology.Algorithm.
The so-called alpha-blending is actually mixing the source and target pixels according to the value of the "Alpha" hybrid vector. For ease of understanding, let's talk about the alpha-blending process between two pixels.
The first step is to separate the RGB color components of the source pixel from the target pixel, and then multiply the three color components of the source pixel by the Alpha value, then, multiply the three color components of the target pixel by the inverse value of Alpha, and then add the results according to the corresponding color components, divide the final result of each component by the maximum value of alpha (usually this step is done by shift, which is also the reason why the maximum value of Alpha is always a power of two ), finally, the three color components are combined into a pixel output.
In this process, we usually use the Mask Method to separate the RGB color components in pixels. As for the specific three mask values: rmask, gmask, and bmask, you can get the result from ddpixelformat in DirectDraw (of course, you can also perform various flexible processing as needed ).
In the descriptive routine given below, we assume that the rmask, gmask, and bmask have stored the bitmask values of RGB three color components. The maximum Alpha value is 256, source and DEST are pointers to source and target pixels.
Int ialpha = 256-* Alpha; // The Reverse value of alpha
* DEST = (rmask & (* Source & rmask) ** Alpha + (* DEST & rmask) * ialpha)> 8 )) | (gmask & (* Source & gmask) ** Alpha + (* DEST & gmask) * ialpha)> 8 )) | (bmask & (* Source & bmask) ** Alpha + (* DEST & bmask) * ialpha)> 8 ));
finally, what is alpha-blending? It's very easy. Let's try programming. If you are interested, I will introduce several other mixed methods next time. Please look forward to "several common pixel mixing methods 』.