Reputation: 109
I've exhausted my brain and have come to you for help.
I recently started working on a project to test out Monogame, and quickly ran into an issue, I'm not sure if it's my fault or Mono's.
I have a system where a level has a bunch of static instances added to it (the geometry), then this geometry is saved to a separate class to render it all. The plan was to use vertex and index buffers and use GraphicsDevice.DrawPrimitives, but this is where i run into issues.
The top image is what it should look like, the bottom one is what it actually looks like:
And here is the relevant code. Right now setting the mode to Array works fine, but Buffer is messed up, so i know that the vertices are being added right, and the arrays are right, and the effect is right, only the buffers are wrong.
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");
}
Anyone have any idea what I'm missing here? Thanks.
Upvotes: 3
Views: 1250
Reputation: 109
I figured it out, after hours of trying everything. I may actually be an idiot.
Instead of using indexed drawing, I was simply trying to draw non-indexed primitives.
In the Render() method, i simply changed
_graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, _primitiveCount);
to:
_graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _vCount, 0, _primitiveCount);
And voila, it all works now.
Upvotes: 3