c# - Monogame Vertex Buffer 行为怪异

标签 c# graphics monogame

我费尽了脑筋才来找你帮忙。

我最近开始做一个测试 Monogame 的项目,很快就遇到了一个问题,我不确定是我的错还是 Mono 的错。

我有一个系统,其中一个关卡添加了一堆静态实例(几何体),然后这个几何体被保存到一个单独的类中以渲染它。计划是使用顶点和索引缓冲区并使用 GraphicsDevice.DrawPrimitives,但这是我遇到问题的地方。

上图是应该的样子,下图是实际的样子:

What it looks like when using array mode What it looks like when using buffer mode

这是相关代码。现在将模式设置为 Array 工作正常,但 Buffer 搞砸了,所以我知道顶点添加正确,数组正确,效果正确,只有缓冲区错误。

    public void End()
    {
        _vertices = _tempVertices.ToArray();
        _vCount = _vertices.Length;
        _indices = _tempIndices.ToArray();
        _iCount = _indices.Length;

        _vBuffer = new VertexBuffer(_graphics, typeof(VertexPositionColorTexture),
            _vCount, BufferUsage.WriteOnly);
        _vBuffer.SetData(_vertices, 0, _vCount);

        _iBuffer = new IndexBuffer(_graphics, IndexElementSize.ThirtyTwoBits,
            _iCount, BufferUsage.WriteOnly);
        _iBuffer.SetData(_indices, 0, _iCount);  

        _tempIndices.Clear();
        _tempVertices.Clear();

        _primitiveCount = _iCount / 3;

        _canDraw = true;
    }

    public void Render()
    {
        if (_canDraw)
        {
            switch (DrawMode)
            {
                case Mode.Buffered:
                    _graphics.Indices = _iBuffer;
                    _graphics.SetVertexBuffer(_vBuffer);

                    _graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, _primitiveCount);
                    break;

                case Mode.Array:
                    _graphics.DrawUserIndexedPrimitives<VertexPositionColorTexture>
                        (PrimitiveType.TriangleList, _vertices, 0, _vCount,
                        _indices, 0, _primitiveCount);
                    break;
            }
        }
        else
            throw new InvalidOperationException("End must be called before this can be rendered");
    }

有人知道我在这里缺少什么吗?谢谢。

最佳答案

经过数小时的尝试,我想通了。我可能真的是个白痴。

我没有使用索引绘图,而是尝试绘制非索引图元。

在 Render() 方法中,我简单地改变了

_graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, _primitiveCount);

到:

_graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _vCount, 0, _primitiveCount);

瞧,现在一切正常了。

关于c# - Monogame Vertex Buffer 行为怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21686583/

相关文章:

c# - 如何在 C# 中获取列表中新添加项的索引?

c# - 跨 API 传递不可变集合的最佳模式是什么

c# - Entity Framework 5 - 为什么在将 PropertyValue 设置回 Original 后实体状态为 "Modified"

c# - 使用 Visual Studio 2010 和 Subversion 与 2 个不同的项目共享一个 CS 文件

c++ - 流氓线被绘制到窗口

c# - 方法访问方法困惑

monogame - SpriteFont 渲染伪影

windows-8 - 关于设置 Windows 8 Monogame 项目的最新说明是什么?

java - Java applet 和应用程序图形功能之间的区别

java - 尝试制作棋盘格