The blending in OpenGL

Source: Internet
Author: User

prior to the use of mixed in the project, but the research is not in-depth, the recent art of a need to make me determined to re-delve into the mix and its  cocos2d-x in the use of, here to share to everyone. 
Blending ( Blend , some translation books call it blending, hereinafter referred to as blending, OpenGL , when an INPUT element passes through all relevant slice elements, it can be merged in some way with the current content in the color cache. The simplest and the default way is to overwrite the existing values directly, which is not actually called merging. In addition, we can also mix the existing color in the frame cache with the INPUT element color. This is the definition of the openGL process, from the perspective of drawing pictures, is actually the color of the upper picture and the color of the lower layer of the composition of the image, in general, the upper picture will completely cover the lower layer, but sometimes, in order to achieve some effect, The upper and lower layers of the picture color will have a different composition algorithm, in order to achieve these color mixing effect, not only the upper and lower layers of the color add minus so simple, set a few parameters

Target color: Dc Source color: Sc

Target Color Department Number: Ds Primary Colors Department Number: Ss

about target colors and primary colors Department The calculation method of the number, altogether has the following option:

Gl_zero (0,0,0)

Gl_one (1,1,1)

Gl_src_color (Rs,gs,bs)

Gl_one_minus_src_color (1-rs,1-gs,1-bs)

Gl_dst_color (RD,GD,BD)

Gl_one_minus_dst_color (1-RD,1-GD,1-BD)

Gl_src_alpha (As,as,as)

Gl_one_minus_src_alpha (1-as,1-as,1-as)

Gl_dst_alpha (Ad,ad,ad)

Gl_one_minus_dst_alpha (1-AD,1-AD,1-AD)

Gl_src_alpha_saturate (f,f,f) f=min (AS,1-AD)

the target color and the primary colors were multiplied by Department number calculation, the resulting two values will be calculated according to a certain method, you can get the last blend of colors, calculation methods include:

Gl_func_add ScSs + DcDs

Gl_func_subtract Scss-dcds

Gl_func_reverse_subtract Dcds-scss

Gl_min MIN (Scss,dcds)

Gl_max MAX (Scss,dcds)

It is necessary to note that, by default, the combination of the target color and the primary colors is in the Gl_func_add way.

It is also necessary to note that there are two other functions glblendfuncseparate and glblendequationseparate respectively set the RGB color and alpha blending method (the first function sets the blending parameters, The second parameter sets the blending method of the target color and the primary colors.

A complete hybrid process should include the following three steps:

Turn on blending

Glenable (Gl_blend);

Set the blending mode

Glblendequation (Gl_func_add);

To set how mixed parameters are generated

Glblendfunc (Sfactor, dfactor);

First use the Glenable setting to turn on blending, and then call Glblendequation to set the combination of the target and primary colors, and finally call Glblendfunc to set how the primary and target color coefficients are generated.

The following example will get a deeper understanding of the mix, one effect in Adobe's software is called filtering, which is mixed mode, which exists in the color blending mode, channel blending mode, and layer blending mode in the Lighten mode group. The blend effect is similar to the projection of multiple photographic slides on top of each other.

Comparison of the effects of using a filter and not using a color filter:


The left side is the blend effect after the filter is used.


The formula for filtering the color is:

Result color = 1-(1-Primary color) * (1-target color)

= 1-(1 + Primary Colors * Target color-primary Colors-target color)

= Primary Colors + target colors-Primary Colors * Target color

= 1 * Primary Colors + (1-Primary colors) * Target color

The coefficient of primary color is 1, the coefficient of the target colour is (1-primary color)

It is concluded that the coefficients of the target color and primary colors are:

BLENDFUNC.SRC = Gl_one

BLENDFUNC.DST = Gl_one_minus_src_color

Here's a little tip for using a filter, especially useful in Lua, where you don't need to create a blendfunc directly, you can get the object from the sprite and then modify it.

Local blendfunc = Sp:getblendfunc ()

BLENDFUNC.SRC = Gl_one

BLENDFUNC.DST = Gl_one_minus_src_color

Sp:setblendfunc (Blendfunc)


Insufficient capacity, limited level, if there are errors, welcome to point out.


The blending in OpenGL

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.