Introduction
Blending, fog, and antialiasing are three special effect processing methods in OpenGL. The fusion provides a transparent or translucent display technology, and the atomization processing is performed based on the distance from the object to the viewpoint; reverse sampling can reduce the error pattern generated when the discretization graph is drawn.
Implement integrated special effects
The R, G, and B components of the two colors can be combined to form a new color. In the rgba color mode, a indicates the Alpha value, corresponds to the blending ratio of colors. Because the value can be described only in rgba mode, fusion cannot be used in color index mode. The fusion operation can be calculated using the source factor (Sr, SG, Sb, SA) and the Target Factor (DR, DG, DB, DA, the fusion result is (RS * Sr + RD * Dr, GS * SG + GD * DG, BS * Sb + bd * dB, as * Sa + Ad * Da) each element value is in the range of [0, 1. We can see that the key to fusion processing is the setting of fusion factors (Sr, SG, Sb, SA) and (DR, DG, DB, DA. In OpenGL, the source and target factors are generated through the glblendfunc () function. The function form is:
Void glblendfunc (glenum sfactor, glenum dfactor) |
Specify the sfactor and dfactor parameters to calculate the source and target factors respectively. For specific values, see the following table. After the fusion factor value is generated, you also need to call glable (gl_blend) and gldisable (gl_blend) to enable and disable fusion processing.
Constant |
Correlation factor |
Fusion factor result |
Gl_zero |
Source factor or Target Factor |
(0, 0, 0) |
Gl_one |
Source factor or Target Factor |
(1, 1, 1) |
Gl_dst_color |
Source factor |
(Rd, Gd, BD, AD) |
Gl_src_color |
Objective factor |
(RS, GS, BS,) |
Gl_one_minus_dst_color |
Source factor |
(1, 1, 1)-(Rd, Gd, BD, AD) |
Gl_one_minus_src_color |
Objective factor |
(1, 1, 1)-(RS, GS, BS,) |
Gl_src_alpha |
Source factor or Target Factor |
(As,) |
Gl_one_minus_src_alpha |
Source factor or Target Factor |
(1, 1, 1)-(as,) |
Gl_dst_alpha |
Source factor or Target Factor |
(AD, AD) |
Gl_one_minus_dst_alpha |
Source factor or Target Factor |
(1, 1, 1)-(AD, AD) |
Gl_src_alpha_saturate |
Source factor |
(F, 1); F = min (AS, 1-AD) |
The following is a simple example of applying the fusion technology. The drawing result in Figure 1 shows that the color of the overlapping parts of two blocks with different colors has changed after fusion processing is enabled, this region is the result of Fusion:
Void callback display () { Glclear (gl_color_buffer_bit); // clear the screen Glcolor4f (0.0, 1.0, 0.0, 0.5); // draw a rectangle Glrectf (0.1, 0.1, 0.6, 0.6 ); Glcolor4f (1.0, 1.0, 0.0, 0.7); // draw a rectangle Glrectf (0.4, 0.3, 0.9, 0.8 ); Glflush (); // force drawing completed } Void Init () { Glable (gl_blend); // enable Fusion Glblendfunc (gl_src_alpha, gl_one_minus_src_alpha); // generates a fusion factor Glshademodel (gl_flat); // you can specify the plane brightness. Glclearcolor (0.0, 0.0, 0.0, 0.0); // clear the screen } |
Figure 1 fusion of colored squares
Special atomization effect
Atomization is a graphic rendering technology that simulates the effects of fog in the natural world on the visual effects of scene objects. This technology gradually fades the color of the object from the viewpoint to the background color. Through atomization, it can better show the distance from the object to the viewpoint. Although the specific calculation of atomization is very complex, it is very simple to use in OpenGL. First, call glable (gl_fog) to enable atomization. After enabling, objects farther away from the viewpoint start to fade into the fog color. Then, you can use the glfog * () function to select the equation for controlling the fog concentration and color. The specific form is:
Void glfog {if} [v] (glenum pname, type PARAM ); |
This function sets the atomization parameters and functions. When pname is gl_fog mode, Param can be gl_exp, gl_exp2 (exponential square), or gl_linear (linear ); if the pname parameter is gl_fog_density, gl_fog_start, or gl_fog_end, The param parameter specifies different calculation formula parameters under different atomizing mathematical models. When the pname parameter is gl_fog_color, param is a pointer to a color vector. Finally, if necessary, you can call the glhint (gl_fog_hint) function to further specify the atomization effect. The following shows the sample code for achieving the atomization effect and the drawing result (Figure 2 ):
Figure 2 atomization effect |
Void callback display () { Glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear the screen Glpushmatrix (); // draws a near-point view Gltranslatef (-3.0,-1.5,-2.0 ); Auxsolidtorus (0.6, 1.5 ); Glpopmatrix (); Glpushmatrix (); // draw a remote view Gltranslatef (2.0, 0.8,-10.0 ); Auxsolidtorus (0.6, 1.5 ); Glpopmatrix (); Glflush (); // force drawing completed } Void Init () { Glfloat mat_ambient [] = {0.7, 0.6, 0.0, 1.0}; // sets the illumination model. Glfloat mat_diffuse [] = {0.7, 0.6, 0.0, 1.0 }; Glfloat mat_specular [] = {1.0, 0.0, 1.0, 1.0 }; Glfloat mat_shininess [] ={ 50.0 }; Glfloat position [] = {5.0, 5.0, 5.0, 1.0 }; Glmaterialfv (gl_front, gl_ambient, mat_ambient ); Glmaterialfv (gl_front, gl_diffuse, mat_diffuse ); Glmaterialfv (gl_front, gl_specular, mat_specular ); Glmaterialfv (gl_front, gl_shininess, mat_shininess ); Glenable (gl_depth_test ); Gldepthfunc (gl_less ); Gllightfv (gl_light0, gl_position, position ); Glable (gl_lighting ); Glable (gl_light0 ); Glfrontface (gl_cw ); Glable (gl_fog); // enable Atomization { Glfogi (gl_fog_mode, gl_linear); // use linear atomization effect Glfloat fogcolor [] = {0.3, 0.3, 0.3, 1.0}; // specify the atomizing color Glfogfv (gl_fog_color, fogcolor ); Glfogf (gl_fog_start, 3.0); // specify the parameters for calculating the formula based on linear variation Glfogf (gl_fog_end, 15.0 ); Glhint (gl_fog_hint, gl_dont_care); // specifies the quality of the atomization effect. } } |
Implementation of drawing Inversion
Since the digital image is expressed by discrete pixel points when the image is drawn to the screen, the graphic elements will produce sawtooth on the smooth curve, this kind of sawtooth is out of shape. Anti-aliasing is also called anti-obfuscation. It determines the color value of a pixel Based on the pixel area of the element. This processing can eliminate the effect of the Sawtooth to a certain extent. In OpenGL, The glhint () function can be used to control the balance between image quality and drawing speed. The function form is as follows:
Void glhint (glenum target, glenum hint ); |
The target parameter specifies the behavior to be controlled. Specifically, gl_point_smooth_hint, gl_line_smooth_hint, and gl_polygon_smooth_hint specify the sampling quality of points, lines, and polygon respectively) it is also implemented by vertex (gl_fastest); gl_perspective_correction_hint specifies the color texture interpolation quality and can correct some visual errors caused by pure linear interpolation. The hint can be: gl_fastest (the most effective choice), gl_nicest (the highest quality choice), and gl_dont_care (no choice ).
Although the color index mode of OpenGL can also implement reverse sample, it is recommended to do so in rgba mode. You must call the glable () function to start the reverse sample (the parameter is gl_point, gl_line_smooth, or gl_polygon_smooth) when performing the reverse sample of the graph element ). If the image is reversed in rgba mode, it must be used with fusion. Generally, gl_src_alpha and gl_one_minus_src_alpha are used as the source and target factors respectively. The following is a sample code for using reverse walk sample to draw a polygon in rgba mode. From the drawing result (figure 3), we can see that the straight line boundary of a polygon is smooth:
Void callback display () { Glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear the screen Auxwiredodecahedron (1.0); // draw 20-sided bodies Glflush (); // force drawing completed } Void Init () { Glable (gl_line_smooth); // enable reverse sample Glable (gl_blend); // start Fusion Glblendfunc (gl_src_alpha, gl_one_minus_src_alpha); // generates a fusion factor Glhint (gl_line_smooth_hint, gl_dont_care); // weigh image quality and rendering speed Gllinewidth (2.0); // line width Glshademodel (gl_flat); // plane Shading Glclearcolor (0.0, 0.0, 0.0, 0.0); // clear the screen Gldepthfunc (gl_less); // compare the activation depth Glenable (gl_depth_test ); } |
Figure 3 anti-sample Polygon