Create a Vertex Declaration

This example demonstrates how to create a vertex declaration.

[C#]

As shown in the following C# code example, first declare a VertexElement array to hold the vertex shader declaration. The declaration array must end with VertexElement.VertexDeclarationEnd as the last element (the size of the vertex element array will be one more than the number of actual vertex elements).

Create a VertexDeclaration instance using the Device and VertexElement array previously created.

Note: The offset parameter of each element is the cumulative offset of the elements from the start of the declaration. For example, the second element is offset 12 bytes, since it is three floats of four bytes each, or (3 * sizeof(float) = 12).

// Create the vertex element array.
VertexElement[] elements = new VertexElement[]
{
    new VertexElement(0, 0, DeclarationType.Float3,
                            DeclarationMethod.Default,
                            DeclarationUsage.Position, 0),
                            
    new VertexElement(0, 12, DeclarationType.Float3,
                             DeclarationMethod.Default,
                             DeclarationUsage.Normal, 0),
                            
    new VertexElement(0, 24, DeclarationType.Float2,
                             DeclarationMethod.Default,
                             DeclarationUsage.TextureCoordinate, 0),
                            
    VertexElement.VertexDeclarationEnd 
};

// Use the vertex element array to create a vertex declaration.
VertexDeclaration decl = new VertexDeclaration(device, elements);