OpenGL basic graphics programming-effect Processing

Source: Internet
Author: User
15.1 Integration

  15.1.1 Alpha value and Fusion(Blending)
The Alpha value has been mentioned in the previous chapters, but almost all routines set it to 1.0, not detailed about the situation when it is another value. Integration is the focus of this chapter. It is the core of transparent technology, digital synthesis and Computer Painting Technology. Fixed name, fusion means that each component of the two colors is combined into one by a certain proportion. This proportion is derived from the Alpha value, that is, the value in rgba or (R, G, B, A). Generally, A is called opacity, and (1-A) is called opacity. Because the value cannot be described in the form of a color table, the fusion operation cannot be performed in the form of a color table.
To better understand these concepts, we can give an example. For example, if you look at the green trees outside the car through the tea glass in the car window, the colors of these trees are not the original green, but the mixture of transparent and non-transparent green colors. That is to say, the final color comes from two parts, one from the glass and the other from the tree green. The percentage of the two parts varies according to the transmission of the glass. If the transmission rate of the glass is 80% (that is, a 80% transmission of a bunch of light on it), the opacity is 20%, that is, the value a here is equal to 0.2. In this way, the color of the trees in the eye is 20% glass and the color of the trees in the eyes is 80%.

  15.1.2 fusion factor(Blending factor)
In OpenGL fusion operations, two factors are actually involved. These two factors are source factor and destination factor ). From the mathematical point of view, the source and target factors are respectively (Sr, SG, Sb, SA) and (DR, DG, DB, DA ), the final result of the fusion is:

(RS * Sr + RD * Dr, GS * SG + GD * DG, BS * Sb + bd * dB, as * Sa + Ad * Da)

In addition, each element value is reduced to [0, 1. In OpenGL, The gjblendfunc () function generates the values of these two fusion factors. The function form is:

Void glblendfunc (glenum sfactor, glenum dfactor)

Control the combination of source and target factors. The sfactor parameter indicates how to calculate the source factor, and the dfactor indicates how to calculate the target factor. The possible values of these parameters are shown in table 15-1.Note:: The value of the fusion factor is within the range of [0, 1]. After the two factors are combined, the value of the fusion factor is also reduced to [0, 1]. The rgba values of the source and target are S and D subscript respectively.

Constant Correlation factor Fusion factor obtained after calculation
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)
Table 15-1 source and target factors

After the glblendfunc () function is used to describe the generation of the fusion factor, you need to call the gl_blend function to start the fusion operation. If not, you can use gldisable (gl_blend) to disable it. If the source factor parameter is the constant gl_one and the Target Factor parameter is the constant gl_zero, the fusion operation is disabled. These values are the default values.

  15.1.3 converged instances
First, let's look at a simple example of integrated application:

  Example 15-1 Alpha Two-Dimensional fusion routine(Blend2d. c)

# Include "Glos. H"
# Include <Gl/Gl. h>
# Include <Gl/Glu. h>
# Include <Gl/Glaux. h>

Void myinit (void );
Void callback myreshape (glsizei W, glsizei H );
Void callback display (void );

/* Initialize Alpha Fusion parameters */
Void myinit (void)
{
Glable (gl_blend );
Glblendfunc (gl_src_alpha, gl_one_minus_src_alpha );
Glshademodel (gl_flat );
Glclearcolor (0.0, 0.0, 0.0, 0.0 );
}

Void callback display (void)
{
Glclear (gl_color_buffer_bit );

Glcolor4f (1.0, 0.0, 0.0, 0.7 );
Glrectf (0.25, 0.4, 0.75, 0.9 );

Glcolor4f (0.0, 1.0, 0.0, 0.5 );
Glrectf (0.1, 0.1, 0.6, 0.6 );
    
Glcolor4f (0.0, 0.0, 1.0, 0.3 );
Glrectf (0.4, 0.1, 0.9, 0.6 );
    
Glflush ();
}

Void callback myreshape (glsizei W, glsizei H)
{
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection );
Glloadidentity ();
If (W <= H)
Gluortho2d (0.0, 1.0, 0.0, 1.0 * (glfloat) h/(glfloat) W );
Else
Gluortho2d (0.0, 1.0 * (glfloat) W/(glfloat) h, 0.0, 1.0 );
Glmatrixmode (gl_modelview );
}

Void main (void)
{
Auxinitdisplaymode (aux_single | aux_rgba );
Auxinitposition (0, 0,500,500 );
Auxinitwindow ("Alpha 2D blending ");
Myinit ();
Auxreshapefunc (myreshape );
Auxmainloop (Display );
}

The preceding program running result shows the effect of integrating the blocks of three different colors (red, green, and blue.

Figure 15-1 combination of red, green, and blue squares


15.2 anti-Sample
Antialiasing, also known as anti-obfuscation, is an important concept in computer graphics. Because a computer-generated image is a digital image composed of discrete points, there must be a certain error between the generated image and the real scene. This error is manifested in the fact that the straight line or smooth curve on the image shows a jagged state, the color pattern loses its original form and color, and small objects are not reflected on the screen. This kind of sawtooth is called a sample. As shown in Figure 15-2, the left side is the sample line and the right side is the anti-sample line.

Figure 15-2 sample line and reverse sample line

  15.2.1 behavior control functions
In OpenGL, many detailed implementation algorithms are different. In this way, the glhint () function can be called to control the balance between image quality and drawing speed, but not all implementations use it. The function form is:

Void glhint (glenum target, glenum hint );

Controls some aspects of OpenGL behavior. The target parameter specifies the action to control. The value of the target parameter is shown in Table 15-2. The hint can be: gl_fastest (the most effective choice), gl_nicest (the highest quality choice), and gl_dont_care (no choice ).

Parameters Meaning
Gl_point_smooth_hint Sampling quality of points, lines, and polygon
Gl_line_smooth_hint
Gl_polygon_smooth_hint
Gl_fog_hint It is pointed out that the calculation of fog is based on each pixel (gl_nicest)
Or by each vertex (gl_fastest)
Gl_perspective_correction_hint Quality of color and texture Interpolation
Table 15-2 glhint () parameters and their meanings

In fact, the explanation of the prompts depends on the specific implementation of OpenGL, and sometimes can be ignored. The gl_perspective_correction_hint parameter is used to specify how the color value and texture value in an element are interpolated, either in the screen space or in the perspective projection correction method (This method requires more calculations ). Generally, the system performs linear color interpolation. Although it is not necessarily correct in terms of principle and technology, it is acceptable to the human eye and fast to achieve. However, in most cases, to make the texture look acceptable, You need to perform perspective correction and interpolation on it. Therefore, this parameter can be used to control the interpolation method. Similarly, this function is also used to control the behavior of graphics implementation during Reverse sample computation.

  15.2.2 reverse walk of points and lines
In OpenGL, we recommend that you use the rgba method to reverse the color table. Regardless of the method, to backsample the elements, you must first start the backsample with glable () (the parameter is gl_point, gl_line_smooth, or gl_polygon_smooth). You can also call glhint () provides an image quality prompt. However, in the rgba mode, you must start the mixing. The most likely mixing factor is gl_src_alpha (source) and gl_one_minus_src_alpha (target ). In addition, if the target factor is gl_one, the intersection of the line should be highlighted. Here is an example:

  Example 15-2 anti-sample line routine(Antiline. c)

# Include "Glos. H"

# Include <Gl/Gl. h>
# Include <Gl/Glu. h>
# Include <Gl/Glaux. h>

Void myinit (void );
Void callback myreshape (glsizei W, glsizei H );
Void callback display (void );
  
/* Initialize the reverse sample to the rgba mode, and set the Alpha mixture, prompt, and line width. */
Void myinit (void)
{
Glable (gl_line_smooth );
Glable (gl_blend );
Glblendfunc (gl_src_alpha, gl_one_minus_src_alpha );
Glhint (gl_line_smooth_hint, gl_dont_care );
Gllinewidth (5.0 );
Glshademodel (gl_flat );
Glclearcolor (0.0, 0.0, 0.0, 0.0 );
Gldepthfunc (gl_less );
Glenable (gl_depth_test );
}

Void callback display (void)
{
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
Glcolor4f (0.0, 0.6, 1.0, 1.0 );
Auxwireoctahedron (1.0 );
Glflush ();
}

Void callback myreshape (glsizei W, glsizei H)
{
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection );
Glloadidentity ();
Gluperspective (45.0, (glfloat) W/(glfloat) h, 3.0, 5.0 );

Glmatrixmode (gl_modelview );
Glloadidentity ();
Gltranslatef (0.0, 0.0,-4.0);/* move the object to the visible area */
Glrotatef (15.0, 1.0, 1.0, 0.0 );
}

Void main (void)
{
Auxinitdisplaymode (aux_single | aux_rgba );
Auxinitposition (0, 0,400,400 );
Auxinitwindow ("antialiased lines using rgba ");
Myinit ();
Auxreshapefunc (myreshape );
Auxmainloop (Display );
}

The preceding program running result shows an anti-image mesh ry.

Figure 15-3 reverse sample line

  15.2.3 reversed Polygon
The reverse walk of the filled polygon is similar to the reverse walk of the point line. The difference is that the point or line is changed to the polygon. The following is an example of a polygon inverse walk in rgba mode:

  Example 15-3 polygon anti-walk routine(Antipoly. c)

# Include "Glos. H"
# Include <Gl/Gl. h>
# Include <Gl/Glu. h>
# Include <Gl/Glaux. h>

Void myinit (void );
Void callback myreshape (glsizei W, glsizei H );
Void callback display (void );

Void myinit (void)
{
Glfloat mat_ambient [] = {0.5, 0.5, 0.0, 1.00 };
Glfloat mat_diffuse [] = {1.0, 0.8, 0.1, 1.0 };
Glfloat position [] = {1.0, 0.0, 1.0, 0.0 };

Glmaterialfv (gl_front, gl_ambient, mat_ambient );
Glmaterialfv (gl_front, gl_diffuse, mat_diffuse );
Gllightfv (gl_light0, gl_position, position );

Glable (gl_lighting );
Glable (gl_light0 );
Glable (gl_blend );
Glcullface (gl_back );
Glable (gl_cull_face );
Glable (gl_polygon_smooth );
Gldisable (gl_depth_test );
Glblendfunc (gl_src_alpha_saturate, gl_one );

Glclearcolor (0.0, 0.0, 0.0, 0.0 );
}

Void callback display (void)
{
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );

Gltranslatef (0.0, 0.0,-8.0 );
Glrotatef (-45.0, 1.0, 0.0, 0.0 );
Glrotatef (45.0, 0.0, 1.0, 0.0 );
Auxsolidicosahedron (1.0 );
    
Glflush ();
}

Void callback myreshape (glsizei W, glsizei H)
{
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection );
Glloadidentity ();
Gluperspective (30.0, (glfloat) W/(glfloat) h, 1.0, 20.0 );
Glmatrixmode (gl_modelview );
}

Void main (void)
{
Auxinitdisplaymode (aux_single | aux_rgba | aux_alpha );
Auxinitposition (0, 0,400,400 );
Auxinitwindow ("antialiased polygons ");
Myinit ();
Auxreshapefunc (myreshape );
Auxmainloop (Display );
}

The above program running result shows a yellow filling reversed 20-sided body.

Figure 15-4 inverted Polygon


15.3. Fog

  15.3.1 introduction and routine of fog
The atomization effect is widely used in today's computer graphics. It not only makes the objects in the scene look more real, but also increases the rendering speed. In many cases, computer images may sometimes have different precision and different edges and corners. In the previous section, the anti-image technology can be used to smooth the border anti-image of an object, make it look more authentic. The atomization processing in this section can make the object look more natural, that is, in the fog, objects far away from the viewpoint will become blurred.
Fog is a general term used to describe the atmospheric effect. It is widely used in visual simulation. It can simulate the effects of smoke, mist, smoke, and pollution. After the fog is started, objects that are far away from the viewpoint are deprecated into the fog color. At the same time, the fog concentration can be controlled, that is, as the distance increases, the object fades at a controllable rate, you can also set the fog color. Fog can be used in two colors. Here is a routine for using fog in the rgba mode:

  Example 15-4 rgba atomization routine(Fogrgb. c)

# Include "Glos. H"
# Include <Gl/Gl. h>
# Include <Gl/Glu. h>
# Include <Gl/Glaux. h>
# Include <math. h>
# Include <stdio. h>

Void myinit (void );
Void callback myreshape (glsizei W, glsizei H );
Void callback display (void );

Void myinit (void)
{
Glfloat mat_ambient [] = {0.7, 0.6, 0.4, 1.00 };
Glfloat mat_diffuse [] = {0.7, 0.0, 0.99, 1.0 };
Glfloat mat_specular [] = {1.0, 0.0, 1.0, 1.00 };
Glfloat mat_shininess [] ={ 15.0 };
Glfloat position [] = {5.0, 5.0, 5.0, 1.0 };
Glfloat fogcolor [4] = {0.6, 0.6, 0.6, 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 );

Glfrontface (gl_cw );
Glable (gl_lighting );
Glable (gl_light0 );

Glable (gl_fog );
{
Glfogi (gl_fog_mode, gl_linear );
Glfogfv (gl_fog_color, fogcolor );
Glfogf (gl_fog_start, 3.0 );
Glfogf (gl_fog_end, 15.0 );
Glhint (gl_fog_hint, gl_dont_care );
Glclearcolor (0.3, 0.3, 0.3, 1.0 );
}
}

Void callback display (void)
{
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );

Glpushmatrix ();
Gltranslatef (-3.0,-1.5,-2.0 );
Auxsolidtorus (0.6, 1.5 );
Glpopmatrix ();
    
Glpushmatrix ();
Gltranslatef (-0.5,-0.5,-7.0 );
Auxsolidtorus (0.6, 1.5 );
Glpopmatrix ();
    
Glpushmatrix ();
Gltranslatef (2.0, 0.8,-10.0 );
Auxsolidtorus (0.6, 1.5 );
Glpopmatrix ();
    
Glflush ();
}

Void callback myreshape (glsizei W, glsizei H)
{
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection );
Glloadidentity ();
If (W <= (H * 3 ))
Glortho (-6.0, 6.0,-2.0 * (glfloat) H * 3)/(glfloat) W, 2.0 * (glfloat) H * 3)/(glfloat) W, 0.0, 10.0 );
Else
Glortho (-6.0 * (glfloat) W/(glfloat) H * 3), 6.0 * (glfloat) W/(glfloat) H * 3),-2.0, 2.0, 0.0, 10.0 );
Glmatrixmode (gl_modelview );
Glloadidentity ();
}

Void main (void)
{
Auxinitdisplaymode (aux_single | aux_rgba );
Auxinitposition (0, 0,500,400 );
Auxinitwindow ("fog_rgb_mode ");
Myinit ();
Auxreshapefunc (myreshape );
Auxmainloop (Display );
}

The above program running results show the effect of three blue-purple rings in the fog, where the atomization calculation adopts a linear method (gl_linear ).

Figure 15-5 atomization effect

  15.3.2 atomization steps
It is very easy to use atomization in OpenGL programs. The procedure is as follows:
1) Start fog. Function call: glable (gl_fog );
2) control fog. The function is called glfog * () and uses it to select the fog equation that controls the concentration and color. The specific form is:

Void glfog {if} [v] (glenum pname, type PARAM );

Set the computing fog parameters and functions. If pname is gl_fog_mode, Param can be gl_exp (default), gl_exp2, or gl_linear, representing three fog factors respectively. If pname is gl_fog_density, gl_fog_start, or gl_fog_end, Param is the value of density, start, and end corresponding to the fog equation. The default values are 1, 0, and 1. In the rgba mode, the pname can be gl_fog_color. In this case, the param parameter is a vector pointer pointing to a value containing rgba. Similarly, in the color table mode, the pname value is gl_fog_index, and its Param value is the fog color index value.
3) If necessary, use the glhint (gl_fog_hint) function to provide a value.

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.