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_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0L ); g_pd3dDevice->BeginScene();
Rendering vertex data from a vertex buffer requires 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 IDirect3DDevice9::SetStreamSource.
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
The first parameter is the stream number and the second parameter is a pointer to the vertex buffer. The third parameter is the offset from the beginning of the stream to the beginning of the vertex data, which is 0 in this example. The last parameter is the number of bytes of each element in the vertex declaration.
The next step is to call IDirect3DDevice9::SetFVF to identify the fixed function vertex shader. Full, custom vertex shaders are an advanced topic, but in this case the vertex shader is only the flexible vertex format (FVF) code. The following code fragment sets the FVF code.
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
The only parameter for SetFVF is the fixed vertex function code to define the vertex data layout.
The next step is to use IDirect3DDevice9::DrawPrimitive to render the vertices in the vertex buffer as shown in the following code fragment.
g_pd3dDevice->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 about different kinds of primitives, see 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_pd3dDevice->EndScene(); g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
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.