# XNA - multiple VertexBuffers?

I'm trying to learn how to use VertexBuffers in XNA 4.0. I can render wireframe shapes and I can render textured shapes. However, I'm having some trouble rendering them both at once. I'm initializing the buffers like this:

vertexBuffer1 = new VertexBuffer(graphics.GraphicsDevice, vertexDeclarationWireframe, numPointsWireframe, BufferUsage.None);
vertexBuffer1.SetData<VertexPositionColor>(pointList1);

vertexBuffer3 = new VertexBuffer(graphics.GraphicsDevice, vertexDeclarationTexture, numPointsTexture, BufferUsage.None);
vertexBuffer3.SetData<VertexPositionTexture>(pointList3);


The above code fails during Draw() because the vertex buffers are using two different types of vertexDeclarations, one for wireframe (VertexPositionColor) and one for textures (VertexPositionTexture). I tried moving the call to VertexBuffer.SetData() to just before the relevant geometry is drawn but that didn't work.

I've also tried setting my Effect object TextureEnabled = false when wireframe is being rendered and resetting it when the textured geometry is rendering, but that didn't work either. The effect seems to be expecting a TextureCoordinate regardless.

What's the proper way to do this?

• Show us your rendering code, including where you use SetVertexBuffer and DrawPrimitives (or similar). – Andrew Russell Aug 4 '11 at 0:47

All you need to do is set a new RasterizerState object on your GraphicsDevice that specifies wireframe (or not). Specifically to accomplish what you're trying to do here are the steps:

1. Draw your vertex buffer with depth & depth write enabled and filled shading
2. On your GraphicsDevice object set a new RasterizerState that specifies wireframe and a new DepthStencilState that disables both depth testing and depth writing
3. Draw the same vertex buffer again but use a different effect. For example when you draw the wireframe mesh only output a constant color or maybe just vertex colors. If you don't change the Effect being used then the wireframe mesh won't show up because it'll be the same color as the original.

This is all accomplished with only 1 vertex buffer.

• An alternative is to set the FillMode value directly in the shader's technique declaration. Cleaner in some circumstances and doesn't require creating new rasterizer state object. – 3Dave Sep 5 '11 at 15:14
• in the technique delcaration you can set the fillmode to wireframe and DepthBias value to a small negative value too. – Blau Oct 4 '11 at 16:18