Microsoft DirectX 8.1 (Visual Basic)

Step 2: Setting Up the Vertex Buffer

Now that the custom vertex format is defined, it is time to initialize the vertices. The Vertices sample project does this by calling the application-defined function InitVB after creating the required Microsoft® Direct3D® objects. The code following fragment initializes the values for three custom vertices.

With Vertices(0): .x = 150: .y =  50: .z = 0.5: .rhw = 1: .color = &HFFFF0000: End With
With Vertices(1): .x = 250: .y = 250: .z = 0.5: .rhw = 1: .color = &HFF00FF00: End With
With Vertices(2): .x =  50: .y = 250: .z = 0.5: .rhw = 1: .color = &HFF00FFFF: End With

The preceding code fills three vertices with the points of a triangle and specifies which color each vertex will emit. The first point is at (150, 50) and emits the color red (&HFFFF0000). The second point is at (250, 250) and emits the color green (&HFF00FF00). The third point is at (50, 250) and emits the color blue-green (&HFF00FFFF). Each of these points has a pixel depth of 0.5 and an RHW of 1. For more information on this vector format see Transformed and Lit Vertices

The next step is to call Direct3DDevice8.CreateVertexBuffer to create a vertex buffer as shown in the following code fragment.

Set g_VB = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 3, _
                 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)
If g_VB Is Nothing Then Exit Function

The first two parameters for CreateVertexBuffer tell Direct3D the desired size and usage for the new vertex buffer. The next two parameters specify the vector format and memory location for the new buffer. The vector format here is D3DFVF_CUSTOMVERTEX, which is the FVF that the Vertices sample project specified earlier. The D3DPOOL_DEFAULT flag tells Direct3D to create the vertex buffer in the memory that is most appropriate for this buffer. The final parameter is the address of the vertex buffer to create.

After creating a vertex buffer, it is filled with data from the custom vertices by calling the D3DVertexBuffer8SetData method as shown in the following code fragment.

D3DVertexBuffer8SetData g_VB, 0, VertexSizeInBytes * 3, 0, Vertices(0)

The first parameter that D3DVertexBuffer8SetData accepts is the vertex buffer to place the data in. The second parameter is offset, in bytes, from the start of the buffer where data is set. The third parameter is the size of the buffer, in bytes. The fourth flag is a combination of one or more flags that describe how to lock the buffer. The sample sets this parameter to 0, which tells the function to send default flags to the lock. The fifth parameter is the buffer that contains the data to place in the vertex buffer.

Now that the vertex buffer is filled with the vertices, it is time to render the display, as described in Step 3: Rendering the Display.