# How can I draw multiple vertexbuffers with indices?

I'm using to types of vertices.

For the triangles:

 0 Vector3 Position,
12 Color Color,
16 Vector3 Normal


For the lines:

 0 Vector3 Position,
12 Color Color


I setup a vertex buffer for each type, and adding them to the GraphicsDevice using VertexBufferBindings. My problem is that I'm only able to draw vertices from the buffer at index 0 of VertexBufferBinding-Array.

//
// This is only a shortened part of the code, which draws the lines but NOT the triangles
//
//
protected override void Initialize()
{
//Triangles
avl = new AllrounderVertexList(new VertexElementUsageDescriptor[]{
new VertexElementUsageDescriptor(VertexElementUsage.Position,0),
new VertexElementUsageDescriptor(VertexElementUsage.Color,0),
new VertexElementUsageDescriptor(VertexElementUsage.Normal,0)
});
//Clock- and counterclockwise
avl.AddAllrounderVertexList(new Vector3(0, 0, 0), Color.Orange, new Vector3(0, -1, 0));
avl.AddAllrounderVertexList(new Vector3(0, 10, 0), Color.Yellow, new Vector3(0, -1, 0));
avl.AddAllrounderVertexList(new Vector3(0, 0, 10), Color.Navy, new Vector3(0, -1, 0));
avl.AddAllrounderVertexList(new Vector3(0, -10, 0), Color.Gray, new Vector3(0, -1, 0));

//Lines
lines = new AllrounderVertexList(new VertexElementUsageDescriptor[]{
new VertexElementUsageDescriptor(VertexElementUsage.Position,0),
new VertexElementUsageDescriptor(VertexElementUsage.Color,0)
});
Color xCol = Color.Red;
Color yCol = Color.Green;
Color zCol = Color.Blue;
for (int i = 0; i <= 50; i+=5)
{

}
short[] lineIndices = new short[lines.Count];
for (int i = 0; i < lineIndices.Length; i++)
lineIndices[i] = (short)i;
indices = new short[6];
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 0;
indices[4] = 3;
indices[5] = 2;

//Adding indices for the 2 triangles at the end
short[] tmp = new short[indices.Length + lineIndices.Length];
Array.Copy(lineIndices, tmp, lineIndices.Length);
Array.Copy(indices, 0, tmp, lineIndices.Length, indices.Length);
avl.AvgNormalize(indices);
indices = tmp;

triangleBuffer = new VertexBuffer(GraphicsDevice, avl.vertexDeclaration, avl.Count, BufferUsage.None);
triangleBuffer.SetData<byte>(avl.Data);
indexBuffer = new IndexBuffer(GraphicsDevice, typeof(ushort), indices.Length, BufferUsage.None);
indexBuffer.SetData<short>(indices);
lineBuffer = new VertexBuffer(GraphicsDevice, lines.vertexDeclaration, lines.Count, BufferUsage.None);
lineBuffer.SetData<byte>(lines.Data);
bindings = new VertexBufferBinding[2];
bindings[0] = new VertexBufferBinding(lineBuffer, 0, 0);
bindings[1] = new VertexBufferBinding(triangleBuffer, 0, 0);
GraphicsDevice.SetVertexBuffers(bindings);
GraphicsDevice.Indices = indexBuffer;
}
VertexBuffer triangleBuffer;
VertexBuffer lineBuffer;
IndexBuffer indexBuffer;
protected override void Draw()
{foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();

GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,lines.Count, 0, 6, lineBuffer.VertexCount, 2);
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, lineBuffer.VertexCount, 0, lineBuffer.VertexCount / 2);
}
}


Do I need to switch somehow between the vertex buffers? Or what am I doing wrong?

Before doing each draw call you need to bind the buffers appropriate to that draw. So you'd first set the vertex/index buffers for triangles to the GraphicsDevice, then do the DrawIndexedPrimitives call(s) for the triangles; then set the buffers for the lines and draw the lines. You'd only have one vertex buffer set at a time, so you'd only use a one-element VertexBufferBinding array.
• @user0817 Pretty much, yeah. The data might be in VRAM or main RAM depending on the BufferUsage setting when creating it, but wherever it is, it's accessible to the GPU and SetVertexBuffers just points to it. Jan 6, 2012 at 18:40