Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Where in XNA 4.0 one can go:


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()).

share|improve this question
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
up vote 2 down vote accepted

@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); 
                numberIndices = mesh.IndexBuffer.SizeInBytes / sizeof(int); 

            totalNumVertices += numberVertices; 
            totalNumFaces += numberIndices / 3; 
share|improve this answer
"googling with bing" - Nice one! – Jonathan Connell Jun 15 '11 at 14:31

Your Answer


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.