IndexBuffer.Lock Method ()

How Do I...?

  • Create a Mesh Object

Locks a range of index data and obtains a pointer to the index buffer memory.

Overload List

public GraphicsStream Lock(int, int, LockFlags);
public Array Lock(int, LockFlags);
public Array Lock(int, Type, LockFlags, int[]);

Remarks

When working with index buffers, multiple lock calls are allowed. However, the number of lock calls must match the number of unlock calls. None of the DrawPrimitives calls will succeed with any outstanding lock count on any currently set index buffer.

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