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

  • \$\begingroup\$ 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. \$\endgroup\$ – 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); 
                numberIndices = mesh.IndexBuffer.SizeInBytes / sizeof(int); 

            totalNumVertices += numberVertices; 
            totalNumFaces += numberIndices / 3; 
  • 1
    \$\begingroup\$ "googling with bing" - Nice one! \$\endgroup\$ – Jonathan Connell Jun 15 '11 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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