Microsoft DirectX 8.1 (Visual Basic) |
You create a vertex buffer object by calling the Direct3DDevice8.CreateVertexBuffer method, which accepts five parameters. The first parameter specifies the vertex buffer length, in bytes. Use the Len Microsoft Visual Basic® function to determine the size of a vertex format, in bytes. Consider the following custom vertex format.
Private Type CUSTOMVERTEX x As Single ' x in screen space y As Single ' y in screen space z As Single ' normalized z rhw As Single ' normalized z rhw color As Long ' vertex color tu As Single ' texture coordinate tv As Single ' texture coordinate End Type ' Custom FVF, which describes the custom vertex structure. Const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZRHW Or D3DFVF_DIFFUSE Or D3DFVF_TEX1)
To create a vertex buffer to hold four CUSTOMVERTEX structures, specify [4*sizeof(CUSTOMVERTEX)] for the Length parameter.
The second parameter is a set of usage controls. Among other things, its value determines if the vertex buffer is capable of containing clipping information—in the form of clip flags—for vertices that exist outside the viewing area. To create a vertex buffer that cannot contain clip flags, include the D3DUSAGE_DONOTCLIP flag for the Usage parameter. The D3DUSAGE_DONOTCLIP flag is applied only if you also indicate that the vertex buffer will contain transformed vertices—the D3DFVF_XYZRHW flag is included in the FVF parameter. The CreateVertexBuffer method ignores the D3DUSAGE_DONOTCLIP flag if you indicate that the buffer will contain untransformed vertices (the D3DFVF_XYZ flag). Clipping flags occupy additional memory, making a clipping-capable vertex buffer slightly larger than a vertex buffer incapable of containing clipping flags. Because these resources are allocated when the vertex buffer is created, you must request a clipping-capable vertex buffer ahead of time.
The third parameter, FVF, is a combination of Flexible Vertex Format Flags that describe the vertex format of the vertex buffer. If you specify 0 for this parameter, then the vertex buffer is a non-FVF vertex buffer. For more information, see FVF Vertex Buffers. The fourth parameter describes the memory class into which to place the vertex buffer.
The final parameter that CreateVertexBuffer accepts is the address of a variable to fill with a pointer to the new Direct3DVertexBuffer8 interface of the vertex buffer object, if the call succeeds.
Note You cannot produce clip flags for a vertex buffer that was created without support for them.
The following Visual Basic code example shows what creating a vertex buffer might look like in code.
' ' For the purposes of this example, the m_d3dDevice variable is ' a valid Direct3DDevice8 object. ' ' The custom vertex type Private Type CUSTOMVERTEX x As Single y As Single z As Single rhw As Single color As Long tu As Single tv As Single End Type Const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1) ' Create a clipping-capable vertex buffer. Allocate enough memory ' in the default memory pool to hold three CUSTOMVERTEX structures. VB = m_d3dDevice.CreateVertexBuffer( 3*len(CUSTOMVERTEX), _ 0 /* Usage */, D3DFVF_CUSTOMVERTEX, _ D3DPOOL_DEFAULT)