1

I'm trying to draw a polygon of a solid color. I'm given a vector of 2D vectors, ordered such that there will be an edge from v0 to v1 to v2 to ... to vn to v0.

Does such a primitive exist?

truvaking
  • 347
  • 2
  • 10
  • Don't want to mark this as a duplicate, but does this answer help you? https://stackoverflow.com/questions/33136388/monogame-xna-draw-polygon-in-spritebatch – Ian H. Mar 29 '20 at 15:08
  • i did that, played around but couldn't figure how to change the shape, by modifying coordinates of each vertices I either got a rectangle or a full screen thingy. – truvaking Mar 29 '20 at 16:46

1 Answers1

2

Credit to Stefan Agartsson for the post this answer's code is based upon.

To describe the vertices as edges instead of triangles, You can use the PrimitiveType.LineStrip instead of PrimitiveType.TriangleList you have to adjust the index and counts as well since each vertex is used only once.

BasicEffect basicEffect = new BasicEffect(device);
basicEffect.Texture = myTexture;
basicEffect.TextureEnabled = true;

VertexPositionTexture[] vert = new VertexPositionTexture[4];
vert[0].Position = new Vector3(0, 0, 0); //These connect in order
vert[1].Position = new Vector3(100, 0, 0);
vert[2].Position = new Vector3(100, 100, 0);
vert[3].Position = new Vector3(0, 100, 0);

vert[0].TextureCoordinate = new Vector2(0, 0);
vert[1].TextureCoordinate = new Vector2(1, 0);
vert[2].TextureCoordinate = new Vector2(1, 1);
vert[3].TextureCoordinate = new Vector2(0, 1);

short[] ind = new short[n+1]; // the +1 is to close the shape
for(int i = 0; i < n; i++)
   ind[i] = i;
ind[n] = 0; // return to start to close.

Then in your render loop you do this:

foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes) 
{
    effectPass.Apply();
    device.DrawUserIndexedPrimitives<VertexPositionTexture>(
        PrimitiveType.LineStrip, vert, 0, vert.Length, ind, 0, ind.Length);
}