fragment就是hlsl積木,方便組合、去除、添加。
FragmentLinker.fx
//==============================================================
// Desc: Effect文件
//==============================================================
//--------------------------------------------------------------
// 全局變量
//--------------------------------------------------------------
float4 g_vMaterialAmbient = float4( 0.3f, 0.3f, 0.3f, 1.0f );
float4 g_vMaterialDiffuse = float4( 0.6f, 0.6f, 0.6f, 1.0f );
float4 g_vLightColor = float4( 1.0f, 1.0f, 1.0f, 1.0f );
float3 g_vLightPosition = float3( 0.0f, 5.0f, -5.0f );
texture g_MeshTexture;
float g_fTime;
float4x4 g_mWorld;
float4x4 g_mWorldViewProjection;
//--------------------------------------------------------------
// 紋理采樣器
//--------------------------------------------------------------
sampler MeshTextureSampler:s0;
//---------------------------------------------------------------
// 頂點(diǎn)渲染器段落, Projection transform
//---------------------------------------------------------------
void Projection( float4 vPosObject: POSITION,
float3 vNormalObject: NORMAL,
float2 vTexCoordIn: TEXCOORD0,
out float4 vPosWorld: r_PosWorld,
out float3 vNormalWorld: r_NormalWorld,
out float4 vPosProj: POSITION,
out float2 vTexCoordOut: TEXCOORD0,
uniform bool bAnimate )
{
//動(dòng)畫頂點(diǎn)
if( bAnimate )
vPosObject.x *= (1 + sin( g_fTime )/2);
//頂點(diǎn)坐標(biāo)變換
vPosProj = mul( vPosObject, g_mWorldViewProjection );
//將頂點(diǎn)坐標(biāo)和法線變換到世界空間, 用于光照
vPosWorld = mul( vPosObject, g_mWorld );
vNormalWorld = mul( vNormalObject, (float3x3)g_mWorld );
//輸出紋理坐標(biāo)
vTexCoordOut = vTexCoordIn;
}
//頂點(diǎn)渲染器段落聲明
vertexfragment ProjectionFragment_Animated = compile_fragment vs_2_0 Projection( true );
vertexfragment ProjectionFragment_Static = compile_fragment vs_2_0 Projection( false );
//---------------------------------------------------------------
// 頂點(diǎn)渲染器段落, Ambient scene lighting
//---------------------------------------------------------------
void Ambient( out float4 vColor: COLOR0 )
{
//計(jì)算環(huán)境光照顏色
vColor = g_vLightColor * g_vMaterialAmbient;
}
//像素渲染器段落
vertexfragment AmbientFragment = compile_fragment vs_2_0 Ambient();
//---------------------------------------------------------------
// 頂點(diǎn)渲染器段落, Diffuse scene lighting
//---------------------------------------------------------------
void AmbientDiffuse( float3 vPosWorld: r_PosWorld,
float3 vNormalWorld: r_NormalWorld,
out float4 vColor: COLOR0 )
{
//計(jì)算燈光方向
float3 vLight = normalize( g_vLightPosition - vPosWorld );
//計(jì)算環(huán)境光照和漫反射光照
vColor = g_vLightColor * g_vMaterialAmbient;
vColor += g_vLightColor * g_vMaterialDiffuse * saturate( dot( vLight, vNormalWorld ) );
}
//頂點(diǎn)渲染器段落聲明
vertexfragment AmbientDiffuseFragment = compile_fragment vs_2_0 AmbientDiffuse();
//--------------------------------------------------------------
// 像素渲染器
//--------------------------------------------------------------
void ModulateTexture( float4 vColorIn: COLOR0,
float2 vTexCoord: TEXCOORD0,
out float4 vColorOut: COLOR0 )
{
//混合頂點(diǎn)光照顏色和紋理顏色
vColorOut = vColorIn * tex2D( MeshTextureSampler, vTexCoord );
}
//=============================================================================
// Desc: 主程序源文件
//=============================================================================
#include "dxstdafx.h"
#include "resource.h"
//#define DEBUG_VS // Uncomment this line to debug vertex shaders
//#define DEBUG_PS // Uncomment this line to debug pixel shaders
//-----------------------------------------------------------------------------
// 全局變量
//-----------------------------------------------------------------------------
ID3DXFont* g_pFont = NULL; //ID3DXFont字體對(duì)象
ID3DXSprite* g_pTextSprite = NULL; //ID3DXSprite文本精靈對(duì)象
bool g_bShowHelp = true; //標(biāo)識(shí)是否顯示簡(jiǎn)單說明文本
CDXUTDialogResourceManager g_DialogResourceManager; //對(duì)話框資源管理器
CD3DSettingsDlg g_SettingsDlg; //Direct3D設(shè)備設(shè)置對(duì)話框
CDXUTDialog g_HUD; //對(duì)話框
CDXUTDialog g_SampleUI; //對(duì)話框
CDXUTMesh g_Mesh; //網(wǎng)格模型
CModelViewerCamera g_Camera; //攝影機(jī)
D3DXVECTOR3 g_vObjectCenter; //網(wǎng)格模型外包圍球中心
FLOAT g_fObjectRadius; //網(wǎng)格模型外包圍球半徑
D3DXMATRIXA16 g_mCenterWorld; //網(wǎng)格模型世界矩陣
ID3DXFragmentLinker* g_pFragmentLinker = NULL; //段落鏈接器
LPD3DXBUFFER g_pCompiledFragments = NULL; //保存編譯過的段落的緩沖區(qū)
IDirect3DPixelShader9* g_pPixelShader = NULL; //像素渲染器
IDirect3DVertexShader9* g_pLinkedShader = NULL; //通過段落鏈接器鏈接生成的頂點(diǎn)渲染器
LPD3DXCONSTANTTABLE g_pConstTable = NULL; //頂點(diǎn)渲染器常量表
//頂點(diǎn)渲染器使用的全局變量
D3DXVECTOR4 g_vMaterialAmbient( 0.3f, 0.3f, 0.3f, 1.0f );
D3DXVECTOR4 g_vMaterialDiffuse( 0.6f, 0.6f, 0.6f, 1.0f );
D3DXVECTOR4 g_vLightColor ( 1.0f, 1.0f, 1.0f, 1.0f );
D3DXVECTOR4 g_vLightPosition ( 0.0f, 5.0f, -5.0f, 1.0f );
//-----------------------------------------------------------------------------
// 控件ID
//-----------------------------------------------------------------------------
#define IDC_STATIC -1
#define IDC_TOGGLEFULLSCREEN 1
#define IDC_TOGGLEREF 3
#define IDC_CHANGEDEVICE 4
#define IDC_ANIMATION 5
#define IDC_LIGHTING 6
//-----------------------------------------------------------------------------
// Desc: 函數(shù)聲明
//-----------------------------------------------------------------------------
bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext );
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext );
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext );
void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext );
void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext );
void CALLBACK OnLostDevice( void* pUserContext );
void CALLBACK OnDestroyDevice( void* pUserContext );
void InitApp();
HRESULT LoadMesh( IDirect3DDevice9* pd3dDevice, WCHAR* strFileName, ID3DXMesh** ppMesh );
void RenderText();
HRESULT LinkVertexShader();
//-----------------------------------------------------------------------------
// Desc: 入口函數(shù)
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
{
//為Debug配置啟用運(yùn)行時(shí)內(nèi)存檢查功能
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
//設(shè)置回調(diào)函數(shù)
DXUTSetCallbackDeviceCreated( OnCreateDevice );
DXUTSetCallbackDeviceReset( OnResetDevice );
DXUTSetCallbackDeviceLost( OnLostDevice );
DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
DXUTSetCallbackMsgProc( MsgProc );
DXUTSetCallbackKeyboard( KeyboardProc );
DXUTSetCallbackFrameRender( OnFrameRender );
DXUTSetCallbackFrameMove( OnFrameMove );
//全屏?xí)r顯示鼠標(biāo)光標(biāo)
DXUTSetCursorSettings( true, true );
//應(yīng)用程序相關(guān)的初始化
InitApp();
//初始化DXUT, 創(chuàng)建窗口, 創(chuàng)建Direct3D設(shè)備對(duì)象
DXUTCreateWindow( L"FragmentLinker" );
DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings );
//進(jìn)入消息循環(huán)和場(chǎng)景渲染
DXUTMainLoop();
//在此進(jìn)行應(yīng)用程序相關(guān)的清除工作
return DXUTGetExitCode();
}
//-----------------------------------------------------------------------------
// Desc: 應(yīng)用程序相關(guān)初始化
//-----------------------------------------------------------------------------
void InitApp()
{
//初始化對(duì)話框
g_SettingsDlg.Init( &g_DialogResourceManager );
g_HUD.Init( &g_DialogResourceManager );
g_SampleUI.Init( &g_DialogResourceManager );
//為g_HUD對(duì)話框設(shè)置消息處理函數(shù),添加控件
g_HUD.SetCallback( OnGUIEvent ); int iY = 10;
g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 35, iY, 125, 22 );
g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 35, iY += 24, 125, 22 );
g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 35, iY += 24, 125, 22, VK_F2 );
//為g_SampleUI對(duì)話框設(shè)置消息處理函數(shù),字體,添加控件
g_SampleUI.SetCallback( OnGUIEvent ); iY = 10;
g_SampleUI.SetFont( 1, L"Arial", 14, FW_BOLD );
CDXUTElement* pElement = g_SampleUI.GetDefaultElement( DXUT_CONTROL_STATIC, 0 );
if( pElement )
{
pElement->iFont = 1;
pElement->dwTextFormat = DT_LEFT | DT_BOTTOM;
}
CDXUTComboBox* pComboBox = NULL;
g_SampleUI.AddStatic( IDC_STATIC, L"(V)ertex Animation", 20, 0, 105, 25 );
g_SampleUI.AddComboBox( IDC_ANIMATION, 20, 25, 140, 24, 'V', false, &pComboBox );
if( pComboBox )
pComboBox->SetDropHeight( 30 );
g_SampleUI.AddStatic( IDC_STATIC, L"(L)ighting", 20, 50, 105, 25 );
g_SampleUI.AddComboBox( IDC_LIGHTING, 20, 75, 140, 24, 'L', false, &pComboBox );
if( pComboBox )
pComboBox->SetDropHeight( 30 );
}
//-----------------------------------------------------------------------------
// Desc: 設(shè)備能力檢查
//-----------------------------------------------------------------------------
bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,
D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
{
//檢查后臺(tái)緩沖區(qū)格式是否支持Alpha混合等操作(post pixel blending operations)
IDirect3D9* pD3D = DXUTGetD3DObject();
if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
return false;
//檢查設(shè)備支持的像素渲染器版本
if( pCaps->PixelShaderVersion < D3DPS_VERSION(1,1) )
return false;
return true;
}
//-----------------------------------------------------------------------------
// Desc: 修改Direct3D渲染設(shè)備設(shè)置
//-----------------------------------------------------------------------------
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
{
//如果硬件不支持, 改為軟件頂點(diǎn)處理
if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) )
{
pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
//調(diào)試頂點(diǎn)渲染器需要參考設(shè)備或軟件頂點(diǎn)處理
#ifdef DEBUG_VS
if( pDeviceSettings->DeviceType != D3DDEVTYPE_REF )
{
pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;
pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
#endif
//調(diào)試像素渲染器需要參考設(shè)備
#ifdef DEBUG_PS
pDeviceSettings->DeviceType = D3DDEVTYPE_REF;
#endif
//如果使用參考設(shè)備,則彈出警告對(duì)話框
static bool s_bFirstTime = true;
if( s_bFirstTime )
{
s_bFirstTime = false;
if( pDeviceSettings->DeviceType == D3DDEVTYPE_REF )
DXUTDisplaySwitchingToREFWarning();
}
return true;
}
//-----------------------------------------------------------------------------
// Desc: 在此創(chuàng)建管理內(nèi)存資源對(duì)象
//-----------------------------------------------------------------------------
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
HRESULT hr;
V_RETURN( g_DialogResourceManager.OnCreateDevice( pd3dDevice ) );
V_RETURN( g_SettingsDlg.OnCreateDevice( pd3dDevice ) );
//創(chuàng)建字體
V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
L"Arial", &g_pFont ) );
WCHAR strPath[MAX_PATH];
//創(chuàng)建網(wǎng)格模型
V_RETURN( DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, L"dwarf\\dwarf.x" ) );
V_RETURN( g_Mesh.Create( pd3dDevice, strPath ) );
//計(jì)算網(wǎng)格模型外包圍球中心和半徑
IDirect3DVertexBuffer9* pVB = NULL;
V_RETURN( g_Mesh.m_pMesh->GetVertexBuffer( &pVB ) );
void* pVertices = NULL;
hr = pVB->Lock( 0, 0, &pVertices, 0 );
if( FAILED(hr) )
{
SAFE_RELEASE( pVB );
return hr;
}
hr = D3DXComputeBoundingSphere( (D3DXVECTOR3*)pVertices, g_Mesh.m_pMesh->GetNumVertices(),
D3DXGetFVFVertexSize(g_Mesh.m_pMesh->GetFVF()), &g_vObjectCenter,
&g_fObjectRadius );
pVB->Unlock();
SAFE_RELEASE( pVB );
if( FAILED(hr) )
return hr;
//將網(wǎng)格模型移動(dòng)到窗口中心
D3DXMatrixTranslation( &g_mCenterWorld, -g_vObjectCenter.x, -g_vObjectCenter.y, -g_vObjectCenter.z );
//編譯段落代碼到指定的緩沖區(qū)
V_RETURN( DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, L"FragmentLinker.fx" ) );
V_RETURN( D3DXGatherFragmentsFromFile( strPath, NULL, NULL, 0, &g_pCompiledFragments, NULL ) );
//創(chuàng)建段落鏈接器
V_RETURN( D3DXCreateFragmentLinker( pd3dDevice, 0, &g_pFragmentLinker ) );
//將段落添加到鏈接器
V_RETURN( g_pFragmentLinker->AddFragments( (DWORD*)g_pCompiledFragments->GetBufferPointer() ) );
//獲取并保存各段落句柄
//hew: 把選項(xiàng)加到下拉框里
CDXUTComboBox* pComboBox = NULL;
pComboBox = g_SampleUI.GetComboBox( IDC_LIGHTING );
pComboBox->RemoveAllItems();
pComboBox->AddItem( L"Ambient", (void*) g_pFragmentLinker->GetFragmentHandleByName( "AmbientFragment" ) );
pComboBox->AddItem( L"Ambient & Diffuse", (void*) g_pFragmentLinker->GetFragmentHandleByName( "AmbientDiffuseFragment" ) );
pComboBox = g_SampleUI.GetComboBox( IDC_ANIMATION );
pComboBox->RemoveAllItems();
pComboBox->AddItem( L"On", (void*) g_pFragmentLinker->GetFragmentHandleByName( "ProjectionFragment_Animated" ) );
pComboBox->AddItem( L"Off", (void*) g_pFragmentLinker->GetFragmentHandleByName( "ProjectionFragment_Static" ) );
//鏈接段落生成頂點(diǎn)渲染器
V_RETURN( LinkVertexShader() );
//查找效果文件, 編譯像素渲染器
ID3DXBuffer *pCode;
V_RETURN( DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, L"FragmentLinker.fx" ) );
V_RETURN( D3DXCompileShaderFromFile( strPath, NULL, NULL, "ModulateTexture", "ps_2_0", 0, &pCode, NULL, NULL ) );
//創(chuàng)建像素渲染器
hr = pd3dDevice->CreatePixelShader( (DWORD*)pCode->GetBufferPointer(), &g_pPixelShader );
pCode->Release();
//設(shè)置像素渲染器
V_RETURN( pd3dDevice->SetPixelShader(g_pPixelShader) );
//設(shè)置攝影機(jī)觀察參數(shù)
D3DXVECTOR3 vecEye(3.0f, 0.0f, -3.0f);
D3DXVECTOR3 vecAt (0.0f, 0.0f, -0.0f);
g_Camera.SetViewParams( &vecEye, &vecAt );
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: 輔助函數(shù), 鏈接段落生成頂點(diǎn)渲染器, 具體鏈接哪些段落通過下拉列表指定
//-----------------------------------------------------------------------------
HRESULT LinkVertexShader()
{
HRESULT hr;
ID3DXBuffer *pCode;
DWORD *pShaderCode;
IDirect3DDevice9 *pd3dDevice = DXUTGetD3DDevice();
const int NUM_FRAGMENTS = 2;
D3DXHANDLE aHandles[ NUM_FRAGMENTS ] = {0};
// hew: 取下拉框的選項(xiàng)
aHandles[ 0 ] = (D3DXHANDLE) g_SampleUI.GetComboBox( IDC_ANIMATION )->GetSelectedData();
aHandles[ 1 ] = (D3DXHANDLE) g_SampleUI.GetComboBox( IDC_LIGHTING )->GetSelectedData();
//先釋放原來鏈接生成的頂點(diǎn)渲染器
SAFE_RELEASE( g_pLinkedShader );
//鏈接段落生成頂點(diǎn)渲染器, 并獲取頂點(diǎn)渲染器常量列表
// hew: 根據(jù)下拉框的項(xiàng)LinkShader
V_RETURN( g_pFragmentLinker->LinkShader( "vs_2_0", 0, aHandles, NUM_FRAGMENTS, &pCode, NULL ) );
pShaderCode = (DWORD*) pCode->GetBufferPointer();
V_RETURN( pd3dDevice->CreateVertexShader( pShaderCode, &g_pLinkedShader ) );
V_RETURN( D3DXGetShaderConstantTable( pShaderCode, &g_pConstTable ) );
SAFE_RELEASE( pCode );
//為當(dāng)前渲染設(shè)備設(shè)置頂點(diǎn)渲染器
V_RETURN( pd3dDevice->SetVertexShader( g_pLinkedShader ) );
//通過常量表為頂點(diǎn)渲染器設(shè)置全局變量
if (g_pConstTable)
{
g_pConstTable->SetVector( pd3dDevice, "g_vMaterialAmbient", &g_vMaterialAmbient );
g_pConstTable->SetVector( pd3dDevice, "g_vMaterialDiffuse", &g_vMaterialDiffuse );
g_pConstTable->SetVector( pd3dDevice, "g_vLightColor", &g_vLightColor );
g_pConstTable->SetVector( pd3dDevice, "g_vLightPosition", &g_vLightPosition );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: 在此創(chuàng)建默認(rèn)內(nèi)存類型資源對(duì)象
//-----------------------------------------------------------------------------
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice,
const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
HRESULT hr;
V_RETURN( g_DialogResourceManager.OnResetDevice() );
V_RETURN( g_SettingsDlg.OnResetDevice() );
//設(shè)置對(duì)話框位置和尺寸
g_HUD.SetLocation( pBackBufferSurfaceDesc->Width-170, 0 );
g_HUD.SetSize( 170, 170 );
g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width-170, pBackBufferSurfaceDesc->Height-350 );
g_SampleUI.SetSize( 170, 300 );
//恢復(fù)字體
if( g_pFont )
V_RETURN( g_pFont->OnResetDevice() );
//創(chuàng)建ID3DXSprite接口對(duì)象
V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pTextSprite ) );
//設(shè)置攝影機(jī)投影參數(shù)
float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
g_Camera.SetProjParams( D3DX_PI/4, fAspectRatio, 0.1f, 1000.0f );
g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
//鏈接段落生成頂點(diǎn)渲染器
V_RETURN(LinkVertexShader());
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: 更新場(chǎng)景
//-----------------------------------------------------------------------------
void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime,
float fElapsedTime, void* pUserContext )
{
D3DXMATRIXA16 mWorld;
D3DXMATRIXA16 mView;
D3DXMATRIXA16 mProj;
D3DXMATRIXA16 mWorldViewProjection;
//根據(jù)用戶輸入更新攝影機(jī)位置
g_Camera.FrameMove( fElapsedTime );
//通過攝影機(jī)獲取觀察和投影矩陣
mWorld = g_mCenterWorld * *g_Camera.GetWorldMatrix();
mProj = *g_Camera.GetProjMatrix();
mView = *g_Camera.GetViewMatrix();
mWorldViewProjection = mWorld * mView * mProj;
//通過常量表, 為渲染器設(shè)置全局變量
if (g_pConstTable)
{
g_pConstTable->SetMatrix( pd3dDevice, "g_mWorldViewProjection", &mWorldViewProjection );
g_pConstTable->SetMatrix( pd3dDevice, "g_mWorld", &mWorld );
g_pConstTable->SetFloat( pd3dDevice, "g_fTime", (float)fTime );
}
}
//-----------------------------------------------------------------------------
// Desc: 渲染場(chǎng)景
//-----------------------------------------------------------------------------
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
//如果正在利用Direct3D設(shè)備設(shè)置對(duì)話框進(jìn)行設(shè)置, 則不渲染場(chǎng)景
if( g_SettingsDlg.IsActive() )
{
g_SettingsDlg.OnRender( fElapsedTime );
return;
}
HRESULT hr;
//清除后臺(tái)顏色緩沖區(qū)和深度緩沖區(qū)
V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0) );
//渲染場(chǎng)景
if( SUCCEEDED( pd3dDevice->BeginScene() ) )
{
//渲染網(wǎng)格模型
V( g_Mesh.Render( pd3dDevice ) );
//渲染文本和控件
RenderText();
V( g_HUD.OnRender( fElapsedTime ) );
V( g_SampleUI.OnRender( fElapsedTime ) );
V( pd3dDevice->EndScene() );
}
}
//-----------------------------------------------------------------------------
// Desc: 渲染文本
//-----------------------------------------------------------------------------
void RenderText()
{
CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
//顯示當(dāng)前Direct3D設(shè)備狀態(tài)和渲染幀速率
txtHelper.Begin();
txtHelper.SetInsertionPos( 5, 5 );
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
txtHelper.DrawTextLine( DXUTGetFrameStats(true) );
txtHelper.DrawTextLine( DXUTGetDeviceStats() );
txtHelper.DrawTextLine( L"Selected fragments are linked on-the-fly to create the current shader" );
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
//顯示其他簡(jiǎn)要信息
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
txtHelper.DrawTextLine( L"段落(Fragment)使用示例程序" );
//顯示簡(jiǎn)單幫助文本
if( g_bShowHelp )
{
const D3DSURFACE_DESC* pd3dsdBackBuffer = DXUTGetBackBufferSurfaceDesc();
txtHelper.SetInsertionPos( 10, pd3dsdBackBuffer->Height-15*6 );
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 0.75f, 0.0f, 1.0f ) );
txtHelper.DrawTextLine( L"Controls:" );
txtHelper.SetInsertionPos( 20, pd3dsdBackBuffer->Height-15*5 );
txtHelper.DrawTextLine( L"Rotate model: Left mouse button\n"
L"Rotate camera: Right mouse button\n"
L"Zoom camera: Mouse wheel scroll\n" );
txtHelper.SetInsertionPos( 250, pd3dsdBackBuffer->Height-15*5 );
txtHelper.DrawTextLine( L"Hide help: F1\n"
L"Quit: ESC\n" );
}
else
{
txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
txtHelper.DrawTextLine( L"Press F1 for help" );
}
txtHelper.End();
}
//-----------------------------------------------------------------------------
// Desc: 消息處理
//-----------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext )
{
*pbNoFurtherProcessing = g_DialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam );
if( *pbNoFurtherProcessing )
return 0;
if( g_SettingsDlg.IsActive() )
{
g_SettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam );
return 0;
}
*pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam );
if( *pbNoFurtherProcessing )
return 0;
*pbNoFurtherProcessing = g_SampleUI.MsgProc( hWnd, uMsg, wParam, lParam );
if( *pbNoFurtherProcessing )
return 0;
g_Camera.HandleMessages( hWnd, uMsg, wParam, lParam );
return 0;
}
//-----------------------------------------------------------------------------
// Desc: 鍵盤消息處理
//-----------------------------------------------------------------------------
void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
{
if( bKeyDown )
{
switch( nChar )
{
case VK_F1: g_bShowHelp = !g_bShowHelp; break;
}
}
}
//-----------------------------------------------------------------------------
// Desc: 各種控件消息處理
//-----------------------------------------------------------------------------
void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
{
switch( nControlID )
{
case IDC_TOGGLEFULLSCREEN: DXUTToggleFullScreen(); break;
case IDC_TOGGLEREF: DXUTToggleREF(); break;
case IDC_CHANGEDEVICE: g_SettingsDlg.SetActive( !g_SettingsDlg.IsActive() ); break;
case IDC_ANIMATION: LinkVertexShader(); break;
case IDC_LIGHTING: LinkVertexShader(); break;
}
}
//-----------------------------------------------------------------------------
// Desc: 釋放在OnResetDevice()中創(chuàng)建的資源
//-----------------------------------------------------------------------------
void CALLBACK OnLostDevice( void* pUserContext )
{
g_DialogResourceManager.OnLostDevice();
g_SettingsDlg.OnLostDevice();
if( g_pFont )
g_pFont->OnLostDevice();
SAFE_RELEASE(g_pTextSprite);
}
//------------------------------------------------------------------------------
// Desc: 釋放在OnCreateDevice()中創(chuàng)建的資源
//------------------------------------------------------------------------------
void CALLBACK OnDestroyDevice( void* pUserContext )
{
g_DialogResourceManager.OnDestroyDevice();
g_SettingsDlg.OnDestroyDevice();
SAFE_RELEASE( g_pPixelShader );
SAFE_RELEASE( g_pLinkedShader );
SAFE_RELEASE( g_pFont );
g_Mesh.Destroy();
SAFE_RELEASE( g_pFragmentLinker );
SAFE_RELEASE( g_pCompiledFragments );
}
posted on 2008-04-14 17:40
七星重劍 閱讀(956)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
Game Graphics