?/TD>
Microsoft DirectX 9.0

Multi-Texture Shader


This topic shows the steps necessary to initialize and use a simple vertex shader that uses a position and multiple texture coordinates for multiple textures.

The first step is to declare the structures that holds the position and color as shown in the code example below.

 
struct XYZBuffer
{
    float x, y, z;
};

struct Tex0Buffer
{
    float tu, tv;
};

struct Tex1Buffer
{
    float tu2, tv2;
};

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 },
    { 2, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
    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 );

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_pd3dDevice->SetVertexDeclaration( m_pVertexDeclaration );
g_d3dDevice->SetVertexShader( m_pVertexShader );
g_d3dDevice->SetStreamSource( 0, xyzbuf, 0, sizeof(XYZBuffer) );
g_d3dDevice->SetStreamSource( 1, tex0buf, 0, sizeof(Tex0Buffer) );
g_d3dDevice->SetStreamSource( 2, tex1buf, 0, sizeof(Tex1Buffer) );
g_d3dDevice->SetIndices( pIB, 0 );
g_d3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, max - min + 1, 0, count / 3 );


© 2002 Microsoft Corporation. All rights reserved.