?/TD>
Microsoft DirectX 9.0

Create an Effect


This example uses an effect file to apply a texture map to an object. The example shows the contents of the effect file as well as the code required in the application to load and run the file.

To create an effect:

Create the Effect File

// Step 1: Create the effect file.
// This effect file maps a 3-D texture map onto the object.
// This code needs to be in a file named "effects.fx"

texture DiffuseTexture;
 
technique T0
{
    pass P0
    {        
        pixelshader  = NULL;
        fvf          = XYZ | Normal | Diffuse | Tex1;
        vertexshader = NULL;
 
        Lighting     = False;
        CullMode     = None;
 
        texture[0]   = < DiffuseTexture >;
        ColorOp[0]   = SelectArg1;
        ColorArg1[0] = texture;
        ColorOp[1]   = Disable;
    }
}

Load the Effect File

{
    HRESULT hr;
    TCHAR sz[512];
    DXUtil_FindMediaFileCb(sz, sizeof(sz), _T("effect.fx"));
    LPD3DXEFFECT l_pEffect;
 
    if(FAILED(hr = D3DXCreateEffectFromFile(m_pd3dDevice, sz, 
	                NULL, NULL, 0, NULL, &l_pEffect, NULL )))
        return hr;
		
    D3DXHANDLE hTechnique;
    if(FAILED(hr = FindNextValidTechnique(NULL, &hTechnique)))
        return hr;

    m_pEffect->SetTechnique(hTechnique);
    m_pEffect->SetTexture("DiffuseTexture", m_pTexture0);

}

When the effect file is created, ID3DXEffect::FindNextValidTechnique returns a technique that has been validated on the hardware.

Render the Effect

{
    HRESULT hr;
    UINT uPasses;
 
    if(FAILED(hr = m_pd3dDevice->SetStreamSource(0, m_pVB, 0,
                     sizeof(CUSTOMVERTEX_POS_NORM_COLOR1_TEX1))))
        return hr;
 
    m_pEffect->Begin(&uPasses, 0 );
    // The 0 specifies that ID3DXEffect::Begin and ID3DXEffect::End will 
    //   save and restore all state modified by the effect.
 
    for(UINT uPass = 0; uPass < uPasses; uPass++)
    {
        // Set the state for a particular pass in a technique.
        m_pEffect->Pass(uPass);
        m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 
		                             dwNumSphereVerts - 2);
    }
 
    m_pEffect->End();
}


© 2002 Microsoft Corporation. All rights reserved.