# Count number of Vertices in VertexBuffer in XNA 3.1

Where in XNA 4.0 one can go:

 myVertexBuffer.VertexCount;


What is the best way to count the number of vertices in 3.1 for a dynamic buffer (i.e. where I do not know the number of elements that could be therein). I have come up with:

myVertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes


Where my vertices are VertexPositionNormalTexture's, but is this the best way? (it will be called often in Draw()).

• That's (likely) the only way. Or, close enough. IIRC the vertex declaration type has a 'vertex stride' parameter that you should use rather than the size in bytes. If your vertex buffer is from a model, you should use the vertex declaration from the model, since it might have additional elements (meaning the stride is larger than VertexPositionNormalTexture.) Not an answer since I can't confirm anything; I don't have 3.1 installed. – Blecki Jun 12 '11 at 16:02

@Blecki's answer is correct, I also don't have 3.1 installed, but some quick googling with bing shows a few examples.

foreach (ModelMesh mesh in model.Meshes)
{
int numberVertices, numberIndices;
numberVertices =
mesh.VertexBuffer.SizeInBytes / mesh.MeshParts[0].VertexStride;
if (mesh.IndexBuffer.IndexElementSize == IndexElementSize.SixteenBits)
numberIndices = mesh.IndexBuffer.SizeInBytes / sizeof(short);
else
numberIndices = mesh.IndexBuffer.SizeInBytes / sizeof(int);

totalNumVertices += numberVertices;
totalNumFaces += numberIndices / 3;
}

• "googling with bing" - Nice one! – Jonathan Connell Jun 15 '11 at 14:31