Monogame顶点缓冲表现异常

3
我已经用尽了我的脑力,现在来向你求助。
最近我开始着手测试Monogame项目,但很快就遇到了一个问题,我不确定是我的问题还是Mono的问题。
我有一个系统,其中一个级别包含一堆静态实例(几何体),然后将这些几何体保存到一个单独的类中以渲染它们。计划是使用顶点和索引缓冲区,并使用GraphicsDevice.DrawPrimitives,但这就是我遇到问题的地方。
上面的图片是应该看起来的样子,下面的图片是实际的样子:
https://istack.dev59.com/tIgQB.webp https://istack.dev59.com/MlLXY.webp 以下是相关代码。目前将模式设置为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");
    }

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

3
我经过几个小时的尝试终于解决了问题。可能我真的很蠢。
我一直在尝试绘制非索引图元,而不是使用索引绘图。
在 Render() 方法中,我仅仅改变了

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

to:

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

看,现在一切都正常工作了。


这让我也在墙上猛撞了几个小时。感谢您发布自己问题的答案! - anton.burger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接