# Visualization Library A lightweight C++ OpenGL middleware for 2D/3D graphics | [Home] [Tutorials] [All Classes] [Grouped Classes]

# Antialiasing and Multisampling Tutorial

Text Antialiasing

Visualization Library addresses text rendering and line/polygon/points antialiasing separately. Visualization Library uses FreeType for text rendering which natively supports high quality sub-pixel antialiasing. Of course the antialiasing effect depends on the type and quality of the font used. The fonts distributed with VL (Bitstream Vera from the Gnome project) for example are excellent fonts and render high quality text. Poor fonts will always render poor text. The user does not need to enable text antialiasing, the text is always rendered antialiased, regardless of the settings discussed below.

Geometry Antialiasing

Antialiasing gometrical primitives is a totally different issue. In this case we can achieve antialiasing in two ways.

1) Smoothing.

Smoothing off | Smoothing on

Enabling point, line and polygon smoothing:

       ref<Effect> fx = new Effect;
       fx->shader()->enable(EN_BLEND);
       fx->shader()->enable(EN_POINT_SMOOTH);
       fx->shader()->enable(EN_LINE_SMOOTH);
       fx->shader()->enable(EN_POLYGON_SMOOTH);

Point, line and polygon smoothing requires alpha blending, for this reason we also enabled EN_BLEND. Unfortunately the fact that we are using blending makes our rendering order-dependent. That is, the order in which the primitives are drawn affects the final result. See also Transparency and Polygon Depth Sorting Tutorial for more information. You can use vl::DepthSortCallback to sort the primitives within an object but since this implies a consistent performance penalty one often accepts a few artifacts to preserve good performances. Usually this is not a big problem for smoothed lines or points, while smoothed polygons are used much more rarely in comparison.

- Pro: supported by basically every OpenGL implementation.
- Cons: needs blending enabled; needs back to front sorting; usually you have to accept some artifacts.

Keep also in mind that smoothed primitives require always more computational power compared to non smoothed ones.

2) Multisampling.

Multisampling off | Multisampling 8x on

When creating your OpenGL context enable multisampling. Doing this using a GUI framework supported by VL is very simple (see also Spinning Cube Tutorial):

     OpenGLContextFormat format;
     ...
     format.setMultisampleSamples(8);
     format.setMultisample(true);

The code above enables 8x multisampling (if your system supports it). Note that, for example, if you require 8x and your system supports only 4x, VL automatically selects 4x multisampling. Note also that selecting the appropriate pixel format is not an OpenGL issues but is rather a GUI framework issue. For this reason you might need to search through the documentation of your GUI framework (Win32, Qt, wxWidgets etc.) in order to open a multisampling-capable OpenGL rendering context.

Multisampling is basically transparent to the application, once is it enabled, everything becomes antialiased. Note that you can still enable/disable multisample on a per-Shader basis using

       ref<Effect> fx = new Effect;
       fx->shader()->enable/disable(EN_MULTISAMPLE); // disabled by default, as all the OpenGL enables.

- Pro: virtually well supported (OpenGL 1.3 upwards); nicely and correctly multisamples everything; just need to set EN_MULTISAMPLE where appropriate; no blending or sorting is needed.
- Cons: slower rendering; can use a consistent amount of GPU memory to store the extra samples; antialiasing quality and support depends on the user's system capability.

For more information on multisampling see http://www.opengl.org/registry/specs/ARB/multisample.txt

Visualization Library v2009.08 Reference Documentation
Copyright 2005-2009 Michele Bosi. All rights reserved.
Updated on Sun Nov 29 11:11:40 2009.
Permission is granted to use this page to write and publish articles regarding Visualization Library.