Original article: Lesson 8: Blending
Translated by: cker
The vast majority of effects in OpenGL are related to some types of (color) mixing. The definition of the mixed color is to combine the color of a certain pixel with the color of the corresponding pixel that has been drawn on the screen. How to combine these two colors depends on the component values of the alpha channel of the color and/or the mixed color function used. Alpha is usually the 4th color component at the end of the color value. In the previous lessons, we used gl_rgb to specify three components of the color. The corresponding gl_rgba can specify the Alpha component value. Furthermore, we can use glcolor4f () instead of glcolor3f ().
Most people think that the Alpha component represents the transparency of the material. This means that when the Alpha value is 0.0, the material is completely transparent. When the Alpha value is 1.0, the material is completely opaque.
8.1. mixed color Formula
If you are not interested in mathematics and want to see how to make it transparent, skip this section. If you want to deeply understand the working principle of (color) mixing, this section should be suitable for you. (TRANSLATOR: in fact, the basic principle of mixing is that after the colors and background colors of each pixel of the image to be divided are separated according to the RGB rules, after mixing the RGB color component * Alpha value + RGB color component * (1-Alpha value) of the background, finally, the RGB components obtained by the hybrid operation are re-merged .) The formula is as follows:
(RS Sr + rd dr, gs sg + gd dg, BS Sb + bd dB, as SA + AD da)
OpenGL calculates the mixed color of the two pixels according to the above formula. The lower-case s and R indicate the source pixel and the target pixel respectively. Uppercase S and D are the corresponding color mixing factors. These determine how you mix these pixels. In most cases, the Alpha values of different color channels are the same, so that the source pixels (as, as) and the target pixels (1, 1, 1, 1)-(as, ). The above formula is like the following:
(RS as + RD (1-As), GS as + Gd (1-As), BS as + BS (1-), as as + AD (1-))
This formula will generate transparent/translucent effects.
8.2. mixed colors in OpenGL
The steps for implementing color mixing in OpenGL are similar to the OpenGL process we mentioned earlier. Then, set the formula and disable the write depth cache when creating a transparent object. Because we want to draw objects behind a translucent image. This is not the correct color mixing method, but most of the time this method works well in simple projects.
Supplement of Rui Martins: The correct color mixing process should be to draw all the scenes before creating transparent images. In addition, the image must be drawn in the order opposite to the depth cache (first draw the farthest object ). Consider mixing two polygon (1 and 2) with Alpha, and different draw orders will produce different results. (Assuming that Polygon 1 is closest to the observer, draw polygon 2 and then polygon 1 in the correct process. As you can see in reality, the light from the two "Transparent" polygon always first passes through polygon 2 and then through polygon 1, finally, it reaches the observer's eyes ). When deep cache is enabled, you should sort transparent images by depth and draw these transparent objects after all scenes are drawn. Otherwise, you will get incorrect results. I know it is very painful to do so sometimes, but this is the right method.
We will use the code for Lesson 7. Add two new variables at the beginning of the Code. For clarity, I have rewritten the entire code segment.
# Include <windows. h> // windows header file
# Include <stdio. h> // header file of the standard input/output Library
# Include <Gl/Gl. h> // header file of the opengl32 Library
# Include <Gl/Glu. h> // header file of the glu32 Library
# Include <Gl/Glaux. h> // header file of the Glaux Library
Hglrc HRC = NULL; // permanent coloring description table
HDC = NULL; // Private GDI device description table
Hwnd = NULL; // save our window handle
Hinstance; // The instance that saves the program
Bool keys [256]; // Array Used for keyboard routines
Bool active = true; // The activity flag of the window. The default value is true.
Bool fullscreen = true; // The full screen flag is set to full screen by default.
Bool light; // light source on/off
Bool blend; // blending ON/OFF (new)
Bool LP; // is the L key pressed?
Bool FP; // is the F key pressed?
Glfloat xrot; // X Rotation
Glfloat yrot; // y Rotation
Glfloat xspeed; // X rotation speed
Glfloat yspeed; // y rotation speed
Glfloat Z =-5.0f; // The distance from the deep Screen
Glfloat lightambient [] = {0.5f}; // environment light Parameter
Glfloat lightdiffuse [] = {1.0f}; // diffuse light Parameter
Glfloat lightposition [] = {0.0f}; // Light Source Position
Gluint filter; // Filter Type
Gluint texture [3]; // storage space of 3 textures
Lresult callback wndproc (hwnd, uint, wparam, lparam); // wndproc Definition
Then move down to loadgltextures () here. Find the line "If (textureimage [0] = loadbmp (" Data/crate.bmp. We now use colored glass textures to replace the wooden case textures in the previous lesson.
If (textureimage [0] = loadbmp ("Data/glass.bmp") // load the glass Bitmap (modified)
Add the following two lines to the initgl () code snippet. The first line draws the object in full brightness and performs a 50% Alpha mixture (translucent) on it ). When the hybrid option is enabled, this object will produce a 50% transparent effect. Set the hybrid type in the second row. Supplement of Rui Martins: the alpha channel value is 0.0, which means the object material is completely transparent. 1.0 means that it is completely opaque.
Glcolor4f (1.0f, 1.0f, 1.0f, 0.5f); // brightness, 50% Alpha mixture (new)
Glblendfunc (gl_src_alpha, gl_one); // translucent hybrid function based on the Alpha Channel value of the source pixel (new)
Find the following code snippet near the end of Lesson 7.
If (Keys [vk_left]) // is the left direction key pressed?
{
Yspeed-= 0.01f; // If yes, yspeed is reduced.
}
Next, we add the following code. These lines monitor whether keys B are pressed. If yes, check whether the mixed options are enabled. Set it to the opposite state.
If (Keys [vk_left]) // is the left direction key pressed?
If (Keys ['B'] &! BP) // is BP false when B Jian is pressed?
{
BP = true; // If yes, BP is set to true
Blend =! Blend; // true/false for switching the mixed options
If (blend) // is it enabled together?
{
Glable (gl_blend); // open the hybrid
Gldisable (gl_depth_test); // disable the deep test.
}
Else // otherwise
{
Gldisable (gl_blend); // disable Hybrid
Glable (gl_depth_test); // enable the deep test.
}
}
If (! Keys ['B']) // is the key B released?
{
BP = false; // If yes, BP is set to false.
}
But how can we specify the color of the mixture when texture textures are used? It is very simple. When adjusting the texture mode, the color of each pixel in the texture is obtained by multiplying the alpha channel parameter with the current pixel color. For example, if the drawn color is (0.5, 0.6, 0.4), we will multiply the color (0.5, 0.6, 0.4, 0.2) (when the Alpha parameter is not specified, 1.0 by default ).
That's it. It is really easy to implement Alpha mixing Using OpenGL.
Original article note (11/13/1999)
I (nehe) mixed color code was modified to make the displayed object look more realistic. At the same time, the Alpha parameter is used to mix the source pixel and the target pixel, which will make the artificial trace of the object very obvious. It will make the back of the object look darker along the side. Basically, objects look weird. The color mixing method I used may not be the best, but it does work. After the light source is enabled, the object looks lifelike. Thanks to the original code provided by Tom, the mixed color method is correct, but the object does not look as attractive as expected.
The code is modified again because the gldepthmask () function has addressing problems on some graphics cards. This command does not seem very effective when enabling or disabling the deep buffer test on some cards, so I have converted the code for enabling or disabling the deep buffer test into old-fashioned glenable and gldisable.
8.3. Alpha mixing of texture maps
The Alpha parameter used for texture maps can be read from the problem maps like the color. The method is as follows. You need to obtain the Alpha parameter while loading the required material. Then, the gl_rgba color format is used when glteximage2d () is called.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lovetangtang/archive/2006/01/16/580568.aspx