D3D9 effect研究及学习

一、程序部分

1、几个全局变量的定义:

ID3DXEffect*g_pEffect = NULL;D3DXMATRIXA16g_mCenterWorld;D3DXHANDLEg_hWorld = NULL;D3DXHANDLEg_hWorldViewProjection = NULL;D3DXHANDLEg_hTechniqueRenderScene = NULL;2、设备检查bool IsDeviceAcceptable(D3DCAPS9* pCaps,D3DFORMAT AdapterFormat,D3DFORMAT BackBufferFormat,bool bWindowed,void* pUserContext){if( pCaps->PixelShaderVersion < D3DPS_VERSION( 2, 0 ) ) //检查像素着色器return false;//通常要检查相关的纹理,深度,模版是否被支持IDirect3D9* pD3D = DXUTGetD3D9Object(); if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, BackBufferFormat ) ) ) return false; return true;}3、效果文件创建载入HRESULT D3DXCreateEffectFromFile( _In_LPDIRECT3DDEVICE9 pDevice, //D3D设备 _In_LPCTSTR pSrcFile,//待载入的文件名或路径 _In_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" ) ); //查找文件路径V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, D3DXFX_NOT_CLONEABLE, NULL, &g_pEffect, NULL ) );return S_OK;}

4、获得效果文件中的变量句柄

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 ); //设置全局变量g_pEffect->SetVector( "g_MaterialDiffuseColor", ( D3DXVECTOR4* )&colorMtrlDiffuse );g_hTechniqueRenderScene = g_pEffect->GetTechniqueByName( "RenderScene" );//获得效果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、设置数据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、渲染void RenderEffect(IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext){pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 70 ), 1.0f, 0 );pd3dDevice->BeginScene()UINT iPass, cPasses;//效果渲染g_pEffect->Begin( &cPasses, 0);for( iPass = 0; iPass < cPasses; iPass++ ){g_pEffect->BeginPass( iPass );//其他事务渲染g_pEffect->EndPass();}g_pEffect->End();pd3dDevice->EndScene();}7、释放相关资源void OnLostDevice(){if( g_pEffect )g_pEffect->OnLostDevice();}void OnDestroyDevice(){SAFE_RELEASE( g_pEffect );}二、fx文件部分//全局变量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_RenderTargetTexture;//全屏渲染目标纹理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-bufferVertexShader = compile vs_2_0 RenderSceneVS();PixelShader = compile ps_2_0 RenderScenePS();}}

,人生难免有挫折,但你是逃避不了的,一定要去面对它

D3D9 effect研究及学习

相关文章:

你感兴趣的文章:

标签云: