| ?/TD> |
| Microsoft DirectX 9.0 |
This topic shows the steps necessary to initialize and use a simple vertex shader that uses a position and a diffuse color.
The first step is to declare the structures that hold the position and color as shown in the code example below.
struct XYZBuffer
{
FLOAT x, y, z;
};
struct ColBuffer
{
D3DCOLOR color;
};
The next step is to create a vertex shader declaration as shown in the code below.
D3DVERTEXELEMENT9 decl[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 1, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
D3DDECL_END()
};
Now create the vertex declaration object:
LPDIRECT3DVERTEXDECLARATION9 m_pVertexDeclaration; g_d3dDevice->CreateVertexDeclaration( decl, &m_pVertexDeclaration );
The next step is to call the IDirect3DDevice9::CreateVertexShader method to create the vertex shader. But first, the shader must be assembled.
TCHAR strVertexShaderPath[512];
LPD3DXBUFFER pCode;
LPDIRECT3DVERTEXSHADER9 m_pVertexShader;
hr = DXUtil_FindMediaFileCb( strVertexShaderPath,
sizeof(strVertexShaderPath), _T("ShaderFile.vsh");
hr = D3DXAssembleShaderFromFile( strVertexShaderPath, NULL, NULL,
dwFlags, &pCode, NULL );
g_d3dDevice->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(),
&m_pVertexShader );
The code example below shows how to set the vertex shader, set the stream source, and then render the triangle list.
g_pd3dDevice->SetVertexDeclaration( m_pVertexDeclaration );
g_d3dDevice->SetVertexShader( m_pVertexShader );
g_d3dDevice->SetStreamSource( 0, xyzbuf, sizeof(XYZBuffer));
g_d3dDevice->SetStreamSource( 1, colbuf, sizeof(ColBuffer));
g_d3dDevice->SetIndices( pIB, 0 );
g_d3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0, max - min + 1, 0, count / 3 );