Table of Contents

How to enable anti-aliasing

Demonstrates how to enable anti-aliasing for your game.

Overview

Figure 1.  Anti-aliasing the edges of a cube: multi-sampling is disabled on the left, and enabled on the right.

Anti-aliasing the edges of a cube: multi-sampling is disabled on the left, and enabled on the right

Anti-aliasing is a technique for minimizing distortion artifacts caused by aliasing when rendering a high-resolution signal (such as a sharp edge) at a low resolution (such as in a render target with a fixed number of pixel locations). anti-aliasing smooths sharp edges by partially rendering to neighboring pixels.

This technique is also called multi-sampling because each pixel value can be the result of multiple samples.

To enable anti-aliasing in your game

  1. Render 3D geometry. One way to do this is by creating a BasicEffect using the BasicEffect class. For more detail, see Creating a Basic Effect.

  2. Set PreferMultiSampling to true in your Game class constructor.

    graphics.PreferMultiSampling = true;
    
  3. Set the view matrix to place the camera close to the object so you can more clearly see the smoothed, anti-aliased edges.

    worldMatrix = Matrix.CreateRotationX(tilt) * Matrix.CreateRotationY(tilt);
    
    viewMatrix = Matrix.CreateLookAt(new Vector3(1.75f, 1.75f, 1.75f), Vector3.Zero, Vector3.Up);
    
    projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
        MathHelper.ToRadians(45),  // 45 degree angle
        (float)GraphicsDevice.Viewport.Width /
        (float)GraphicsDevice.Viewport.Height,
        1.0f, 100.0f);
    
  4. Draw the geometry by calling GraphicsDevice.DrawPrimitives.

    RasterizerState rasterizerState1 = new RasterizerState();
    rasterizerState1.CullMode = CullMode.None;
    graphics.GraphicsDevice.RasterizerState = rasterizerState1;
    foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
    {
        pass.Apply();
    
        graphics.GraphicsDevice.DrawPrimitives(
            PrimitiveType.TriangleList,
            0,
            12
        );
    }
    

See Also

Concepts

Reference

© 2012 Microsoft Corporation. All rights reserved.