• 11
• 9
• 10
• 9
• 11

# Monogame wierdness

This topic is 658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites

Well, the bottom rectangle is drawing as a triangle because specify to only draw a single primitive (1 triangle) in your DrawUserIndexedPrimitives call.

Other than that, I suspect your index buffers (or vertex buffers) are incorrect, or some other parameters to DrawUserIndexedPrimitives are wrong.

##### Share on other sites

Good catch, however, rendering two triangles now means it took the first 2 verticies from the polygon instead of only the first, and it looksallmost the same considering how close the vertices of the polygon are together.

At startup, the verticies of the rectangles are defined as:


VertexPositionColor[] rectanglePositions =
{
new VertexPositionColor(new Vector3(0   ,0,0), lineColor),
new VertexPositionColor(new Vector3(1000,0,0), lineColor),
new VertexPositionColor(new Vector3(0   ,200,0), lineColor),
new VertexPositionColor(new Vector3(1000,200,0), lineColor),
new VertexPositionColor(new Vector3(0   ,900,0), lineColor),
new VertexPositionColor(new Vector3(1000,900,0), lineColor)
};

static int[] topRectangle = { 0, 1, 2, 3 };
static int[] bottomRectangle = {2, 3, 4, 5 };


They seem pretty straightforward...

##### Share on other sites

Not sure of this, but I think the numVertices parameter of DrawUserIndexedPrimitives might specify the range of the vertex buffer to draw from. In that case, it should probably be 6, not 4, since your 2nd rectangle uses indices up to 5. (the XNA documentation isn't very clear on this issue)

##### Share on other sites

Thanks, that works. Look at those beautiful rectangles.

[attachment=32460:008.png]