Color Blending function in OpenGL (i)

Source: Internet
Author: User
Tags transparent color

color Blending function in OpenGL (i)
        We know that material properties and illumination parameters can greatly increase the fidelity of a graphic, but in addition to this, many of the effects are achieved by blending colors when we model the real world. A transparent object, such as a glass cup, is a mixture of the light emitted from behind it and the color of the transparent object. This transparency is implemented in OpenGL by first drawing the background object and then mixing the foreground object (such as the Water Cup) with the color already present in the color buffer. In this process, the alpha-value component of the color plays an important role.
blending features of colorsin general, OpenGL stores the color value in the color buffer during rendering, storing the depth value of each fragment (pixel) in the depth buffer. When depth detection is turned off, the new color value simply overrides the color value that already exists in the color buffer, and when depth detection is turned on, the new color value replaces the original color only if it is closer to the adjacent clipping plane than the original color. Of course, this is the case when OpenGL's hybrid feature is turned off. When the blending feature is enabled, the new color is combined with the color in the color buffer. By making different combinations of these colors, you can produce many different kinds of effects.
before we introduce color blending, we need to first clarify the concept of both the target color and the source color terms. First, the color already stored in the color buffer is called the target color, which contains a separate red, green, and blue component and an optional alpha value. Second, the color that enters the color buffer as the result of the current render command is called the source color, and it also contains four color components (red, green, blue, and an optional alpha-valued ingredient). It is through the different combination of the target color and the source color that we can achieve the function of color blending. To enable the blending feature:
glenable (gl_blend);
When the blending feature is enabled, the combination of the source and target colors is controlled by a mixed equation. By default, the mixing equation used is as follows:          
wherein, CF is the final calculation of the resulting color, CS is the source color, CD is the target color. S is the source blending factor, and D is the target blending factor. These two mixing factors can be set by the following function:
Glblendfunc (Glenum S, Glenum D);
As you can see, formal parameters S and D are enumerated values, not actual values that can be directly specified. Just a little vague about the concept, let's illustrate the practical application of these concepts through a common example of mixed functions:
//Set source blending factor and target blending factor Glblendfunc (Gl_src_alpha, gl_one_minus_src_alpha);
according to the blending function above, if the target color in the color buffer is red (1.0f,0.0f, 0.0f, 0.0f), the source color is a blue (0.0f, 0.0f, 1.0, 0.6f), its alpha value is 0.6, then the final color calculated is:
Cd = target color = (1.0f,0.0f, 0.0f, 0.0f)Cs = Source color = (0.0f, 0.0f, 1.0, 0.6f)S = source color alpha value = 0.6D = 1-source color alpha value = 0.4So, according to the mixed equation:
     
equivalent to: Cf = (Blue * 0.6) + (RED * 0.4)The resulting color is the combination of the target color red and the source color blue, and the higher the alpha value of the source color, the more source color components are added, and the fewer ingredients the target color retains. This mixing function is often used to draw the effect of transparent objects in front of some opaque objects. This technique requires a background object to be drawn first, and then a mixture of transparent objects. The effect will surprise you:
     
Let's take a look at the idea of how the Reflection sample program is made through the code:
void renderscene ()      {
// empty color buffers and depth buffers Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);
// -----------------------First draw a sphere underneath the floor-------------------- // save matrix status Glpushmatrix (); // Place the light source 0 below the floor GLLIGHTFV (gl_light0, gl_position, flightposmirror); // save matrix status Glpushmatrix (); // set the clockwise side to the front of the polygon, mirroring the polygon Glfrontface (GL_CW); // Use the scaling factor of 1 on the y-axis to invert the y-axis Glscalef (1.0f, -1.0f, 1.0f); // draw a sphere below the floor Drawsphere (); //Set the side of the counterclockwise wrap to the front of the polygon Glfrontface (GL_CCW); // restore matrix status Glpopmatrix ();
// -----------------to achieve the illusion of reflection by setting a transparent color blending effect on the floor----------------- // Turn off light calculation gldisable (gl_lighting); // turn on color blending glenable (gl_blend); // Set blend factor Glblendfunc (Gl_src_alpha, gl_one_minus_src_alpha); // floor drawing Drawground (); // Turn off color blending gldisable (gl_blend); // turn on lighting calculation glenable (gl_lighting);
// --------------------finally draw a sphere above the floor--------------------------- // Place the light source 0 on the floor GLLIGHTFV (gl_light0, gl_position, flightpos); // draw a sphere above the floor Drawsphere (); // restore matrix status Glpopmatrix ();
// execution Buffer Exchange glutswapbuffers ();
     }
when we are drawing the scene, we first draw the sphere below the floor in a upside-down manner, then open the blend, draw a transparent layer on top of the sphere, and finally restore the inverted coordinate system, drawing the sphere above the floor. After the completion of these three parts, the illusion of a floor reflecting the sphere is complete:
                 The complete code for this part of the sample program has been placed on GitHub, and a friend in need can refer to it (https://github.com/dxm3dp/OpenGL-06-Reflection Click the open link ).

This article is from Du Xiaomeng's blog, do not use for any commercial purposes, reproduced please maintain integrity and identify the source: Http://blog.csdn.net/haohan_meng

Color Blending function in OpenGL (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.