4

我正在使用嵌入在 WinForms 中的 XNA,从 Microsoft 网站下载。并且注意到当模型被绘制时,一切看起来都很好,但是当我旋转我的相机时,模型上的边缘开始看起来参差不齐。这是我正在谈论的两张图片:

当相机离目标越近时,这一点就越明显。我正在使用它来绘制每个网格:

foreach (ModelMesh mesh in model.Meshes)
{
    foreach (BasicEffect effect in mesh.Effects)
    {
        effect.View = cam.view;
        effect.Projection = cam.projection;
        effect.World = mesh.ParentBone.Transform;
        effect.EnableDefaultLighting();
    }
    mesh.Draw();
}

有时,如果我旋转模型,会出现一些奇怪的阴影。阴影甚至不会改变它们的“位置”,而是保持完全相同。


编辑:所以我用谷歌搜索了一下,发现启用 MultiSampling 应该可以摆脱锯齿状的边缘。现在,有谁知道如何在 WinForms 中做到这一点?

编辑2:关于后备缓冲区,我没有在任何地方设置它,所以我猜它应该是这样。这是 GraphicsDeviceService.cpp 构造函数:

    GraphicsDeviceService(IntPtr windowHandle, int width, int height)
    {
        parameters = new PresentationParameters();

        parameters.BackBufferWidth = Math.Max(width, 1);
        parameters.BackBufferHeight = Math.Max(height, 1);
        parameters.BackBufferFormat = SurfaceFormat.Color;
        parameters.DepthStencilFormat = DepthFormat.Depth24;
        parameters.DeviceWindowHandle = windowHandle;
        parameters.PresentationInterval = PresentInterval.Immediate;
        parameters.IsFullScreen = false;

        graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter,
                                            GraphicsProfile.Reach,
                                            parameters);
    }

重置方法也相应地设置了 backBuffer。

编辑 3: 我尝试设置MultiSampleCount更大的数字,但没有任何帮助,这里有两张正在发生的事情的图片。首先我在某个地方实例化对象,然后我只向右移动相机。整个物体拉伸并得到这些锯齿状边缘,如下图所示。这是相机移动代码:

http://img152.imageshack.us/img152/5204/normalu.png

http://img28.imageshack.us/img28/4434/movedright.png

KeyboardState state = Keyboard.GetState();

Vector3 v;

if (state.IsKeyDown(Keys.Up) || state.IsKeyDown(Keys.W))
    v = new Vector3(0, 0, 1) * moveSpeed;
else if (state.IsKeyDown(Keys.Down) || state.IsKeyDown(Keys.S))
    v = new Vector3(0, 0, -1) * moveSpeed;
else if (state.IsKeyDown(Keys.Left) || state.IsKeyDown(Keys.A))
    v = new Vector3(1, 0, 0) * moveSpeed;
else if (state.IsKeyDown(Keys.Right) || state.IsKeyDown(Keys.D))
    v = new Vector3(-1, 0, 0) * moveSpeed;
else if (state.IsKeyDown(Keys.PageUp))
    v = new Vector3(0, -1, 0) * moveSpeed;
else if (state.IsKeyDown(Keys.PageDown))
    v = new Vector3(0, 1, 0) * moveSpeed;
else
    v = new Vector3(0, 0, 0);

view *= Matrix.CreateTranslation(v);

以及相机视图和投影:

view = Matrix.CreateLookAt(cameraPos, Vector3.Zero, Vector3.Up);
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f), ratio, 0.5f, 50f);

cameraPos 在这种情况下0,0,10

编辑:阴影 我最终得到了阴影问题的图片。如您所见,每个网格都有自己的阴影,而不是只有一个阴影穿过所有网格。

http://img843.imageshack.us/img843/8263/weirdshadowing.png

这是模型的俯视图。

4

1 回答 1

3

Anti-Aliasing for XNA embedded in WinForms

This question is actually a duplicate of this question.

Download the XNA/WinForms Sample here.

In your GraphicsDeviceService class file navigate to the constructor:

GraphicsDeviceService(IntPtr windowHandle, int width, int height)
{
    parameters = new PresentationParameters();

    // Add this line
    // Increase the count to get higher quality anti-aliasing
    parameters.MultiSampleCount = 8;

    // More parameter settings and initialization
    // ...
}

You can see the difference in the image below:

with and without multisampling comparison


Old answer

Anti-Aliasing for Standard XNA Application

graphics.PreferMultiSampling = true;

enables anti-aliasing for your backbuffer as explained in this MSDN article.

The PreferMultiSampling property is a member of the GraphicsDeviceManager class.

Increase/Decrease Quality

The MultiSampleCount property of the PresentationParameter class can be used to change the number of samples. More samples per pixel means less artifacts and increased render time.


Further Reading on Multisampling


Please note that this solution will only be effective if the source of the problem lies within the graphics pipeline, more specifically the rasterizer.

As noted by Nico Schertler, make sure that the image resolution is not changed in the process of embedding it into WinForms. If the steps on the edges are not exactly one pixel wide it is a strong indicator that the problem comes from the presentation of the texture in WinForms.

I cannot address the shadow problem here, as it is hard to guess the light settings from the images.

于 2012-11-18T21:36:28.967 回答