D3d9 Effect Research and learning

Source: Internet
Author: User
Tags mul

Part one of the procedure

1, the definition of several global variables:

id3dxeffect*                g_peffect = null;d3dxmatrixa16               g_mcenterworld;d3dxhandle                  g_hworld = Null;d3dxhandle                  g _hworldviewprojection = null;d3dxhandle                  g_htechniquerenderscene = NULL;

2, equipment inspection

BOOL Isdeviceacceptable (d3dcaps9* pcaps,d3dformat adapterformat,d3dformat backbufferformat,bool bWindowed,void* Pusercontext) {    if (Pcaps->pixelshaderversion < d3dps_version (2, 0))  //check pixel shader        return false;
<pre name= "code" class= "CPP" >//<span style= "font-family:arial; font-size:14px; line-height:26px; " > Render a </span><span style= "FONT-SIZE:14PX; line-height:26px; Font-family:calibri; " >surface</span><span style= "font-family:arial; font-size:14px; line-height:26px; " >, it is usually necessary to check this </span><span style= "FONT-SIZE:14PX; line-height:26px; Font-family:calibri; " >surface</span><span style= "font-family:arial; font-size:14px; line-height:26px; " > </span><span style= "font-family:arial; font-size:14px; line-height:26px; " > Format </span><span style= "font-family:arial; font-size:14px; line-height:26px; " Whether > can be used for </span><span style= "FONT-SIZE:14PX; line-height:26px; Font-family:calibri; " >texture</span><span style= "font-family:arial; font-size:14px; line-height:26px; " >, </span><span style= "FONT-SIZE:14PX; line-height:26px; Font-family:calibri; " >rendertarget</span><Span style= "font-family:arial; font-size:14px; line-height:26px; " >, </span><span style= "FONT-SIZE:14PX; line-height:26px; Font-family:calibri; " >depth-stenil buffer</span>
idirect3d9* pd3d = Dxutgetd3d9object (); if (FAILED (Pd3d->checkdeviceformat (pcaps->adapterordinal, Pcaps->devicetype, Adapterformat, D3DUSAGE_ Query_postpixelshader_blending, D3drtype_texture, Backbufferformat)) return false; return true;}


3. Effect file Creation loading

hresult d3dxcreateeffectfromfile (_In_    Lpdirect3ddevice9 pdevice,//d3d device _in_   lpctstr psrcfile,//file name or path to be loaded _in_  & nbsp const  D3dxmacro *pdefines, _in_   lpd3dxinclude pinclude, _in_   dword Flags, _In_    lpd3dxeffectpool Ppool, _out_  lpd3dxeffect *ppeffect, _out_  lpd3dxbuffer * ppcompilationerrors); 
typedef struct D3dxmacro {  LPCSTR Name;  LPCSTR Definition;} D3dxmacro, *lpd3dxmacro;

HRESULT Oncreateloadfile (idirect3ddevice9* pd3ddevice, const d3dsurface_desc* pbackbuffersurfacedesc,void* Pusercontext) {    dxutfinddxsdkmediafilecch (str, MAX_PATH, TEXT ("Compiledeffect.fxo"));  Find file path    V_return (D3dxcreateeffectfromfile (pd3ddevice, str, NULL, NULL, d3dxfx_not_cloneable, NULL, &g_ Peffect, NULL));    return S_OK;}

4. Get the variable handle in the effect file

HRESULT Geteffectparameterhandle (idirect3ddevice9* pd3ddevice,                                const d3dsurface_desc* PBACKBUFFERSURFACEDESC, void* pusercontext) {    d3dxcolor colormtrldiffuse (1.0f, 1.0f, 1.0f, 1.0f);    D3dxcolor colormtrlambient (0.35f, 0.35f, 0.35f, 0);
    G_peffect->setvector ("G_materialambientcolor", (d3dxvector4*) &colormtrlambient);  Set global variable    g_peffect->setvector ("G_materialdiffusecolor", (d3dxvector4*) &colormtrldiffuse);    G_htechniquerenderscene = G_peffect->gettechniquebyname ("Renderscene");              Get effect    g_htime = G_peffect->getparameterbyname (NULL, "g_ftime");    G_hworld = G_peffect->getparameterbyname (NULL, "G_mworld");    G_hworldviewprojection = G_peffect->getparameterbyname (NULL, "g_mworldviewprojection");    G_hmeshtexture = G_peffect->getparameterbyname (NULL, "g_meshtexture");    return S_OK;}


5. Set the data

void Seteffectparater (double ftime, float felapsedtime, void* pusercontext) {    d3dxmatrixa16 mworld;    D3dxmatrixa16 MView;    D3dxmatrixa16 mProj;    D3dxmatrixa16 mworldviewprojection;    Mworld = G_mcenterworld * *g_camera.getworldmatrix ();    MProj = *g_camera.getprojmatrix ();    MView = *g_camera.getviewmatrix ();    mworldviewprojection = Mworld * MView * mProj;    G_peffect->setmatrix (G_hworldviewprojection, &mworldviewprojection);    G_peffect->setmatrix (G_hworld, &mworld);    G_peffect->setfloat (G_htime, (float) ftime);}

6. Rendering

void Rendereffect (idirect3ddevice9* pd3ddevice, double ftime, float felapsedtime, void* pusercontext) {    Pd3ddevice->clear (0, NULL, D3dclear_target | D3dclear_zbuffer, D3dcolor_argb (0, 1.0f, 0);    Pd3ddevice->beginscene ()
        UINT IPass, cpasses;                           Effect Rendering        g_peffect->begin (&cpasses, 0);        for (iPass = 0; IPass < cpasses; ipass++)        {            g_peffect->beginpass (iPass);
                          Other transaction rendering
            G_peffect->endpass ();
        }        g_peffect->end ();
     Pd3ddevice->endscene ();}

7. Release related Resources

void Onlostdevice () {    if (g_peffect)        g_peffect->onlostdevice ();} void Ondestroydevice () {    safe_release (g_peffect);}

Second, the FX file part

Global variables
FLOAT4 g_materialambientcolor;float4 G_materialdiffusecolor;float3 g_lightdir = Normalize (FLOAT3 (1.0f, 1.0f, -1.0f)); Float4 g_lightambient = {0.2f, 0.2f, 0.2f, 0.2f};float4 g_lightdiffuse = {1.0f, 1.0f, 1.0f, 1.0f};texture g_rendertarg ettexture;//Full screen render target texture float g_ftime;float4x4 g_mworld;float4x4 g_mworldviewprojection;sampler RenderTargetSampler =    sampler_state{Texture = <g_RenderTargetTexture>;      Minfilter = point;    Magfilter = point;    Mipfilter = NONE;    Addressu = Clamp; ADDRESSV = Clamp;};    struct vs_output{float4 position:position;     FLOAT4 diffuse:color0; Float2 textureuv:texcoord0;};                         Vs_output Renderscenevs (float4 vpos:position, Float3 Vnormal:normal,    Float2 vtexcoord0:texcoord0) {vs_output OUTPUT;      FLOAT3 Vnormalworldspace;    FLOAT4 vanimatedpos = vPOS;    Vanimatedpos.x *= (1.0 + cos (g_ftime)/10);    Vanimatedpos.y *= (1.0 + sin (g_ftime)/10); Vanimatedpos.z *= (1.0 + COS (g_ftime)/10);    Output.position = Mul (Vanimatedpos, g_mworldviewprojection);    Vnormalworldspace = Normalize (Mul (Vnormal, (float3x3) g_mworld));                          Output.Diffuse.rgb = G_materialdiffusecolor * g_lightdiffuse * MAX (0,dot (Vnormalworldspace, g_lightdir)) +       G_materialambientcolor * g_lightambient;     Output.diffuse.a = 1.0f;     OUTPUT.TEXTUREUV = vTexCoord0;    return Output; }struct ps_output{float4 rgbcolor:color0;};    Ps_output rendersceneps (Vs_output in) {Ps_output OUTPUT;    Output.rgbcolor = tex2d (Meshtexturesampler, IN.TEXTUREUV) * in.diffuse; return Output;}                                          Technique renderscene{Pass P0 {zenable = true;        Z-buffer vertexshader = Compile vs_2_0 renderscenevs ();    PixelShader = Compile ps_2_0 rendersceneps (); }}

D3d9 Effect Research and learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.