Blend in OpenGL)

Source: Internet
Author: User

Blend is a technology that combines the source and target colors to generate special effects in some way. A mixture is often used to draw transparent or translucent objects. The α value that plays a key role in blending is actually mixing the source and target colors at a given ratio to achieve different levels of transparency. If the α value is 0, it is completely transparent, and if the α value is 1, it is completely opaque. The hybrid operation can only be performed in rgba mode, and the α value cannot be specified in color index mode. The Rendering sequence of objects affects the mixed processing of OpenGL.

 

Glable (gl_blend); // enable Hybrid

Gldisable (gl_blend); // disable hybrid access

 

Obtain mixed information

Glget (gl_blend_src );

Glget (gl_blend_dst );

Glisenable (gl_blend );

 

Glblendfunc (glenum sfactor, glenum dfactor); // mixed function

Sfactor source mixing factor

Dfactor target hybrid factor

Mixed factor Enumeration

Gl_dst_alpha

(AD, AD)

Gl_dst_color

(Rd, Gd, BD, AD)

Gl_one

(1, 1, 1)

Gl_one_minus_dst_alpha

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

Gl_one_minus_dst_color

(1, 1, 1)-(Rd, Gd, BD, AD)

Gl_one_minus_src_alpha

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

Gl_src_alpha

(As,)

Gl_src_alpha_saturate

(F, 1): F = min (AS, 1-AD)

Gl_zero

(0, 0, 0, 0)

 

Glblendfunc (gl_one, gl_zero); // The Source color overwrites the target color.

Glblendfunc (gl_zero, gl_one); // The target color will overwrite the source color.

Glblendfunc (gl_src_alpha, gl_one_minus_src_alpha ); //

If the source color is (1.0, 0.9, 0.7, 0.8)

Use gl_src_alpha as the source color

That is, 0.8*1.0, 0.8*0.9, 0.8*0.8, 0.8*0.7

Result 0.8, 0.72, 0.64, 0.56

 

The target color is (0.6, 0.5, 0.4, 0.3)

Use gl_one_minus_src_alpha for the target color

That is, 1-0.8 = 0.2

0.2*0.6, 0.2*0.5, 0.2*0.4, 0.2*0.3

Result 0.12, 0.1, 0.08, 0.06

Therefore, using this hybrid function, the α value of the source color determines the percentage of the result color.

Here, the Alpha value of the source color is 0.8, that is, the source color accounts for 80% of the result color, and the target color accounts for 20%.

 

Arrange the polygon from far to near, and use the following function

Glblendfunc (gl_src_alpha_saturate, gl_one );

Glable (gl_polygon_smooth );

The polygon anti-pattern can be optimized, but there must be an Alpha-bit plane to store the accumulated overwrite value.

 

Hybrid 3D object

When a 3D object is mixed, the basic principle is the same as that of a 2D object, but the deep detection must be disabled or set to read-only.

Because deep detection removes some blocked objects.

 

Glable (gl_depth_test); // enable deep Cache

Gldisable (gl_depth_test); // disable the deep cache.

 

Gldepthmask (gl_false); // The deep cache is read-only.

Gldepthmask (gl_true); // The deep cache is read/write.

 

To use hybrid and deep detection in 3D scenarios, follow these steps:

1. Use deep Detection

2. Draw opaque objects

3. Read-Only deep Detection

4. Draw a translucent object

5. Set read/write Depth Detection

 

The following describes how to test the hybrid algorithm and use the glblendfunc (gl_src_alpha, gl_one); mode.

Glclearcolor (0.0f, 1.0f, 0.0f, 0.5f); // green background (target), first put in Frame Buffer

Glcolor4f (1.0f, 0.0f, 0.0f, 0.5f); // rectangular color (source color), fragment

Calculation Result:

 

(0.5f, 1.0f, 0.0f, 0.75f)

Green background, red rectangle

Turn on the color when blending is disabled

 

Result:

1. the background color is also involved in the hybrid process;

2. The green background test shows that the value in rgba has no effect on a single color.

Transparency is calculated based on the value;

3. When the input color value is RGB, the default value of A is 0.0 for hybrid calculation.

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.