OpenGL anti-sample mixing multiple sampling blend multisample

Source: Internet
Author: User

1. Reverse sampling

When a non-horizontal and non-vertical straight line or polygon boundary is drawn on a Raster Graphic Display, a more or less jagged or tiered appearance is displayed. This is because the straight line, polygon, and color boundary are continuous, and the grating is composed of discrete points. It represents a straight line and polygon on the grating display device and must be sampled in a discrete position. Due to the information distortion caused by inadequate sampling reconstruction, it is called a sample (aliasing ). The technology used to reduce or eliminate such effects is called antialiasing ).

2. Implementation of OpenGL anti-Sample

The anti-Image Processing in OpenGL uses a fusion technique to implement vertex, line, and image edge interpolation, as well as fog, color, and texture interpolation. OpenGL implementation Reverse sampleTwo conditions must be met: Enable HybridAnd enable Reverse sampleProcessing. 3. OpenGL hybrid Blend
What is hybrid? Mixing is to mix the two colors together. Specifically, the original color at a certain pixel location and the color to be painted are mixed together in some way to achieve special effects.
Suppose we need to draw a scenario: to look at the green object through the red glass, we can first draw the green object, then draw the red glass. When drawing the red glass, use the "mix" function to mix the red and the original green to get a new color, it looks like the glass is translucent.
To use the mixed functions of OpenGL, you only need to call: glable (gl_blend.
To disable the OpenGL hybrid function, you only need to call: gldisable (gl_blend. Glblendfunc (source factor, Target Factor) defines a hybrid method. For example:
If glblendfunc (gl_one, gl_zero); is set, the source color is used completely, and the target color is not used at all, therefore, the image effect is the same as that when the image is not mixed (of course, the efficiency may be a little lower ). If the source and target factors are not set, this is the default setting.
If glblendfunc (gl_zero, gl_one); is set, the source color is not used at all. Therefore, no matter what you want to draw, it will not be painted at last. (But this does not mean that this setting is useless, and sometimes it may be used for special purposes)
If glblendfunc (gl_src_alpha, gl_one_minus_src_alpha); is set, the source color is multiplied by its own Alpha value, and the target color is multiplied by 1.0 minus the Alpha value of the source color, the larger the Alpha value of the source color, the larger the proportion of the source color in the new color, and the smaller the proportion of the target color. In this case, we can simply think of the Alpha value of the source color as "opacity ". This is also the most common method for mixing.
If glblendfunc (gl_one, gl_one); is set, the source color and the target color are used completely. The final color is actually a simple addition of the two colors. For example, red (1, 0, 0) and green (0, 1, 0) are added to get (1, 1, 0), and the result is yellow.
Note:
The source color and target color are related to the draw sequence. If a red object is first drawn, then a green object is drawn on it. Green indicates the source color, and red indicates the target color. If the order is reversed, red indicates the source color, and Green indicates the target color. When drawing, pay attention to the order, so that the source color of the painting corresponds to the set source factor, and the target color corresponds to the set target factor. Do not be confused by the disordered order. Considerations for 3D Mixing
Problem
: Maybe you can't wait to draw a 3D scene with a translucent object. However, I am afraid I am not able to do it now. Another point is that we must pay attention when mixing 3D scenes, that is, deep buffering. The deep buffer is such a piece of data that records how close each pixel is to the observer. When the deep buffer test is enabled, if the pixel to be drawn is closer than the original pixel, the pixel will be drawn. Otherwise, pixels will be ignored and will not be drawn. This is useful when drawing opaque objects-whether it is to draw near objects first and then draw distant objects, or to draw distant objects and then draw near objects first, or simply draw in a disordered order, and the final display result is always close to the object to cover the distant object. However, when you need to achieve a translucent effect, you will find that everything is not that good. If you draw a close-range semi-transparent object, it retains some information in the depth buffer, so that the distant object cannot be drawn again. Although a translucent object is still translucent, what you see through it is not the correct content. Solution: To solve the above problem, you need to set the depth buffer to read-only when creating a translucent object. As a result, although the translucent object is drawn, the depth buffer remains in the original state. If another object appears after the translucent object, it can be drawn before the opaque object (because the depth of the object is recorded in the depth buffer ). To draw an opaque object later, you only need to set the depth buffer to readable and writable. Hmm? How do I draw a part of a semi-transparent part of an object? To do this well, you only need to divide the object into two parts, one of which is semi-transparent and the other part is non-transparent. You can draw them separately.
Note:: Even with the above techniques, we still cannot draw in a disordered order as we wish. You must first draw an opaque object and then draw a transparent object. Otherwise, assume that the background is blue, near a red glass, and a green object in the middle. If you first draw a red translucent glass, it will first mix it with the blue background, then when you draw a green object in the middle, you can no longer mix it with the red glass. Summary:The order of drawing is to draw all opaque objects first. If both objects are non-transparent, it does not matter who is first or who is later. Then, set the depth buffer to read-only. Next, draw all translucent objects. If the two objects are semi-transparent, then the first one needs to follow his own wishes (note that the first one will become the "target color ", the source color, so the order will have some impact on the result ). Finally, set the depth buffer to the readable and writable form. Call gldepthmask (gl_false); you can set the depth buffer to read-only. Call gldepthmask (gl_true); you can set the depth buffer to a readable and writable form. 4. Anti-Sample Function of OpenGL

Void glhint (glenum target, glenum hint );

Hint defines the anti-Sample Method
Gl_fastest provides the most effective options
Gl_nicest provides the highest quality options
Gl_dont_care not selected

Target defines the anti-sample object

 

Gl_point_smooth_hint specifies a vertex.

Gl_line_smooth_hint line
Sample Quality of gl_polygon_smooth_hint Polygon
Gl_fog_hint indicates whether the atomization calculation is performed by each pixel (gl_nicest) or by each vertex (gl_fastest)
Gl_perspective_correction_hint specifies the quality of color texture interpolation.
Gl_perspective_correction_hint is used to correct observation errors caused by pure linear interpolation.

5. Example 1 of OpenGL anti-Sample-point anti-Sample# Include <windows. h> # include <Gl/glut. h> # include <math. h> # define x_screen 800 # define y_screen 600 # define little 50 # define middle 20 # define large 8 void mybackground () {glclearcolor (0.0, 0.0, 0.0, 1.0 ); glcolor3f (1.0, 1.0, 1.0);} void mydisplay () {glable (gl_depth_test); // if there is no anti-aliasing, the vertex is square. If we start the anti-aliasing setting, the dot is a dot. Glenable (gl_point_smooth); glable (gl_line_smooth); glhint (gl_point_smooth_hint, gl_nicest); // make round points, not square pointsglhint (callback, gl_nicest ); // antialias the linesglable (gl_blend); glblendfunc (gl_src_alpha, latency); glclear (gl_color_buffer_bit | latency); int I; glbegin (gl_points); for (I = 0; I <little; I ++) glvertex2f (50.0 + rand () % x_screen, 50.0 + rand () % y_screen); glend (); glpointsize (2 ); glbegin (gl_points); for (I = 0; I <middle; I ++) glvertex2f (50.0 + rand () % x_screen, 50.0 + rand () % y_screen ); glend (); glpointsize (8); glbegin (gl_points); for (I = 0; I <large; I ++) glvertex2f (50.0 + rand () % x_screen, 50.0 + rand () % y_screen); glend (); glbegin (gl_polygon); for (I = 0; I <64; I ++) glvertex2f (600 + 50.0 * Cos (float) I/10), 500 + 50.0 * sin (float) I/10); glend (); gllinewidth (3 ); glblendfunc (gl_src_alpha, latency); glable (gl_blend); gl_line_smooth); glbegin (gl_line_strip); for (I = 0; I <19; I ++) {glvertex2f (RAND () % 10 + I * 70, Rand () % 50 + 50.0 + (I % 2) * 80);} glend (); fig ();} void mychange (int w, int h) {glviewport (0.0, W, H); glmatrixmode (gl_projection); glloadidentity (); gluortho2d (0.0, x_screen, y_screen ); glmatrixmode (gl_modelview); glloadidentity () ;}void main () {gluinitdisplaymode (glu_double | glu_rgb | glu_depth); gluinitwindowsize (x_screen, y_screen); glucreatewindow ("star "); gludisplayfunc (mydisplay); glureshapefunc (mychange); mybackground (); glumainloop ();} 6. Example of OpenGL anti-walk 2 --- line anti-walk# Include <Gl/glut. h> # include <stdio. h> static float rotangle = 0 .; void Init (void) {glfloat values [2]; glgetfloatv (gl_line_width_granularity, values); printf ("gl_line_width_granularity value is % 3.1f \ n", values [0]); glgetfloatv (values, values); printf ("gl_line_width_range values are % 3.1f % 3.1f \ n", values [0], values [1]); glable (gl_line_smooth ); glenable (gl_blend); glblendfunc (gl_src_alpha, gl_one_minus_src_alpha); glhint (values, gl_dont_care); gllinewidth (1.5); glclearcolor (0.0, 0.0, 0.0, 0.0 );} void display (void) {glclear (gl_color_buffer_bit); glcolor3f (0.0, 1.0, 0.0); glpushmatrix (); glrotatef (-rotangle, 0.0, 0.0, 0.1 ); glbegin (gl_lines); glvertex2f (-0.5, 0.5); glvertex2f (0.5,-0.5); glend (); glpopmatrix (); glcolor3f (0.0, 0.0, 1.0 ); glpushmatrix (); glrotatef (rotangle, 0.0, 0.0, 0.1); glbegin (gl_lines); glvertex2f (0.5, 0.5); glvertex2f (-0.5,-0.5); glend (); glpopmatrix (); glflush ();} void reshape (int w, int h) {glviewport (0, 0, W, H); glmatrixmode (gl_projection); glloadidentity (); if (W <= h) gluortho2d (-1.0, 1.0,-1.0 * (glfloat) h/(glfloat) W, 1.0 * (glfloat) h/(glfloat) W ); else gluortho2d (-1.0 * (glfloat) W/(glfloat) h, 1.0 * (glfloat) W/(glfloat) h,-1.0, 1.0); glmatrixmode (gl_modelview ); glloadidentity ();} void keyboard (unsigned char key, int X, int y) {Switch (key) {Case 'r': rotangle + = 20 .; if (rotangle> = 360.0) rotangle = 0.0; fig (); break; Case 27: exit (0); break; default: break;} int main (INT argc, char ** argv) {gluinit (& argc, argv); gluinitdisplaymode (glu_single | glu_rgb); gluinitwindowsize (200,200); glucreatewindow (argv [0]); Init (); fig (reshape); fig (keyboard); fig (Display); fig (); Return 0 ;}

 

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

1. color mixing is used to eliminate some sertices. It mainly targets the point-and-line and non-overlapping polygon anti-aliasing.

The configuration code for anti-aliasing is as follows:

Glblendfunc (gl_src_alpha, gl_one_minus_src_alpha );

Glable (gl_blend );

Glenable (gl_point_smooth );

Glhint (gl_point_smooth_hint, gl_nicest );

Glable (gl_line_smooth );

Glhint (gl_line_smooth_hint, gl_nicest );

Glable (gl_polygon_smooth );

Glhint (gl_polygon_smooth_hint, gl_nicest );

The code for canceling the anti-aliasing operation is as follows:

Gldisable (gl_blend );

Gldisable (gl_line_smooth );

Gldisable (gl_point_smooth );

Gldisable (gl_polygon_smooth );

2. Multi-Sample)

Not all platforms support the use of color mixing to eliminate polygon sawtooth. In addition, polygon mixing also has sequence problems and is inconvenient to use. OpenGL introduces multi-sample to solve the problem of polygon sawtooth, and adds a frame cache containing color, attempt, and module cache value. The code for enabling the multi-sample function is as follows:

// Apply for a frame cache with dual caching, including color, depth, and multiple sampling.

Fig );

Glable (gl_multisample); // enable multiple caches

Gldisable (gl_multisample); // disable multiple caches

Note: multiple sampling and mixing cannot be enabled at the same time. These two methods can only be mutually exclusive. That is, you must disable another method before using any method.

OpenGL anti-sample mixing multiple sampling blend multisample

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.