标签:direct3d directx 游戏编程 游戏开发 effect
一、程序部分
1、几个全局变量的定义:
ID3DXEffect* g_pEffect = NULL; D3DXMATRIXA16 g_mCenterWorld; D3DXHANDLE g_hWorld = NULL; D3DXHANDLE g_hWorldViewProjection = NULL; D3DXHANDLE g_hTechniqueRenderScene = NULL;
bool IsDeviceAcceptable(D3DCAPS9* pCaps,D3DFORMAT AdapterFormat,D3DFORMAT BackBufferFormat,bool bWindowed,void* pUserContext) { if( pCaps->PixelShaderVersion < D3DPS_VERSION( 2, 0 ) ) //检查像素着色器 return false;
<pre name="code" class="cpp"> //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">渲染一个</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-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;">格式</span><span style="font-family: Arial; font-size: 14px; line-height: 26px;">是否可以用于</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;}
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; }
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 ); }
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(); }
void OnLostDevice() { if( g_pEffect ) g_pEffect->OnLostDevice(); } void OnDestroyDevice() { SAFE_RELEASE( g_pEffect ); }
//全局变量
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-buffer VertexShader = compile vs_2_0 RenderSceneVS(); PixelShader = compile ps_2_0 RenderScenePS(); } }
标签:direct3d directx 游戏编程 游戏开发 effect
原文地址:http://blog.csdn.net/a809146548/article/details/43915981