0
\$\begingroup\$

I have studying XNA on my spare time for about a year now and I could use some professional help on this issue. Any time given to my problem is appreciated.

I have two VertexPositionColor variables both of which are placed into two separate vertex buffers to draw up three triangles

basicEffect = new BasicEffect(GraphicsDevice);
        VertexPositionColor[] vertices = new VertexPositionColor[6];
        vertices[0] = new VertexPositionColor(new Vector3(0, 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(0.05f, 1, 0), Color.Yellow);
        vertices[4] = new VertexPositionColor(new Vector3(-1, -1, 0), Color.Blue);
        vertices[5] = new VertexPositionColor(new Vector3(-1, 1, 0), Color.Pink);
        vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), 6, BufferUsage.WriteOnly);
        vertexBuffer.SetData<VertexPositionColor>(vertices);

        VertexPositionColor[] vertices2 = new VertexPositionColor[3];
        vertices2[0] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Blue); 
        vertices2[1] = new VertexPositionColor(new Vector3(-1, -1, 0), Color.Red); 
        vertices2[2] = new VertexPositionColor(new Vector3(0.9f, -0.9f, 0), Color.Orange);
        vertexBuffer2 = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), 6, BufferUsage.WriteOnly);
        vertexBuffer2.SetData<VertexPositionColor>(vertices2);

I then use the draw function to draw all three triangles ..

public override void Draw(GameTime gameTime)
    {
        basicEffect.World = world;
        basicEffect.View = view;
        basicEffect.Projection = projection;
        basicEffect.VertexColorEnabled = true;
        GraphicsDevice.SetVertexBuffer(vertexBuffer);
        GraphicsDevice.SetVertexBuffer(vertexBuffer2);
        RasterizerState rasterizerState = new RasterizerState();
        rasterizerState.CullMode = CullMode.None;
        GraphicsDevice.RasterizerState = rasterizerState;
        foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
        {
            pass.Apply();
            GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 3);

        }
        base.Draw(gameTime);
    }

But it does not draw all three triangles. I have tried various variations and all that gets drawn is either the last vertex buffer or the first one. I know it would be more efficient to place them all in the same buffer or have them used with indices, but in the future I will run into issues where I am going to need more than one buffer to draw something and I want to be prepared. What is the problem here and how do I draw more than one vertex buffer at a time?

\$\endgroup\$
0
\$\begingroup\$

Do your draw calls for each vertex buffer you set, with the corresponding number of primitives as the third parameter in DrawPrimitives. Calling GraphicsDevice.SetVertexBuffer two times will replace the first vertex buffer.

Your draw function should look similar to this:

public override void Draw(GameTime gameTime)
{
    basicEffect.World = world;
    basicEffect.View = view;
    basicEffect.Projection = projection;
    basicEffect.VertexColorEnabled = true;
    RasterizerState rasterizerState = new RasterizerState();
    rasterizerState.CullMode = CullMode.None;
    GraphicsDevice.RasterizerState = rasterizerState;
    GraphicsDevice.SetVertexBuffer(vertexBuffer);
    foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
    {
        pass.Apply();
        GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);

    }
    GraphicsDevice.SetVertexBuffer(vertexBuffer2);
    foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
    {
        pass.Apply();
        GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

    }
    base.Draw(gameTime);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.