来源:http://www.cnblogs.com/fence/archive/2010/03/12/1683918.html
LPDIRECT3DINDEXBUFFER9 g_pIB = NULL; // Buffer to hold indeces
HRESULT hr;
V_RETURN( g_DialogResourceManager.OnCreateDevice( pd3dDevice ) );
V_RETURN( g_SettingsDlg.OnCreateDevice( pd3dDevice ) );
// Initialize the font
V_RETURN( D3DXCreateFont( pd3dDevice, 15 , 0 , FW_BOLD, 1 , FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
L " Arial " , & g_pFont ) );
// Initialize vertices for rendering a triangle
CUSTOMVERTEX vertices[] =
{
{ 0.0f , 0.0f , 0.5f , 1.0f , 0xffff0000 , }, // x, y, z, rhw, color
{ ( float )pBackBufferSurfaceDesc -> Width, 0.0f , 0.5f , 1.0f , 0xff00ff00 , },
{ ( float )pBackBufferSurfaceDesc -> Width, ( float )pBackBufferSurfaceDesc -> Height, 0.5f , 1.0f , 0xffff00ff , },
{ 0.0f , ( float )pBackBufferSurfaceDesc -> Height, 0.5f , 1.0f , 0xff0000ff , },
};
// Create the vertex buffer
V_RETURN( pd3dDevice -> CreateVertexBuffer( sizeof (vertices),
0 , D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, & g_pVB, NULL ) );
VOID * pVertices;
V_RETURN( g_pVB -> Lock( 0 , sizeof (vertices), ( void ** ) & pVertices, 0 ) );
memcpy( pVertices, vertices, sizeof (vertices) );
g_pVB -> Unlock();
// Initialize index buffer for rendering
WORDwIndeces[] = { 0 , 1 , 2 , 0 , 2 , 3 };
// Create the index buffer
V_RETURN( pd3dDevice -> CreateIndexBuffer( sizeof (wIndeces),
0 , D3DFMT_INDEX16,
D3DPOOL_MANAGED, & g_pIB, NULL) );
VOID * pIndeces;
V_RETURN( g_pIB -> Lock( 0 , sizeof (wIndeces), & pIndeces, 0 ) );
memcpy( pIndeces, wIndeces, sizeof (wIndeces) );
g_pIB -> Unlock();
returnS_OK;
OnRender中BeginScene和EndScene之间
pd3dDevice -> SetStreamSource( 0 , g_pVB, 0 , sizeof (CUSTOMVERTEX) );
pd3dDevice -> SetFVF( D3DFVF_CUSTOMVERTEX );
pd3dDevice -> SetIndices( g_pIB );
// pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );
pd3dDevice -> DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0 , 0 , 4 , 0 , 2 );
OnDestroyDevice
SAFE_RELEASE( g_pVB );
SAFE_RELEASE( g_pIB );
DrawPrimitive( D3DPT_TRIANGLELIST, // PrimitiveType
0 , // StartVertex
2 ); // PrimitiveCount
DrawIndexPrimitive( D3DPT_TRIANGLELIST, // PrimitiveType
0 , // BaseVertexIndex
0 , // MinIndex
4 , // NumVertices
0 , // StartIndex
2 ); // PrimitiveCount
DrawIndexPrimitive( D3DPT_TRIANGLELIST, // PrimitiveType
0 , // BaseVertexIndex
0 , // MinIndex
4 , // NumVertices
3 , // StartIndex
1 ); // PrimitiveCount
DrawIndexPrimitive( D3DPT_TRIANGLELIST, // PrimitiveType
50 , // BaseVertexIndex
0 , // MinIndex
4 , // NumVertices
3 , // StartIndex
1 ); // PrimitiveCount