?/TD> |
Microsoft DirectX 9.0 |
This topic shows the steps necessary to initialize and use a simple vertex shader that uses a position and texture coordinates.
The first step is to declare the structures that hold the position and texture coordinates as shown in the code example below.
struct XYZBuffer { float x, y, z; }; struct TEX0Buffer { float tu, tv; };
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_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
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 = m_pd3dDevice->CreateVertexDeclaration( decl, &m_pVertexDeclaration ); 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 );
After creating the vertex buffer and vertex shader, they are ready to use. The code example below shows how to set the vertex shader, set the stream source, and then draw a triangle list that uses the new vertex shader.
g_d3dDevice->SetVertexShader( m_pVertexShader ); g_d3dDevice->SetStreamSource( 0, xyzbuf, 0, sizeof(XYZBuffer)); g_d3dDevice->SetStreamSource( 1, tex0buf, 0, sizeof(TEX0Buffer)); g_d3dDevice->SetIndices( pIB, 0 ); g_d3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, max - min + 1, 0, count / 3 );