How can I anti-aliasing in OpenGL?

Source: Internet
Author: User
Document directory
  • Enable anti-aliasing
  • Anti-tooth Quality
  • Line example
  • Multiple sampling

Recently encountered problems in the project: When drawing a graph, the straight line will have obvious serrations, and the graph is extremely unattractive. The image changes significantly after anti-aliasing is used.

The main method of anti-aliasing is as follows:

When the point is very large, it is shown as follows:

What we actually want to see isDotInsteadRectangle

Effect after anti-aliasing is enabled

A little round

Code

glPointSize(10);glEnable (GL_POINT_SMOOTH);glHint (GL_POINT_SMOOTH, GL_NICEST);
Enable anti-aliasing

Or enable anti-aliasing with glEnable, which can be processed based on different images

  1. GL_POINT_SMOOTH point
  2. GL_LINE_SMOOTH line
  3. GL_POLYGON_SMOOTH Polygon
Anti-tooth Quality

Of course, the better the effect, the slower the computer speed, that is, there is a parameter setting

GlHint is used to set the anti-sawtooth degree of point, line, and polygon.

  1. GL_DONT_CARE should be the default.
  2. GL_FASTEST speed first
  3. GL_NICEST graphic display quality is preferred
Line example

Not Enabled

After enabling

Obviously, after anti-aliasing is enabled, the coarse line is changed to a rectangle, but not a parallelogram.

Code

glEnable (GL_LINE_SMOOTH);glHint (GL_LINE_SMOOTH, GL_NICEST);glLineWidth (10);
Multiple sampling

It has multiple colors, depths, and multi-group texture coordinates based on the number of pixel samples. It is especially suitable for polygon.

Three steps

1. Obtain a window that supports multisampling. With GLUT, you can ask for one by calling

Gluinitdisplaymode (glu_double | glu_rgb |

Glu_multisample );

2. After you 've opened a window, you will need to verify that multisampling

Is available. For instance, GLUT may give you a window

With "almost" what you have asked for. If querying the state variable

GL_SAMPLE_BUFFERS returns a value of one and GL_SAMPLES returns

A value greater than one, then you'll be able to use multisampling.

(GL_SAMPLES returns the number of subpixel samples. If there is only

One sample, multisampling is supported tively disabled .)

GLint bufs, samples;

GlGetIntegerv (GL_SAMPLE_BUFFERS, & bufs );

GlGetIntegerv (GL_SAMPLES, & samples );

3. To turn on multisampling, call

GlEnable (GL_MULTISAMPLE );

Comparison of the two figures below

These functions are supported by OpenGL.

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.