Chapter 6 OpenGL programming guide: mixing, anti-sawtooth, fog, polygon offset

Source: Internet
Author: User
1. Hybrid (blending)

After the elements are raked, fragment is written to framebuffer. The fragment and the corresponding framebuffer pixels are operated to form a new pixel color. Use gl_blend to activate the hybrid function. Otherwise, fragmen overwrites the corresponding pixel.

The Alpha component value of the color can be used at this time.

Blend factor)

In the hybrid operation, the fragment color is called the source, and the pixel color is called the destination. The source and target operations must be multiplied by a factor: Sr, SG, Sb, SA, Dr, DG, DB, Da, the formula for mixed operation can be expressed as: (RS * Sr + RD * Dr, GS * SG + GD * DG, BS * Sb + bd * dB, as * Sa + Ad * Da ). The final calculation result is pinned in the range of [0, 1.

Two APIs can be used to set this factor: glblendfunc (glenum srcfactor, glenum destfactor) and glblendfuncseparate (glenum srcrgb, glenum destrgb, glenum srcalpha, glenum destalpha ), the difference between the latter is that the Alpha component can be set separately. Parameters are enumerated values. For more information about the list of parameter values and their meanings, see the original document.

When setting a factor, some parameter values with the constant character need another color constant for calculation. This constant uses glblendcolor (glclampf red, glclampf green, glclampf blue, glclampf alpha.

Blend Equation)

The arithmetic operation used in the previous section is addition, which can also be changed. glblendequation (glenum mode); glblendequationseparate (glenum modergb, glenum modealpha) can be used to set arithmetic operations, the distinction between the two APIS is clear at a glance. For specific parameters, see the original document.

Hybrid use case

This section describes some use cases that use blend to achieve a certain effect. Generally, one effect can be achieved in multiple ways:

1) evenly mix two images: Set the source factor to gl_one, set the target factor to gl_zero, and draw the first image to the frame buffer. Then set the source factor to gl_src_alpha, set the target factor to gl_one_minus_alpha, and draw the second image to the frame buffer with Alpha 0.5. By adjusting the Alpha value, you can control the proportion of the two images.

2) evenly mix three images: Clear framebuffer, set the source factor to 0.333333333, set the target factor to gl_src_alpha, and draw three images with Alpha 0.5 in sequence.

3) filter effect: Set the source factor to gl_dst_color or gl_one_minus_dst_color and set the target factor to gl_src_color or gl_one_minus_src_color to adjust the color components of the image. (My understanding) for example
Buffer is cleared to (0.4, 0.3, 0.2, 1.0), and then the source and target factors are set to gl_dst_color and gl_src_color respectively, the rendering effect is red 80%, green 60%, blue 40%.

4) Non-rectangular raster: You can perform irregular raster operations on images by mixing them. For example, you can draw a polygon of any shape and set the internal color Alpha to 1.0, the external color Alpha is set to 0. After the image is drawn, the image can be displayed only inside the polygon through the control parameters. Billboarding technology is based on this.

5) Anti-sawtooth: This will be discussed later.

Mixing and order

The visual effects of two overlapping transparent objects are related to the order in which they are drawn.

In a three-dimensional model, if depthbuffer is enabled and a translucent object is closer to the viewpoint, gldepthmask (gl_false) can temporarily convert the depth buffer to readonly, therefore, this should be done when you draw a translucent object, so that the objects behind the translucent object can have the opportunity to draw.

However, in any case, I think the order of drawing will affect the final visual effect.

2. The anti-sawtooth screen is essentially a pixel matrix. After all, pixels are squares of a certain size, which means that a set of pixels in a straight line cannot be strictly in a straight line. Observe the straight lines on the screen, and you will find the result of a sawtooth line, especially when the straight line is vertical or almost flat.

Line, point, and polygonEnable this feature with either gl_point_smooth, gl_line_smooth, or gl_polygon_smooth. This function is implemented by calculating a coverage value for the pixels passing through a straight line, indicating the coverage of the straight line to the pixel. Before blend is executed, the Alpha value of fragment is first multiplied by the coverage value. As you can see from this, anti-sawtooth is based on blend.
Multi-sample (multisampling) Anti-sawtoothMulti-sample operations are performed through an additional buffer and computation. Specifically, each pixel contains multiple sampling points. OpenGL stores the color, depth, and stencel information of Fragment in each sampling point. The final fragement coverage value in this pixel is calculated by the information of this sampling point. Steps for using multiple sampling: 1) gluinitdisplaymode (glu_double | glu_rgb | glu_multisample); 2) glgetintegerv (samples, & bufs); glgetintegerv (gl_samples, & samples ); you can check whether multiple samples are available. The former returns 1, and the latter returns 1 to indicate availability. 3) gl_multisample (gl_multisample), which can be activated. 3. Frog)

By using fog, you can achieve the effect of "the object fades out of sight as it moves away from the viewpoint.

To use fog, you must first call glable (gl_fog); you can set the fog color glfogfv (gl_fog_color, fogcolor); the fog concentration glfogf (gl_fog_density, 0.5); the fog range glfogf (gl_fog_start, 1.0), glfogf (gl_fog_end,
5.0). The parameter value indicates the distance from the viewpoint. The fog mode glfogi (gl_fog_mode, fogmode ).

Calculation Formula

OpenGL will mix the fragment color and the fog color. Before mixing, calculate a frog mixture factor:

Z: the depth of fragment is represented by default, and can be set by API;
Mode: gl_exp, gl_exp2, and gl_linear are three modes. F is calculated differently in each mode.
Density: fog concentration
The value of F is limited to [0, 1.

F after calculation, the fragment color of the fog effect is calculated:

Cf: fog color
CI: original fragment color

Fog Coordinate

By default, the above fog coordinates use the fragment depth, but you can also use API settings: glfog (gl_fog_coord_src, gl_fog_coord) to set fog coordinates from manual settings; glfogcoord * () to set the fog coordinates of vertices.

4. Point parameters sometimes we want to use points to represent small prototype or spherical objects, rather than complex and inefficient polygon models, such as lights and water drops. Glpointsize () and gl_point_smooth can generate points of various sizes and circles.
Glpointparameterf {if} {v} () can specify more useful vertex parameters:
Gl_point_distance_attenuation specifies a set of constants to control the dimensional attenuation mode of the Point of view away from the point of view;
Gl_point_size_min and gl_point_size_max can control the attenuation within the same range;
Gl_point_fade_threshold_size specifies a constant size value. When the point size drops to a value smaller than this size, its alpha value degrades proportionally;
Gl_point_sprite_coord_origin specifies the texture coordinate start point of the vertex, gl_lower_left or gl_upper_left.
5. Polygon offset: If you want to highlight the boundary of an object, you may first draw a polygon (which forms the surface of the object) in gl_fill mode, then draw the same polygon in gl_line mode. However, polygon in different modes are not raster in the same way, and the generated depth (depth) value is not necessarily the same. Therefore, the possible result is that the boundary is blurred and intermittent. To solve this problem, polygon offset is used to apply a depth coordinate offset to the polygon. Glable () gl_polygon_offset_fill, gl_polygon_offset_line, and gl_polygon_offset_point to activate polygon offset in different modes;
Glpolygonoffset (glfloat factor, glfloat units) to set the offset parameter. The offset calculation method is offset = m * factor + R * units, and m is the depth slope (depth slop ), calculated by the system; R is a predefined value of the system used to produce meaningful, minimum depth deviation. A reasonable parameter value varies depending on the actual situation. The width, depth, and depth slope of the line will be affected.

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: 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.