3dxnaxna-4.0multisampling

Method for high quality output in XNA 4.0


I'm using this example project's XNA 4.0 form control in an application I'm writing: http://creators.xna.com/en-US/sample/winforms_series1

It's working great and I've done quite a bit with visuals and animation. The main issue I'm scratching my head at is the 3d model and primitive 3D shapes (cylinders with a tessellation of 30) I render have very jagged edges to them as if they are low resolution.

I tried to figure out how to enable multisampling, but all of the examples I can find online don't seem to apply to this novel way of using XNA in the custom form control.

Inside the GraphicsDeviceService() constructor there is a PresentationParameters object created, but the only property available is parameters.MultiSampleCount of type integer. I tried setting that with no effect.

I also attempted making the back-buffer twice as large as the control's size (GraphicsDeviceService.cs):

    GraphicsDeviceService(IntPtr windowHandle, int width, int height)
    {
        parameters.BackBufferWidth = width * 2;
        parameters.BackBufferHeight = height * 2;
        ...
    }

Then I changed this function (GraphicsDeviceControl.cs):


    void EndDraw()
    {
        Rectangle sourceRectangle = new Rectangle(0, 0, ClientSize.Width * 2, ClientSize.Height * 2);
        Rectangle destinationRectangle = new Rectangle(0, 0, ClientSize.Width, ClientSize.Height);
        GraphicsDevice.Present(sourceRectangle, destinationRectangle, this.Handle);
    }

But that didn't work properly. My objects rendered to the screen were relegated to 1/4th of the window and clipped. It did look slightly less jagged though...

So at this point I'm not sure what I can do to get high quality graphics using this method (XNA control in a window). I'm pretty new to XNA in general, so any suggestions would be most helpful.


Solution

  • I downloaded that code sample to see where the PresentationParameters were being set up. These are what you need to modify.

    It's in the GraphicsDeviceService class.

    In the constructor of this class, it is setting up an object called "parameters", a PresentationParamters object. Add this line after the new and before passing the parameters to the graphicsDevice:

    parameters.MultiSampleCount = 10;
    

    That value I picked arbitrarily. It provides a healthy antialiasing. Read more about antialiasing if you need to understand what this number exactly is. It's the number of passes through the antialias filter. So you may want to lower it for better performance, or raise it for more antialiasing.