1
\$\begingroup\$

In my project when I add graphics.PreferMultiSampling = true; this line in the Game1 Constructor (as the msdn website suggests), anti aliasing does not enable. Any idea what I might be missing ? Here is the full code:

public Game1() : base()
{
    graphics = new GraphicsDeviceManager(this);
    graphics.PreferredBackBufferWidth = 720;  // game's width
    graphics.PreferredBackBufferHeight = 720; // game's height
    graphics.PreferMultiSampling = true;
    Content.RootDirectory = "Content";
}

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);
    LightningSegment = Content.Load<Texture2D>("Lightning Segment");
}

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.DarkCyan);

    Matrix skew = Matrix.Identity;
    skew.M12 = (float)Math.Tan((double)Math.PI / 6);
    spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, skew *
         Matrix.CreateScale(1.5f, 3f, 1));
    spriteBatch.Draw(LightningSegment, new Vector2(200, 200), null, Color.Red, 0, 
         new Vector2(0f, 300f / 2f), new Vector2(250f, 1f), SpriteEffects.None, 0f);
    spriteBatch.End();

    base.Draw(gameTime);
}
\$\endgroup\$
3
\$\begingroup\$

First of all your hardware has to support MultiSampling. You can check this with the below property.

GraphicsAdapter.CheckDeviceMultiSampleType

In addition to enabling it for the back buffer

graphics.PreferMultiSampling = true;

You can create a new RasterizerState that has the field MultiSampleAntiAlias set to true and pas this into SpriteBatch.Begin.

Creating the needed RasterizerState.

RasterizerState = new RasterizerState { MultiSampleAntiAlias = true };

If you want the default state pass in null.

SpriteBatch.Begin(
    SpriteSortMode.Immediate,
    null,
    null,
    null,
    RasterizerState);

The above can cause changes to the graphics card's state even after SpriteBatch.End is called. Use the below if you want to revert the state back to the defualt.

GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ how would i use that when drawing primitives ? \$\endgroup\$ – dimitris93 Feb 4 '15 at 21:33
  • \$\begingroup\$ See this answer about when to use ApplyChanges. \$\endgroup\$ – Andrew Russell Feb 5 '15 at 3:30
  • \$\begingroup\$ Also note that the default for MultiSampleAntiAlias is true, and it is true for the default RasterizerState for SpriteBatch.Begin. So in most circumstances you should not need to worry about it. \$\endgroup\$ – Andrew Russell Feb 5 '15 at 3:48
  • \$\begingroup\$ @AndrewRussell i still can not enable anti aliasing, i posted a more detailed question here gamedev.stackexchange.com/questions/95658/… \$\endgroup\$ – dimitris93 Feb 25 '15 at 16:16
3
\$\begingroup\$

If anyone here is using MonoGame instead of XNA, the process is ever so slightly different. Notice that we change the graphics profile to HiDef. This allows more advanced shader features (like AA) to be enabled (as some are disabled when the default Reach profile is enabled).

protected override void Initialize()
{
    _graphics.GraphicsProfile = GraphicsProfile.HiDef;
    _graphics.PreferMultiSampling = true;
    GraphicsDevice.PresentationParameters.MultiSampleCount = 8;
    _graphics.ApplyChanges();

    // your stuff here
}

The MultiSampleCount can be either 2, 4, 8, 16, or 32. I would only go up to 8 maximum though since not all computers support higher than that. 4 is the safest bet.

This should work on both the DX and GL versions.

If you'd like to know how to do this with RenderTargets, have a look at this useful post by Kosmonaut Games.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Well, since you asked for "properly" I would recommend shader based AA, but it depends on specific project. Look for FXAA if you want cheap AA, or there is SMAA for XNA somewhere around (although not as effective as its common implementation due to XNA stencil limitations).

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.