2D Primitives, strange rendering issue

I’m developing a game which uses a great deal of “hand drawn” 2D rendering. That is, I draw lines, rectangles and whatnot instead of using a lot of sprites. I found a good, free 2D primitives library that does most of the heavy lifting.

It doesn’t have polygon support, though, so I found another solution. And let me tell you, the 2D primitives support for MonoGame in the wild is sorely lacking.

Back on track, it renders the primitives fine, but it they render with a “tunnel” look to them:
image

The triangle and square show the bizarre rendering issue. They’re supposed to be solid colors. The trapezoid is the only one that looks decent, but that’s because it’s supposed to be all black.

I suspect the issue is the effect the original developer used. He inherited from BasicEffect to create his own effect and did this in the constructor:

public StandardBasicEffect(GraphicsDevice graphicsDevice)
        : base(graphicsDevice)
        {
            VertexColorEnabled = true;

            Projection = Matrix.CreateOrthographicOffCenter(
                0, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, 0, 0, 1);
        }

I’m guessing that Projection he specified is causing the strange rendering. I tried changing it up, but my Projection knowledge is weak sauce. Whenever I changed it, my primitives just disappear.

When rendering the polygon, he does this:

        private void draw(BasicEffect effect)
        {
            effect.CurrentTechnique.Passes[0].Apply();
            _graphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                PrimitiveType.TriangleList, _triangulatedVertices, 0, _vertices.Length);
        }

Like I said, it works–the polygons appear in the correct place and size–but they render with a “tunnel” effect.

Anyone have any insight as to what might be going on or how to resolve it?

That projection matrix shouldn’t be causing any problems, all it does is converts coordinates to the same coordinate system that SpriteBatch uses (so that the topleft corner is 0,0, and positive Y-axis is downwards). I use the same matrix in my code. (I originally found that matrix code from here)

I don’t know what’s causing your issue but have you considered using Monogame.Extended’s libraries for drawing primitives? It has PrimitiveBatch and PrimitiveDrawing class, which has methods like DrawSolidPolygon. If you just need to draw a triangle, you can call PrimitiveBatch.AddVertex(Vector2, Color, PrimitiveType.TriangleList); for each of the triangle’s 3 vertices.

public class Game1 : Game
{
    ...

    private Matrix PrimitivesProjectionMatrix;
    private PrimitiveBatch PB;
    private PrimitiveDrawing PD;

    protected override void Initialize()
    {
        PrimitivesProjectionMatrix = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);
        PB = new PrimitiveBatch(GraphicsDevice, 1024);
        PD = new PrimitiveDrawing(PB);
    }

    protected override void Draw(GameTime gameTime)
    {
        PB.Begin(PrimitivesProjectionMatrix, Matrix.Identity);
        PD.DrawSolidPolygon(Vector2.Zero, vertices, Color.White, false);
        PB.End();
    }
}
1 Like

Looks like your triangle has a vertex in the center, and it’s actually made up of 3 sub triangles.

Looks like the vertex color for the center vertex is black.

3 Likes

Wow, in less than 24 hours I got two excellent responses!

@markus : You were absolutely correct! I dug through the code and discovered the original author didn’t set the colors for the vertexes, so they were still at the default of Black. I fixed that, and:
image

Tada! Works!

@Videogamers0 : I didn’t know about the MonoGame.Extended library! I’m definitely going to check it out. It will probably even be a better solution than the one I’m working worth.

Much obliged to you both! You got me moving forward again! Thank you!