VertexBuffer.Lock(Int32,Type,LockFlags,Int32[]) Method (Microsoft.DirectX.Direct3D)

How Do I...?

  • Create a Mesh Object

Locks a range of vertex data and obtains the vertex buffer memory.

Definition

Visual Basic Public Function Lock( _
    ByVal offsetToLock As Integer, _
    ByVal typeVertex As TypeLeave Site, _
    ByVal flags As LockFlags, _
    ByVal ranks() As Integer _
) As ArrayLeave Site
C# public ArrayLeave Site Lock(
    int offsetToLock,
    TypeLeave Site typeVertex,
    LockFlags flags,
    int[] ranks
);
C++ public:
ArrayLeave SiteLock(
    int offsetToLock,
    TypeLeave SitetypeVertex,
    LockFlags flags,
    array<int>^ ranks
);
JScript public function Lock(
    offsetToLock : int,
    typeVertex : TypeLeave Site,
    flags : LockFlags,
    ranks : int[]
) : ArrayLeave Site;

Parameters

offsetToLock System.Int32
Offset into the vertex data to lock, in bytes. To lock the entire vertex buffer, specify 0 for both param_Int32_sizeToLock and param_Int32_offsetToLock.
typeVertex System.Type
A TypeLeave Site object that indicates the type of array data to return. This can be a value type or any type that contains only value types.
flags Microsoft.DirectX.Direct3D.LockFlags
Zero or more LockFlags locking flags that describe the type of lock to perform. For this method, the valid flags are Discard, NoDirtyUpdate, NoSystemLock, ReadOnly, and NoOverWrite. For a description of the flags, see LockFlags.
ranks System.Int32[]
Array of one to three Int32Leave Site values that indicate the dimensions of the returning ArrayLeave Site.

Return Value

System.Array
An ArrayLeave Site that represents the locked vertex buffer.

Remarks

When working with vertex buffers, multiple lock calls are allowed; however, the number of lock calls must match the number of unlock calls. Any outstanding lock count on any currently set vertex buffer causes the DrawPrimitives calls to fail.

When using a Lock method to retrieve an ArrayLeave Site from a resource that was not created with a TypeLeave Site, always use the overload that accepts a TypeLeave Site. Use the ranks parameter to indicate the number of members in the returned ArrayLeave Site.

The Lock.Discard and NoOverWrite flags are valid only on buffers created with Dynamic.

Exceptions

InvalidCallException

The method call is invalid. For example, a method's parameter might contain an invalid value.

How Do I...?

Create a Mesh Object

This example shows how to create a Mesh object.

In the following C# code example, device is assumed to be the rendering Device.

              [C#]
              

int numberVerts = 8;
short[] indices = {
    0,1,2, // Front Face 
    1,3,2, // Front Face 
    4,5,6, // Back Face 
    6,5,7, // Back Face 
    0,5,4, // Top Face 
    0,2,5, // Top Face 
    1,6,7, // Bottom Face 
    1,7,3, // Bottom Face 
    0,6,1, // Left Face 
    4,6,0, // Left Face 
    2,3,7, // Right Face 
    5,2,7 // Right Face 
};

Mesh mesh = new Mesh(numberVerts * 3, numberVerts, MeshFlags.Managed,
                     CustomVertex.PositionColored.Format, device);

using(VertexBuffer vb = mesh.VertexBuffer)
{
    GraphicsStream data = vb.Lock(0, 0, LockFlags.None);

    data.Write(new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, 0x00ff00ff));
    data.Write(new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, 0x00ff00ff));
    data.Write(new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, 0x00ff00ff));
    data.Write(new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, 0x00ff00ff));
    data.Write(new CustomVertex.PositionColored(-1.0f, 1.0f, -1.0f, 0x00ff00ff));
    data.Write(new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, 0x00ff00ff));
    data.Write(new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, 0x00ff00ff));
    data.Write(new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, 0x00ff00ff));

    vb.Unlock();
}

using (IndexBuffer ib = mesh.IndexBuffer)
{
    ib.SetData(indices, 0, LockFlags.None);
}

See Also