我费尽了脑筋才来找你帮忙。
我最近开始做一个测试 Monogame 的项目,很快就遇到了一个问题,我不确定是我的错还是 Mono 的错。
我有一个系统,其中一个关卡添加了一堆静态实例(几何体),然后这个几何体被保存到一个单独的类中以渲染它。计划是使用顶点和索引缓冲区并使用 GraphicsDevice.DrawPrimitives,但这是我遇到问题的地方。
上图是应该的样子,下图是实际的样子:
这是相关代码。现在将模式设置为 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/