Export (0) Print
Expand All

Dynamic Vertex Buffers in XNA

Describes several techniques for implementing dynamic vertex buffers in an XNA Framework game.

Geometry in a 3D game is ultimately defined as a set of vertices. Commonly, a game needs to modify the position of one or more existing vertices or must generate the vertices at run time. Because the grapics device can access these buffers at any time, take care when modifying existing vertex buffers and when rendering the resulting geometry defined by these buffers. The following sections discuss several techniques you can implement in your game.


For index or vertex buffers that are frequently updated during game runtime, use the DynamicIndexBuffer and DynamicVertexBuffer classes. They are optimized to render faster when updated frequently.

Use VertexBuffer and IndexBuffer when index or vertex buffer data is infrequently updated.

Updating a Set of Primitives Dynamically

The DrawUserPrimitives method can be used as the basis for rendering a set of primitives that are dynamically generated each frame. The Primitives Sample, located on the XNA Creators Club Online Web site, implements an abstraction layer around this method similar to the SpriteBatch class.

The sample renders primitives by first calling Begin, adding the necessary vertices, using the Add method, and then calling End. This forces the buffer to be drawn to the current device. The Flush method calls DrawUserPrimitives method when End is called or when the buffer has no room for new vertices. If there is no room, the buffer is written to the device, reset and the pending vertices are added.

For a complete code listing, download the Primitives Sample from the XNA Creators Club Online Web site.

Dynamically Rendering a Persistent Set of Primitives

The Particle 3D Sample, located on the XNA Creators Club Online Web site, implements a dynamic vertex buffer that contains custom vertices with a limited lifespan. Particles are added and removed by the application to a fixed length buffer. The active subset of vertices are rendered dynamically by the custom shader of the sample.

Because particles have a limited lifespan, the sample code must manage the vertex buffer in real time. The ParticleSystem class handles all adding, updating, and deleting.

Generating Geometry Programmatically

Sometimes, geometry to be rendered is either not known at design-time or changes based upon events during run time. For cases like this, you can use the following procedure to programmatically generate geometry that can be rendered at run time.

  1. Create a vertex buffer to store the vertices of the geometry to be generated.

  2. Create an index buffer that stores the indices used to render the geometry represented by the vertex buffer.

  3. Once the vertex and index data have been generated, call SetData for the vertex buffer and SetData for the index buffer.

  4. In the method that renders your frame (the Draw method for a default XNA game project), set the vertex stream and index data of the current graphics device. For a specific example of this, see DrawIndexedPrimitives.

  5. Call DrawIndexedPrimitives.

For a more advanced solution for this problem, download the Generated Geometry Sample from the XNA Creators Club Online Web site.

This sample uses the MeshBuilder helper class and a custom processor to generate a terrain map from a bitmap loaded by the content manager.

Specifically, examine the Process method, located in TerrainProcessor.cs. This method programmatically creates the terrain geometry based on input from the specified bitmap.

Community Additions

© 2015 Microsoft