Microsoft DirectX 8.1 (C++) |
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 IDirect3DDevice8::SetStreamSource.
g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
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. In the sample code above, the size of a CUSTOMVERTEX is used for the size of the component.
The next step is to let Direct3D know what vertex shader to use by calling IDirect3DDevice8::SetVertexShader. Full, custom vertex shaders are an advanced topic, but in most cases the vertex shader is only the FVF code. This lets Direct3D know what types of vertices it is dealing with. The following code fragment sets the vertex shader.
g_pd3dDevice->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 IDirect3DDevice8::CreateVertexShader, or an 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 IDirect3DDevice8::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 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_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.