There are a few methods, but the following locates an MSAA FBConfig, looks up its XVisual, and then creates an X window in that visual. You'll notice some similarities with the WGL method above.

+

This is the method you'd use on UNIX/Linux if you want to create an X window with multisampling capability. There are a few methods for doing this, but the following locates an MSAA FBConfig, looks up its XVisual, and then creates an X window in that visual. You'll notice some similarities with the WGL method above.

GLUT is a wrapper around the platform-specific GL integration layers (e.g. WGL, GLX, etc.) which allows you to prototype simple GL examples quickly. In GLUT, the key to allocating an MSAA window is this:

+

GLUT is a wrapper around the platform-specific GL integration layers (e.g. WGL, GLX, etc.) which allows you to prototype simple cross-platform GL examples quickly. In GLUT, the key to allocating an MSAA window is this:

glutInitDisplayMode( ... | GLUT_MULTISAMPLE );

glutInitDisplayMode( ... | GLUT_MULTISAMPLE );

Line 153:

Line 172:

void reshape(int width, int height)

void reshape(int width, int height)

{

{

−

glViewport(0, 0, width, height);

+

glViewport(0, 0, width, height);

}

}

void mouse(int button, int state, int x, int y)

void mouse(int button, int state, int x, int y)

{

{

−

if (state==GLUT_DOWN)

+

if (state==GLUT_DOWN)

−

{

+

{

−

msaa = !msaa;

+

msaa = !msaa;

−

glutPostRedisplay();

+

glutPostRedisplay();

−

}

+

}

}

}

Line 168:

Line 187:

void display()

void display()

{

{

−

int err=0;

+

int err=0;

−

glClear(GL_COLOR_BUFFER_BIT);

+

glClear(GL_COLOR_BUFFER_BIT);

−

glMatrixMode(GL_PROJECTION);

+

glMatrixMode(GL_PROJECTION);

−

glLoadIdentity();

+

glLoadIdentity();

−

glOrtho(-1,1,-1,1,-1,1);

+

glOrtho(-1,1,-1,1,-1,1);

−

glMatrixMode(GL_MODELVIEW);

+

glMatrixMode(GL_MODELVIEW);

−

glLoadIdentity();

+

glLoadIdentity();

−

glRotatef(rotation_angle, 0,0,1);

+

glRotatef(rotation_angle, 0,0,1);

−

glColor4f(1,0,0,1);

+

glColor4f(1,0,0,1);

−

if (msaa)

+

if (msaa)

−

{

+

{

−

glEnable(GL_MULTISAMPLE_ARB);

+

glEnable(GL_MULTISAMPLE_ARB);

−

printf("msaa on\n");

+

printf("msaa on\n");

−

}

+

}

−

else

+

else

−

{

+

{

−

printf("msaa off\n");

+

printf("msaa off\n");

−

glDisable(GL_MULTISAMPLE_ARB);

+

glDisable(GL_MULTISAMPLE_ARB);

−

}

+

}

−

+

−

glRectf(-.5,-.5,.5,.5);

+

−

glutSwapBuffers();

+

glRectf(-.5,-.5,.5,.5);

−

err = glGetError();

+

−

if (err)

+

glutSwapBuffers();

−

fprintf(stderr, "%s\n", gluErrorString(err));

+

err = glGetError();

+

if (err)

+

fprintf(stderr, "%s\n", gluErrorString(err));

}

}

Line 204:

Line 223:

main (int argc, char** argv)

main (int argc, char** argv)

{

{

−

glutInit(&argc, argv);

+

glutInit(&argc, argv);

−

glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE);

+

glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE);

−

glutCreateWindow(argv[0]);

+

glutCreateWindow(argv[0]);

−

+

−

glutDisplayFunc(display);

+

−

glutMouseFunc(mouse);

+

−

glutReshapeFunc(reshape);

+

−

glutReshapeWindow(400,400);

+

glutDisplayFunc(display);

+

glutMouseFunc(mouse);

+

glutReshapeFunc(reshape);

+

+

glutReshapeWindow(400,400);

−

printf("%s\n",glGetString(GL_RENDERER));

+

printf("%s\n",glGetString(GL_RENDERER));

−

rotation_angle=30;

+

rotation_angle=30;

−

glutMainLoop();

+

glutMainLoop();

−

return 0;

+

return 0;

}

}

</pre>

</pre>

Revision as of 23:26, 11 November 2012

Multisampling, also known as multisample antialiasing (MSAA), is one method for achieving full-screen antialiasing (FSAA). Unlike supersampling (SSAA) which can result in the same pixel being shaded multiple times per pixel, multisampling runs the fragment program just once per pixel rasterized. However with MSAA multiple depth/stencil comparisons are performed per pixel, one for each of the subsamples, which gives you sub-pixel spatial precision on your geometry and nice, smoothed edges on your polygons.

History

Before GL_ARB_multisample extension, the edges of lines, polygons, and points could be selectively antialiased using using glEnable(GL_LINE_SMOOTH), glEnable(GL_POLYGON_SMOOTH), glEnable(GL_POINT_SMOOTH) respectively, combined with a blending function, such as glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). Such features were typically not implemented in hardware in early consumer graphics cards at the time, and were done in software resulting in poor performance. More expensive "workstation" graphics cards from HP, Sun, and SGI at the time did implement these features in hardware. Modern programs should not make use of these features.

Rendering with Multisampling

While the second step is standardized across all render target types and platforms, the first is only standardized for FBOs (as it is totally internal to GL). When rendering to a window, the allocation method depends on the specific platform GL integration layer in-use (e.g. WGL, GLX, AGL, etc.). GLUT provides a wrapper around some of these so you don't have to care.

The following sections describe how to perform MSAA render target allocation (#1) for various render target types and platforms.

Allocating a Multisample Render Target

Render-to-FBO

As mentioned, allocation of multisample off-screen FBOs is platform independent. Here's an example:

Render-to-Window

Microsoft Windows (using wgl)

The wgl functions required to create a multisampled OpenGL context are not available until a valid OpenGL context is made current to the thread. This leads to a temporary context code path which can get very involved with platform specific detailed. Users of libraries such as GLUT, GLEW, or GLee can significantly reduce the amount of effort required. The rest of this section assumes that valid context is present and all wgl extension function pointers have been obtained, if not, please see how to create a temporary context.

A valid pixel format for the framebuffer is choose using the wglChoosePixelFormatARB function with a list of attributes to specify the multisampling properties. In order to choose a framebuffer format that incorporates multisampling, you must add it to the list of attributes. For example, this list of attributes does not select a multisampled pixel format:

To consider multisampled visuals, the WGL_SAMPLE_BUFFERS_ARB and WGL_SAMPLES_ARB attributes must be present. The WGL_SAMPLE_BUFFERS_ARB must be set to 1, and WGL_SAMPLES_ARB is the number of samples, e.g. for 8x multisampling, WGL_SAMPLES_ARB would be set to 8. This attribute list is the same as above, but considers 4x multisampled pixel formats too:

Once the correct pixel format is found, creating a context proceeds as normal.

X Windows (using GLX)

This is the method you'd use on UNIX/Linux if you want to create an X window with multisampling capability. There are a few methods for doing this, but the following locates an MSAA FBConfig, looks up its XVisual, and then creates an X window in that visual. You'll notice some similarities with the WGL method above.

MacOS X (using Cocoa)

TBD

Cross-platform (using GLUT)

GLUT is a wrapper around the platform-specific GL integration layers (e.g. WGL, GLX, etc.) which allows you to prototype simple cross-platform GL examples quickly. In GLUT, the key to allocating an MSAA window is this:

glutInitDisplayMode( ... | GLUT_MULTISAMPLE );

which directs GLUT to allocate an MSAA-capable window. The following is a complete example showing how to render with multisampling in a simple GLUT test program:

Conclusion

Setting up FSAA takes a few steps but it is worth it. Today's GPUs are very rapid and the user wants control over the quality of the game's graphics.
On Windows, the extension we are interested in are WGL_ARB_extensions_string which defines wglGetExtensionsStringARB, but the only way to check if this is available is to call wglGetExtensionsStringARB. So this is a chicken and egg situation. Just get the function pointer to wglGetExtensionsStringARB and use it to see if you have WGL_ARB_pixel_format and WGL_ARB_multisample.