#### 5.6.3 Vertex Buffer Bindings

Vertex buffer bindings are about associating a vertex buffer with a source index used in Vertex Declarations.

#### Creating the Vertex Buffer

Firstly, lets look at how you create a vertex buffer:

    HardwareVertexBufferSharedPtr vbuf =
    	HardwareBufferManager::getSingleton().createVertexBuffer(
    		3*sizeof(Real), // size of one whole vertex
    		numVertices, // number of vertices
    		HardwareBuffer::HBU_STATIC_WRITE_ONLY, // usage
    		false); // no shadow buffer

Notice that we use The Hardware Buffer Manager to create our vertex buffer, and that a class called HardwareVertexBufferSharedPtr is returned from the method, rather than a raw pointer. This is because vertex buffers are reference counted - you are able to use a single vertex buffer as a source for multiple pieces of geometry therefore a standard pointer would not be good enough, because you would not know when all the different users of it had finished with it. The HardwareVertexBufferSharedPtr class manages its own destruction by keeping a reference count of the number of times it is being used - when the last HardwareVertexBufferSharedPtr is destroyed, the buffer itself automatically destroys itself.

The parameters to the creation of a vertex buffer are as follows:

vertexSize

  The size in bytes of a whole vertex in this buffer. A vertex may include multiple elements, and in fact the contents of the vertex data may be reinterpreted by different vertex declarations if you wish. Therefore you must tell the buffer manager how large a whole vertex is, but not the internal format of the vertex, since that is down to the declaration to interpret. In the above example, the size is set to the size of 3 floating point values - this would be enough to hold a standard 3D position or normal, or a 3D texture coordinate, per vertex.

numVertices

  The number of vertices in this buffer. Remember, not all the vertices have to be used at once - it can be beneficial to create large buffers which are shared between many chunks of geometry because changing vertex buffer bindings is a render state switch, and those are best minimised.

usage

  This tells the system how you intend to use the buffer. See Buffer Usage

useShadowBuffer

  Tells the system whether you want this buffer backed by a system-memory copy. See Shadow Buffers

#### Binding the Vertex Buffer

The second part of the process is to bind this buffer which you have created to a source index. To do this, you call:

    vertexBufferBinding->setBinding(0, vbuf);

This results in the vertex buffer you created earlier being bound to source index 0, so any vertex element which is pulling its data from source index 0 will retrieve data from this buffer.
There are also methods for retrieving buffers from the binding data - see the API reference for full details.