Issue with drawing vertex

Hi,

I’m having some trouble renedering simples shapes with Monogame.
Im on Windows 10 and im using the monogame directX template.

I have a basic code that is supposed to draw just a rectangle on screen and rotate it around.
I’m trying to use a vertex buffer and a index buffer and render the whole thing as a TriangleStrip.

I’ve came up with this :

And then i coded it. I understand the fact that monogame is culling my vertex in a counter clock wise direction.
So i indexed them using clock ward direction (points 0-1-2 for the first triangle and 2-3-0 for the second one).

The thing is, when i try to render it on my screen, i get one of them displaying properly and another that is being drawn in the opposite way.
Maybe there is an issue with monogame or (more probably) i didnt understood the concept of indexed primitives correctly.
Here is the result : (for some reasons the gif isnt showing except if you click on it, on my computer at least)

Its supposed to be a simple rectangle rotating on the screen and disapearing when you see his backward face but instead i have 2 triangles facing opposite direction, even tho im respecting the counter clock wise culling thing.

And here is the basic code i used to do it :

public class Game1 : Game
    {
        GraphicsDeviceManager graphics;

        VertexBuffer VertexBuffer { get; set; }
        IndexBuffer IndexBuffer { get; set; }
        BasicEffect effect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
        }
        
        protected override void Initialize()
        {
            // Create the shape into the buffers
            VertexPositionColor[] vertices = new VertexPositionColor[4];

            vertices[0] = new VertexPositionColor(new Vector3(-1, 1, 0), Color.Red);
            vertices[1] = new VertexPositionColor(new Vector3(1, 1, 0), Color.Green);
            vertices[2] = new VertexPositionColor(new Vector3(1, -1, 0), Color.Blue);
            vertices[3] = new VertexPositionColor(new Vector3(-1, -1, 0), Color.Yellow);

            VertexBuffer = new VertexBuffer(graphics.GraphicsDevice, typeof(VertexPositionColor), 4, BufferUsage.WriteOnly);
            VertexBuffer.SetData(vertices);

            short[] indices = new short[6];
            indices[0] = 0; indices[1] = 1; indices[2] = 2;
            indices[3] = 2; indices[4] = 3; indices[5] = 0;

            IndexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(short), 6, BufferUsage.WriteOnly);
            IndexBuffer.SetData(indices);

            base.Initialize();
        }
        
        protected override void LoadContent()
        {
            System.Console.WriteLine(GraphicsDevice.RasterizerState);
            effect = new BasicEffect(GraphicsDevice);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // Draw the shape
            GraphicsDevice.SetVertexBuffer(VertexBuffer);
            GraphicsDevice.Indices = IndexBuffer;

            effect.World = Matrix.CreateTranslation(Vector3.Zero) * Matrix.CreateRotationY((float)gameTime.TotalGameTime.TotalSeconds);
            effect.View = Matrix.CreateLookAt(new Vector3(0, 0, -10), Vector3.Zero, Vector3.UnitY);
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4,
                Window.ClientBounds.Width / (float)Window.ClientBounds.Height,
                1,
                100);
            effect.VertexColorEnabled = true;

            foreach (var pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, 4);
            }

            base.Draw(gameTime);
        }
    }

Can someone tell me where i fucked up ?

From the indices I see you are trying to draw two triangles. Use TriangleList.

what would be the procedure to draw a rectangle with as a triangle strip ?

try [0,1,2,3] as the indices.

which makes using indices a bit redundant in the case of a single quad.

I tried, so using 0-1-2-3 as indices i get this result :

And by reducing the number of primitives from 4 to 2, i get this :

( from DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, 4); to DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, 2);)

So i guess i only need 2 primitives but its still not in the right order.

Thx for your time btw :slight_smile:

1 Like

Triangles are primitives.
So a quad or a square made of 4 points (a quad) requires two triangles or primitives.

instead of going thru it i made a runnable copy paste example.

Thats a nice example, but you are drawing them triangles as a triangle list, and i would like to understand how to do it as a TriangleStrip.

thanks to @nkast i found the solution. I recommend to whatch this : https://www.asawicki.info/news_1524_how_to_flip_triangles_in_triangle_mesh
(If some other people have the same issue)