Microsoft DirectX 8.1 (Visual Basic)

Step 3: Rendering the Display

Now that the vertex buffer is filled with vertices, it is time to render the display. Rendering the display starts by clearing the back buffer to a blue color and then calling BeginScene.

g_D3DDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET, &HFF&, 1#, 0
g_D3DDevice.BeginScene

Rendering vertex data from a vertex buffer is divided into a few steps. First, you need to set the stream source; in this case, use stream 0. The source of the stream is specified by calling Direct3DDevice8.SetStreamSource.

g_D3DDevice.SetStreamSource 0, g_VB, sizeOfVertex

The first parameter of SetStreamSource tells Microsoft® Direct3D® the source of the device data stream. The second parameter is the vertex buffer to bind to the data stream. The third parameter is the size of the component, in bytes.

The next step is to let Direct3D know what vertex shader to use by calling Direct3DDevice8.SetVertexShader. Full, custom vertex shaders are an advanced topic, but in most cases the vertex shader is just the FVF. This lets Direct3D know what types of vertices it is dealing with. The following code fragment sets the vertex shader.

g_D3DDevice.SetVertexShader D3DFVF_CUSTOMVERTEX

The only parameter for SetVertexShader is a handle to the vertex shader to use. The value for this parameter can be a handle returned by Direct3DDevice8.CreateVertexShader, or a FVF code. Here, the FVF code defined by D3DFVF_CUSTOMVERTEX is used.

For more information on vertex shaders, see Vertex Shaders.

The next step is to use Direct3DDevice8.DrawPrimitive to render the vertices in the vertex buffer as shown in the following code fragment.

g_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1

The first parameter accepted by DrawPrimitive is a flag that tells Direct3D what type of primitives to draw. This sample uses the flag D3DPT_TRIANGLELIST to specify a list of triangles. The second parameter is the index of the first vertex to load. The third parameter tells the number of primitives to draw. Because this sample draws only one triangle, this value is set to 1.

For more information on different kinds of primitives, see 3-D Primitives.

The last steps are to end the scene and then present the back buffer to the front buffer. This is shown in the following code fragment.

g_D3DDevice.EndScene
g_D3DDevice.Present ByVal 0, ByVal 0, 0, ByVal 0

After the back buffer is presented to the front buffer, the client window shows a triangle with three different colored points.

This tutorial has shown you how to use vertices to render geometric shapes. Tutorial 3: Using Matrices introduces the concept of matrices and how to use them.