Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)
{
    lines.AddAllrounderVertexList(new Vector3(-50, i, 0), yCol);
    lines.AddAllrounderVertexList(new Vector3(50, i, 0), yCol);
    lines.AddAllrounderVertexList(new Vector3(i, -50, 0), xCol);
    lines.AddAllrounderVertexList(new Vector3(i, 50, 0), xCol);
    lines.AddAllrounderVertexList(new Vector3(0, -50, i), zCol);
    lines.AddAllrounderVertexList(new Vector3(0, 50, i), zCol);

}
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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

Setting multiple vertex buffers at once is done when you want to use instancing, or when you want to have some vertex components in one array and others in a different array (this is done in some cases for performance reasons, or to allow some vertex components to be dynamic and others static, etc.) All the components from all of the currently-bound vertex buffers act as inputs to the vertex shader.

share|improve this answer
    
So do vertex buffers save their data automatically on the graphic cards ram and setVertexBuffer only points to the memory to be used? –  user0817 Jan 6 '12 at 18:28
    
@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. –  Nathan Reed Jan 6 '12 at 18:40
    
This explains why I could not find any relating stuff with google. Thank you for your answer. –  user0817 Jan 6 '12 at 19:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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