Share via


VertexBuffer Class (Microsoft.DirectX.Direct3D)

How Do I...?

  • Read and Write VertexBuffer and IndexBuffer Data With GraphicsStreams
  • Read and Write VertexBuffer Data With Arrays

Manipulates vertex buffer resources.

Definition

Visual Basic NotInheritable Public Class VertexBuffer
    Inherits Resource
    Implements IDisposableLeave Site
C# public sealed class VertexBuffer : Resource, IDisposableLeave Site
C++ public ref class VertexBuffer sealed : Resource, IDisposableLeave Site
JScript public final class VertexBuffer extends Resource implements IDisposableLeave Site

Members Table

The following table lists the members exposed by the object.

Events

Event Description
Created Occurs after a device is reset and the VertexBuffer is re-created.
Disposing Occurs when the Dispose method is called or when the VertexBuffer object is finalized and collected by the garbage collector of the .NET common language runtime.

Methods

Method Description
Dispose Immediately releases the unmanaged resources used by the VertexBuffer object.
Equals Returns a value that indicates whether the current instance is equal to a specified object. Inherited from Resource.
Finalize Allows the VertexBuffer object to free resources before it is destroyed by the garbage collector.
FreePrivateData Frees the specified private data associated with the current resource. Inherited from Resource.
GetHashCode Returns the hash code for the current instance. Inherited from Resource.
GetObjectByValue This member supports the infrastructure for Microsoft DirectX 9.0 for Managed Code and is not intended to be used directly from your code.
GetPrivateData Copies the private data associated with a resource to a buffer. Inherited from Resource.
Lock Locks a range of vertex data and obtains the vertex buffer memory.
static (Shared in Visual Basic) op_Equality Compares the current instance of a class to another instance to determine whether they are the same. Inherited from Resource.
static (Shared in Visual Basic) op_Inequality Compares the current instance of a class to another instance to determine whether they are different. Inherited from Resource.
PreLoad Preloads a managed resource. Inherited from Resource.
raise_Created Raises a VertexBuffer.Created event when called from within a derived class.
raise_Disposing Raises the Microsoft.DirectX.Direct3D.VertexBuffer.Disposing event when called from within a derived class.
SetData Locks, sets, and unlocks a range of vertex data.
SetPriority Assigns the resource-management priority for the current resource. Inherited from Resource.
SetPrivateData Associates data with the resource that is intended for use by the application. Data is passed by value, and multiple sets of data can be associated with a single resource. Inherited from Resource.
Unlock Unlocks vertex data.
UpdateUnmanagedPointer Updates the unmanaged pointer for this Resource object. This method supports the Microsoft .NET Framework infrastructure and is not intended to be used directly in your code. Inherited from Resource.
UpdateUnmanagedPointer Updates the unmanaged pointer for this VertexBuffer object. This method supports the Framework infrastructure and is not intended to be used directly in your code.
VertexBuffer Creates a new instance of the VertexBuffer class.

Properties

Property Description
Description Retrieves a description of the vertex buffer resource.
Device Retrieves the device associated with a resource. Inherited from Resource.
Disposed Gets a value that indicates whether the object is disposed.
Priority Retrieves or sets the priority for the current resource. Inherited from Resource.
SizeInBytes Retrieves the size of the VertexBuffer data, in bytes.
Type Retrieves the type of a resource. Inherited from Resource.
UnmanagedComPointer Returns the unmanaged Component Object Model (COM) IDirect3DResource9 interface pointer. Inherited from Resource.
UnmanagedComPointer Returns the unmanaged COM IDirect3DVertexBuffer9 interface pointer.

Inheritance Hierarchy

ObjectLeave Site

MarshalByRefObjectLeave Site

Resource

VertexBuffer

Remarks

This object inherits additional functionality from the Resource object.

How Do I...?

Read and Write VertexBuffer and IndexBuffer Data With GraphicsStreams

This example demonstrates how to use GraphicsStream objects to fill and retrieve data from a VertexBuffer and IndexBuffer objects.

[C#]
In the following C# code example, a VertexBuffer object is created using a flexible vertex format (FVF) type defined by the PositionNormalTexVertex structure. The vertex buffer data is then locked using its offset and size in bytes. A GraphicsStream object is returned which is derived from StreamLeave Site and can be used in a similar fashon. In the code example, the Write method is used to copy data into the stream from an array of vertex data.
The second part of the method shows use of GraphicsStream using unsafe data access. The InternalDataPointerGraphicsStream.InternalData property returns a void pointer to the vertex buffer data. In this code example, the data is cast to an array of PositionNormalTexVertex structures which makes data manipulation more readable.

using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

public struct PositionNormalTexVertex
{
    public Vector3 Position;
    public Vector3 Normal;
    public  float Tu0, Tv0;
    public static readonly VertexFormats FVF = VertexFormats.Position | VertexFormats.Texture1;
}
public class Example
{
    public unsafe void  GraphicsStreamReadWrite()
    {
        //Create a vertex buffer in the managed pool
        VertexBuffer vb = new VertexBuffer(typeof(PositionNormalTexVertex), 100, device, Usage.None, PositionNormalTexVertex.FVF, Pool.Managed);

        //First, fill an array of PositionNormalTexVertex elements with data.
        PositionNormalTexVertex[] vertices = new PositionNormalTexVertex[50];
        for(int i=0; i<50; i++)
        {
            //fill the vertices with some data...
            vertices[i].Position = new Vector3(3f,4f,5f);
        }

        //The size of the verticies are 32-bytes each (float3 (12) + float3 (12) + float(4) + float(4))
        //To lock 50 verticies, the size of the lock would be 1600 (32 * 50)
        GraphicsStream vbData =  vb.Lock(0,1600, LockFlags.None);

        //copy the vertex data into the vertex buffer
        vbData.Write(vertices);

        //Unlock the VB
        vb.Unlock();


        //This time, lock the entire VertexBuffer
        vbData =  vb.Lock(0, 3200, LockFlags.None);

        //Cast the InternalDataPointer (a void pointer) to an array of verticies
        PositionNormalTexVertex* vbArray = (PositionNormalTexVertex*) vbData.InternalDataPointer;

        for(int i=0; i<100; i++)
        {
            //perform some operations on the data
            vbArray[i].Tu0 = i;
            vbArray[i].Tv0 = vbArray[i].Tu0 * 2;

            Console.WriteLine(vbArray[i].Tv0.ToString());
        }

        //Unlock the buffer
        vb.Unlock();
        vb.Dispose();
    }
}

Read and Write VertexBuffer Data With Arrays

This example demonstrates how to use arrays to fill and retrieve data from a VertexBuffer and IndexBuffer objects.

[C#]
In the following C# code example, a VertexBuffer object is created using a FVF type defined by the PositionNormalTexVertex structure. The first part of the code sample shows the use of Lock to lock an arbitrary number of vertices and fill them with data. The second part shows how to lock an entire VertexBuffer for reading and retrieving data from the array. A similar procedure can be used to write to and read from nearly all Microsoft Direct3D resource types.
Using ArraysLeave Site is not the most efficient method for filling and reading from Direct3D resources; it requires extra memory and cycles to copy the unmanaged resources into managed arrays. However, it makes for very readable code with no unsafe access.

using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

public struct PositionNormalTexVertex
{
    public Vector3 Position;
    public Vector3 Normal;
    public float Tu0, Tv0;
    public static readonly VertexFormats FVF = VertexFormats.Position | VertexFormats.Texture1;
}

public class Example
{
    public void ArrayBasedReadWrite()
    {
        //Create a vertex buffer in the managed pool
        VertexBuffer vb = new VertexBuffer(typeof(PositionNormalTexVertex), 100, device, Usage.None, PositionNormalTex1Vertex.FVF, Pool.Managed);

        //Fill an array of the appropriate type with the VB data using Lock()
        PositionNormalTexVertex[] vbData = (PositionNormalTexVertex[]) vb.Lock(0, typeof(PositionNormalTexVertex), LockFlags.None, 50);
        for(int i=0; i<50; i++)
        {
            //set your vertices to something...
            vbData[i].Position = new Vector3(2f,2f,2f);  
            vbData[i].Normal = new Vector3(1f,0f,0f);
            vbData[i].Tu0 = i;
            vbData[i].Tv0 = i;
        }
        //Unlock the vb before you can use it elsewhere
        vb.Unlock();

        //This lock overload simply locks the entire VB -- setting ReadOnly can improve perf when reading a vertexbuffer
        vbData = (PositionNormalTexVertex[]) vb.Lock(0, LockFlags.ReadOnly);
        for(int i=0; i<100; i++)
        {
            //read some vertex data
            Console.WriteLine("Vertex " + i + "Tu: " +  vbData[i].Tu0 + " , Tv: " + vbData[i].Tv0);
        }

        //Unlock the buffer
        vb.Unlock();


        vb.Dispose();
    }
}

Class Information

Namespace Microsoft.DirectX.Direct3D
Assembly Microsoft.DirectX.Direct3D (microsoft.directx.direct3d.dll)
Strong Name Microsoft.DirectX.Direct3D,  Version=1.0.900.0,  Culture=neutral,  PublicKeyToken=d3231b57b74a1492