Create an Effect

DirectX8

Microsoft DirectX 8.1 (C++)

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:

Step 1: Create the Effect File
Step 2: Load the Effect File
Step 3: Render the Effect

Step 1: 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
    {        
        Texture[0]   = NULL;
        PixelShader  = NULL;
        VertexShader = XYZ | Normal | Diffuse | Tex1;
 
        Lighting     = False;
        CullMode     = None;
 
        Texture[0]   = <DiffuseTexture>;
        ColorOp[0]   = SelectArg1;
        ColorArg1[0] = Texture;
        ColorOp[1]   = Disable;
    }
}

Step 2: Load the Effect File

{
    HRESULT hr;
    D3DXTECHNIQUE_DESC technique;
    ID3DXEffect m_pEffect;
	
    // Assumes that m_pd3dDevice has been initialized
    if(FAILED(hr = D3DXCreateEffectFromFile(m_pd3dDevice, "effect.fx", &m;_pEffect, NULL)))
        return hr;
 
    if(FAILED(hr = FindNextValidTechnique(NULL, &technique;)))
        return hr;
 
    m_pEffect->SetTechnique(technique.Index);
    m_pEffect->SetTexture("DiffuseTexture", m_pTexture0);
}

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

Step 3: Render the Effect

{
    HRESULT hr;
    UINT uPasses;
 
    if(FAILED(hr = m_pd3dDevice->SetStreamSource(0, m_pVB,
	                     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();
 
}