4
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\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, 2011 at 16:02

1 Answer 1

2
\$\begingroup\$

@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; 
        } 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ "googling with bing" - Nice one! \$\endgroup\$ Jun 15, 2011 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .