So, I needed to draw two filled rectangles and a polygon in Monogame. Now, based on my research, the normal way seemed to be making an orthographic camera that mapped 3d coordinates exactly to screen coordinates, then using drawUserIndexedPrimitives to draw arbitrary rectangles to the screen space.
The code for the projection is:
effect = new BasicEffect(graphics.GraphicsDevice);
effect.World = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, -1, 1);
effect.DiffuseColor = Color.Red.ToVector3();
I use the same effect for all 3 shapes. (Should I?)
Next, my drawing code:
foreach (EffectPass pass in effect.Techniques[0].Passes)
{
pass.Apply();
GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, rectanglePositions,
0, 4, topRectangle, 0, 2);
}
effect.DiffuseColor = backgroundColors[colorIndex+1].ToVector3();
foreach (EffectPass pass in effect.Techniques[0].Passes)
{
pass.Apply();
GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, rectanglePositions,
0, 4, bottomRectangle, 0, 1);
}
spriteBatch.Begin();
...
effect.DiffuseColor = Color.Red.ToVector3();
if (polygon.Count > 4)
{
foreach (EffectPass pass in effect.Techniques[0].Passes)
{
pass.Apply();
GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineStrip, polygon.ToArray(), 0, polygon.Count, indexData, 0, polygon.Count);
}
}
The vertexes for the two rectangles are stored in the same array, since they share two vertices, which need to be updated every frame.
[attachment=32444:006.png]
However, before the polygon exists (When polygon.Count <= 3), only the top rectangle is drawn. Afterwards, the bottom rectangle seems to make a wierd triangle from the right first two vertices, but ending at the first vertices of the polygon. The triangle also flickers to other verticies occasionally. Am I misunderstanding how drawUserIndexedPrimitives works, do I need to call some reset function in between draws, or what am I doring wrong?