Microsoft DirectX 9.0

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_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0L );

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 to IDirect3DDevice9::SetStreamSource identifies 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. In the preceding sample code, the size of a CUSTOMVERTEX is used for the size of the component.

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.

For more information about vertex shaders, see Vertex Shaders.

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 Microsoft?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 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_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.

© 2002 Microsoft Corporation. All rights reserved.