多层纹理混合

None.gif//=============================================================================
None.gif
//Desc:主程序源文件
None.gif
//=============================================================================
None.gif
#include"dxstdafx.h"
None.gif#include
"resource.h"
None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//全局变量
None.gif
//-----------------------------------------------------------------------------
None.gif
ID3DXFont*g_pFont=NULL;//ID3DXFont字体对象
None.gif
ID3DXSprite*g_pTextSprite=NULL;//ID3DXSprite文本精灵对象
None.gif
boolg_bShowHelp=true;//标识是否显示简单说明文本
None.gif

None.gifCDXUTDialogResourceManagerg_DialogResourceManager;
//对话框资源管理器
None.gif
CD3DSettingsDlgg_SettingsDlg;//Direct3D设备设置对话框
None.gif
CDXUTDialogg_HUD;//
None.gif
CDXUTDialogg_SampleUI;//
None.gif

None.gifLPDIRECT3DVERTEXBUFFER9g_pVB
=NULL;//顶点缓冲区
None.gif
LPDIRECT3DTEXTURE9g_pTexture1=NULL;//第一层纹理
None.gif
LPDIRECT3DTEXTURE9g_pTexture2=NULL;//第二层纹理
None.gif
None.gif
None.gif
//定义顶点结构和灵活顶点格式
None.gif
structCUSTOMVERTEX
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifFLOATx,y,z;
//未经过坐标转换的顶点坐标
InBlock.gif
DWORDcolor;//顶点漫反射颜色值
InBlock.gif
FLOATu,v;//顶点纹理坐标
InBlock.gif
FLOATu1,v1;//顶点纹理坐标
InBlock.gif

ExpandedBlockEnd.gif}
;
None.gif
#defineD3DFVF_CUSTOMVERTEX(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX2)
None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//控件ID
None.gif
//-----------------------------------------------------------------------------
None.gif
#defineIDC_TOGGLEFULLSCREEN1
None.gif
#defineIDC_TOGGLEREF2
None.gif
#defineIDC_CHANGEDEVICE3
None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:函数声明
None.gif
//------------------------------------------------------------------------------
None.gif
boolCALLBACKIsDeviceAcceptable(D3DCAPS9*pCaps,D3DFORMATAdapterFormat,D3DFORMATBackBufferFormat,boolbWindowed,void*pUserContext);
None.gif
boolCALLBACKModifyDeviceSettings(DXUTDeviceSettings*pDeviceSettings,constD3DCAPS9*pCaps,void*pUserContext);
None.gifHRESULTCALLBACKOnCreateDevice(IDirect3DDevice9
*pd3dDevice,constD3DSURFACE_DESC*pBackBufferSurfaceDesc,void*pUserContext);
None.gifHRESULTCALLBACKOnResetDevice(IDirect3DDevice9
*pd3dDevice,constD3DSURFACE_DESC*pBackBufferSurfaceDesc,void*pUserContext);
None.gif
voidCALLBACKOnFrameMove(IDirect3DDevice9*pd3dDevice,doublefTime,floatfElapsedTime,void*pUserContext);
None.gif
voidCALLBACKOnFrameRender(IDirect3DDevice9*pd3dDevice,doublefTime,floatfElapsedTime,void*pUserContext);
None.gifLRESULTCALLBACKMsgProc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam,
bool*pbNoFurtherProcessing,void*pUserContext);
None.gif
voidCALLBACKKeyboardProc(UINTnChar,boolbKeyDown,boolbAltDown,void*pUserContext);
None.gif
voidCALLBACKOnGUIEvent(UINTnEvent,intnControlID,CDXUTControl*pControl,void*pUserContext);
None.gif
voidCALLBACKOnLostDevice(void*pUserContext);
None.gif
voidCALLBACKOnDestroyDevice(void*pUserContext);
None.gif
None.gif
voidInitApp();
None.gif
voidRenderText();
None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:入口函数
None.gif
//-----------------------------------------------------------------------------
None.gif
INTWINAPIWinMain(HINSTANCE,HINSTANCE,LPSTR,int)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//为Debug配置启用运行时内存检查功能
InBlock.gif
#ifdefined(DEBUG)|defined(_DEBUG)
InBlock.gif_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF
|_CRTDBG_LEAK_CHECK_DF);
InBlock.gif
#endif
InBlock.gif
InBlock.gif
//设置回调函数
InBlock.gif
DXUTSetCallbackDeviceCreated(OnCreateDevice);
InBlock.gifDXUTSetCallbackDeviceReset(OnResetDevice);
InBlock.gifDXUTSetCallbackDeviceLost(OnLostDevice);
InBlock.gifDXUTSetCallbackDeviceDestroyed(OnDestroyDevice);
InBlock.gifDXUTSetCallbackMsgProc(MsgProc);
InBlock.gifDXUTSetCallbackKeyboard(KeyboardProc);
InBlock.gifDXUTSetCallbackFrameRender(OnFrameRender);
InBlock.gifDXUTSetCallbackFrameMove(OnFrameMove);
InBlock.gif
InBlock.gif
//应用程序相关的初始化
InBlock.gif
InitApp();
InBlock.gif
InBlock.gif
//初始化DXUT,创建窗口,创建Direct3D设备对象
InBlock.gif
DXUTInit(true,true,true);
InBlock.gifDXUTSetCursorSettings(
true,true);
InBlock.gifDXUTCreateWindow(L
"MultiTexture");
InBlock.gifDXUTCreateDevice(D3DADAPTER_DEFAULT,
true,640,480,
InBlock.gifIsDeviceAcceptable,ModifyDeviceSettings);
InBlock.gif
InBlock.gif
//进入消息循环和场景渲染
InBlock.gif
DXUTMainLoop();
InBlock.gif
InBlock.gif
//在此进行应用程序相关的清除工作
InBlock.gif

InBlock.gif
returnDXUTGetExitCode();
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:应用程序相关初始化
None.gif
//-----------------------------------------------------------------------------
None.gif
voidInitApp()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//初始化对话框
InBlock.gif
g_SettingsDlg.Init(&g_DialogResourceManager);
InBlock.gifg_HUD.Init(
&g_DialogResourceManager);
InBlock.gifg_SampleUI.Init(
&g_DialogResourceManager);
InBlock.gif
InBlock.gif
//为g_HUD对话框设置消息处理函数,添加控件
InBlock.gif
g_HUD.SetCallback(OnGUIEvent);intiY=10;
InBlock.gifg_HUD.AddButton(IDC_TOGGLEFULLSCREEN,L
"Togglefullscreen",35,iY,125,22);
InBlock.gifg_HUD.AddButton(IDC_TOGGLEREF,L
"ToggleREF(F3)",35,iY+=24,125,22);
InBlock.gifg_HUD.AddButton(IDC_CHANGEDEVICE,L
"Changedevice(F2)",35,iY+=24,125,22,VK_F2);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:设备能力检查
None.gif
//-----------------------------------------------------------------------------
None.gif
boolCALLBACKIsDeviceAcceptable(D3DCAPS9*pCaps,D3DFORMATAdapterFormat,
None.gifD3DFORMATBackBufferFormat,
boolbWindowed,
None.gif
void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//检查后台缓冲区格式是否支持Alpha混合等操作(postpixelblendingoperations)
InBlock.gif
IDirect3D9*pD3D=DXUTGetD3DObject();
InBlock.gif
if(FAILED(pD3D->CheckDeviceFormat(pCaps->AdapterOrdinal,pCaps->DeviceType,
InBlock.gifAdapterFormat,D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
InBlock.gifD3DRTYPE_TEXTURE,BackBufferFormat)))
InBlock.gif
returnfalse;
InBlock.gif
InBlock.gif
//检查当前渲染设备是否支持多层纹理混合
InBlock.gif
if(pCaps->MaxTextureBlendStages<=1)
InBlock.gif
returnfalse;
InBlock.gif
InBlock.gif
returntrue;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:修改Direct3D渲染设备设置
None.gif
//-----------------------------------------------------------------------------
None.gif
boolCALLBACKModifyDeviceSettings(DXUTDeviceSettings*pDeviceSettings,
None.gif
constD3DCAPS9*pCaps,void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//如果不支持硬件顶点处理则使用软件顶点处理
InBlock.gif
if((pCaps->DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDeviceSettings
->BehaviorFlags=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//如果使用参考设备,则弹出警告对话框
InBlock.gif
staticbools_bFirstTime=true;
InBlock.gif
if(s_bFirstTime)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifs_bFirstTime
=false;
InBlock.gif
if(pDeviceSettings->DeviceType==D3DDEVTYPE_REF)
InBlock.gifDXUTDisplaySwitchingToREFWarning();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
returntrue;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:在此创建管理内存资源对象
None.gif
//-----------------------------------------------------------------------------
None.gif
HRESULTCALLBACKOnCreateDevice(IDirect3DDevice9*pd3dDevice,
None.gif
constD3DSURFACE_DESC*pBackBufferSurfaceDesc,
None.gif
void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifHRESULThr;
InBlock.gif
InBlock.gifV_RETURN(g_DialogResourceManager.OnCreateDevice(pd3dDevice));
InBlock.gifV_RETURN(g_SettingsDlg.OnCreateDevice(pd3dDevice));
InBlock.gif
InBlock.gif
//创建字体
InBlock.gif
V_RETURN(D3DXCreateFont(pd3dDevice,15,0,FW_BOLD,1,FALSE,DEFAULT_CHARSET,
InBlock.gifOUT_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH
|FF_DONTCARE,
InBlock.gifL
"Arial",&g_pFont));
InBlock.gif
InBlock.gif
InBlock.gif
//创建第一层纹理对象
InBlock.gif
V_RETURN(D3DXCreateTextureFromFile(pd3dDevice,L"Wall.bmp",&g_pTexture1));
InBlock.gif
InBlock.gif
//创建第二层纹理对象
InBlock.gif
V_RETURN(D3DXCreateTextureFromFile(pd3dDevice,L"light.jpg",&g_pTexture2));
InBlock.gif
InBlock.gif
//设置顶点数据
InBlock.gif
CUSTOMVERTEXg_Vertices[]=
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{-3.0f,-3.0f,0.0f,0xffffffff,0.0f,1.0f,0.0f,1.0f},
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{-3.0f,3.0f,0.0f,0xffffffff,0.0f,0.0f,0.0f,0.0f},
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{3.0f,-3.0f,0.0f,0xffffffff,1.0f,1.0f,1.0f,1.0f},
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{3.0f,3.0f,0.0f,0xffffffff,1.0f,0.0f,1.0f,0.0f}
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//创建顶点缓冲区
InBlock.gif
V_RETURN(pd3dDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
InBlock.gif
0,D3DFVF_CUSTOMVERTEX,
InBlock.gifD3DPOOL_MANAGED,
&g_pVB,NULL));
InBlock.gif
//填充顶点数据
InBlock.gif
VOID*pVertices;
InBlock.gifV_RETURN(g_pVB
->Lock(0,sizeof(g_Vertices),(void**)&pVertices,0));
InBlock.gifmemcpy(pVertices,g_Vertices,
sizeof(g_Vertices));
InBlock.gifg_pVB
->Unlock();
InBlock.gif
InBlock.gif
returnS_OK;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:在此创建默认内存类型资源对象
None.gif
//-----------------------------------------------------------------------------
None.gif
HRESULTCALLBACKOnResetDevice(IDirect3DDevice9*pd3dDevice,
None.gif
constD3DSURFACE_DESC*pBackBufferSurfaceDesc,
None.gif
void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifHRESULThr;
InBlock.gif
InBlock.gifV_RETURN(g_DialogResourceManager.OnResetDevice());
InBlock.gifV_RETURN(g_SettingsDlg.OnResetDevice());
InBlock.gif
InBlock.gif
//设置对话框位置和尺寸
InBlock.gif
g_HUD.SetLocation(pBackBufferSurfaceDesc->Width-170,0);
InBlock.gifg_HUD.SetSize(
170,170);
InBlock.gifg_SampleUI.SetLocation(pBackBufferSurfaceDesc
->Width-170,
InBlock.gifpBackBufferSurfaceDesc
->Height-350);
InBlock.gifg_SampleUI.SetSize(
170,300);
InBlock.gif
InBlock.gif
//恢复字体
InBlock.gif
if(g_pFont)
InBlock.gifV_RETURN(g_pFont
->OnResetDevice());
InBlock.gif
InBlock.gif
//创建ID3DXSprite接口对象
InBlock.gif
V_RETURN(D3DXCreateSprite(pd3dDevice,&g_pTextSprite));
InBlock.gif
InBlock.gif
//设置观察矩阵
InBlock.gif
D3DXMATRIXA16matView;
InBlock.gifD3DXVECTOR3vEyePt(
0.0f,0.0f,-8);
InBlock.gifD3DXVECTOR3vLookatPt(
0.0f,0.0f,0.0f);
InBlock.gifD3DXVECTOR3vUpVec(
0.0f,1.0f,0.0f);
InBlock.gifD3DXMatrixLookAtLH(
&matView,&vEyePt,&vLookatPt,&vUpVec);
InBlock.gifpd3dDevice
->SetTransform(D3DTS_VIEW,&matView);
InBlock.gif
InBlock.gif
//设置投影矩阵
InBlock.gif
D3DXMATRIXA16matProj;
InBlock.gif
floatfAspectRatio=(float)pBackBufferSurfaceDesc->Width/pBackBufferSurfaceDesc->Height;
InBlock.gifD3DXMatrixPerspectiveFovLH(
&matProj,D3DX_PI/4,fAspectRatio,1.0f,100.0f);
InBlock.gifpd3dDevice
->SetTransform(D3DTS_PROJECTION,&matProj);
InBlock.gif
InBlock.gif
//设置第层纹理、纹理阶段状态和纹理坐标索引
InBlock.gif
pd3dDevice->SetTexture(0,g_pTexture1);
InBlock.gifpd3dDevice
->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
InBlock.gifpd3dDevice
->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
InBlock.gifpd3dDevice
->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);
InBlock.gifpd3dDevice
->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE);
InBlock.gifpd3dDevice
->SetTextureStageState(0,D3DTSS_TEXCOORDINDEX,0);
InBlock.gifpd3dDevice
->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
InBlock.gifpd3dDevice
->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
InBlock.gif
InBlock.gif
//设置第层纹理、纹理阶段状态和纹理坐标索引
InBlock.gif
pd3dDevice->SetTexture(1,g_pTexture2);
InBlock.gifpd3dDevice
->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_ADD);
InBlock.gifpd3dDevice
->SetTextureStageState(1,D3DTSS_COLORARG1,D3DTA_TEXTURE);
InBlock.gifpd3dDevice
->SetTextureStageState(1,D3DTSS_COLORARG2,D3DTA_CURRENT);
InBlock.gifpd3dDevice
->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_DISABLE);
InBlock.gifpd3dDevice
->SetTextureStageState(1,D3DTSS_TEXCOORDINDEX,1);
InBlock.gifpd3dDevice
->SetSamplerState(1,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
InBlock.gifpd3dDevice
->SetSamplerState(1,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
InBlock.gif
InBlock.gif
//关闭光照计算
InBlock.gif
pd3dDevice->SetRenderState(D3DRS_LIGHTING,false);
InBlock.gif
InBlock.gif
returnS_OK;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:更新场景
None.gif
//-----------------------------------------------------------------------------
None.gif
voidCALLBACKOnFrameMove(IDirect3DDevice9*pd3dDevice,doublefTime,
None.gif
floatfElapsedTime,void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:渲染场景
None.gif
//-----------------------------------------------------------------------------
None.gif
voidCALLBACKOnFrameRender(IDirect3DDevice9*pd3dDevice,doublefTime,
None.gif
floatfElapsedTime,void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifHRESULThr;
InBlock.gif
InBlock.gif
//如果正在利用Direct3D设备设置对话框进行设置,则不渲染场景
InBlock.gif
if(g_SettingsDlg.IsActive())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifg_SettingsDlg.OnRender(fElapsedTime);
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//清除后台颜色缓冲区和深度缓冲区
InBlock.gif
V(pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
InBlock.gifD3DCOLOR_ARGB(
0,45,50,170),1.0f,0));
InBlock.gif
InBlock.gif
//渲染场景
InBlock.gif
if(SUCCEEDED(pd3dDevice->BeginScene()))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpd3dDevice
->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX));
InBlock.gifpd3dDevice
->SetFVF(D3DFVF_CUSTOMVERTEX);
InBlock.gifpd3dDevice
->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
InBlock.gif
InBlock.gif
//渲染文本和控件
InBlock.gif
DXUT_BeginPerfEvent(DXUT_PERFEVENTCOLOR,L"HUD/Stats");
InBlock.gifRenderText();
InBlock.gifV(g_HUD.OnRender(fElapsedTime));
InBlock.gifV(g_SampleUI.OnRender(fElapsedTime));
InBlock.gifDXUT_EndPerfEvent();
InBlock.gif
InBlock.gifV(pd3dDevice
->EndScene());
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:渲染文本
None.gif
//-----------------------------------------------------------------------------
None.gif
voidRenderText()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifCDXUTTextHelpertxtHelper(g_pFont,g_pTextSprite,
15);
InBlock.gif
InBlock.gif
//显示当前Direct3D设备状态和渲染帧速率
InBlock.gif
txtHelper.Begin();
InBlock.giftxtHelper.SetInsertionPos(
5,5);
InBlock.giftxtHelper.SetForegroundColor(D3DXCOLOR(
1.0f,1.0f,0.0f,1.0f));
InBlock.giftxtHelper.DrawTextLine(DXUTGetFrameStats(
true));
InBlock.giftxtHelper.DrawTextLine(DXUTGetDeviceStats());
InBlock.gif
InBlock.gif
//显示其他简要信息
InBlock.gif
txtHelper.SetForegroundColor(D3DXCOLOR(1.0f,1.0f,1.0f,1.0f));
InBlock.giftxtHelper.DrawTextLine(L
"多层纹理混合");
InBlock.gif
InBlock.gif
//显示简单帮助文本
InBlock.gif
constD3DSURFACE_DESC*pd3dsdBackBuffer=DXUTGetBackBufferSurfaceDesc();
InBlock.gif
if(g_bShowHelp)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giftxtHelper.SetInsertionPos(
10,pd3dsdBackBuffer->Height-15*6);
InBlock.giftxtHelper.SetForegroundColor(D3DXCOLOR(
1.0f,0.75f,0.0f,1.0f));
InBlock.giftxtHelper.DrawTextLine(L
"Controls(F1tohide):");
InBlock.gif
InBlock.giftxtHelper.SetInsertionPos(
40,pd3dsdBackBuffer->Height-15*5);
InBlock.giftxtHelper.DrawTextLine(L
"Quit:ESC");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giftxtHelper.SetInsertionPos(
10,pd3dsdBackBuffer->Height-15*2);
InBlock.giftxtHelper.SetForegroundColor(D3DXCOLOR(
1.0f,1.0f,1.0f,1.0f));
InBlock.giftxtHelper.DrawTextLine(L
"PressF1forhelp");
ExpandedSubBlockEnd.gif}

InBlock.giftxtHelper.End();
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:消息处理
None.gif
//-----------------------------------------------------------------------------
None.gif
LRESULTCALLBACKMsgProc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam,
None.gif
bool*pbNoFurtherProcessing,void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
*pbNoFurtherProcessing=g_DialogResourceManager.MsgProc(hWnd,uMsg,wParam,lParam);
InBlock.gif
if(*pbNoFurtherProcessing)
InBlock.gif
return0;
InBlock.gif
InBlock.gif
if(g_SettingsDlg.IsActive())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifg_SettingsDlg.MsgProc(hWnd,uMsg,wParam,lParam);
InBlock.gif
return0;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
*pbNoFurtherProcessing=g_HUD.MsgProc(hWnd,uMsg,wParam,lParam);
InBlock.gif
if(*pbNoFurtherProcessing)
InBlock.gif
return0;
InBlock.gif
InBlock.gif
*pbNoFurtherProcessing=g_SampleUI.MsgProc(hWnd,uMsg,wParam,lParam);
InBlock.gif
if(*pbNoFurtherProcessing)
InBlock.gif
return0;
InBlock.gif
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:键盘消息处理
None.gif
//-----------------------------------------------------------------------------
None.gif
voidCALLBACKKeyboardProc(UINTnChar,boolbKeyDown,boolbAltDown,void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(bKeyDown)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
switch(nChar)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseVK_F1:g_bShowHelp=!g_bShowHelp;break;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:处理各种控件消息
None.gif
//-----------------------------------------------------------------------------
None.gif
voidCALLBACKOnGUIEvent(UINTnEvent,intnControlID,CDXUTControl*pControl,
None.gif
void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
switch(nControlID)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseIDC_TOGGLEFULLSCREEN:
InBlock.gifDXUTToggleFullScreen();
InBlock.gif
break;
InBlock.gif
InBlock.gif
caseIDC_TOGGLEREF:
InBlock.gifDXUTToggleREF();
InBlock.gif
break;
InBlock.gif
InBlock.gif
caseIDC_CHANGEDEVICE:
InBlock.gifg_SettingsDlg.SetActive(
!g_SettingsDlg.IsActive());
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:释放在OnResetDevice()中创建的资源
None.gif
//-----------------------------------------------------------------------------
None.gif
voidCALLBACKOnLostDevice(void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifg_DialogResourceManager.OnLostDevice();
InBlock.gifg_SettingsDlg.OnLostDevice();
InBlock.gif
if(g_pFont)
InBlock.gifg_pFont
->OnLostDevice();
InBlock.gifSAFE_RELEASE(g_pTextSprite);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//------------------------------------------------------------------------------
None.gif
//Desc:释放在OnCreateDevice()中创建的资源
None.gif
//------------------------------------------------------------------------------
None.gif
voidCALLBACKOnDestroyDevice(void*pUserContext)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifg_DialogResourceManager.OnDestroyDevice();
InBlock.gifg_SettingsDlg.OnDestroyDevice();
InBlock.gifSAFE_RELEASE(g_pFont);
InBlock.gif
InBlock.gifSAFE_RELEASE(g_pVB);
InBlock.gifSAFE_RELEASE(g_pTexture1);
InBlock.gifSAFE_RELEASE(g_pTexture2);
ExpandedBlockEnd.gif}

None.gif
None.gif
阅读更多

纹理混合+光照?

03-05

这样的shader哪错了,渲染的效果是全黑:rnstruct Mtrlrnrn float4 ambient;rn float4 diffuse;rn float4 spec;rn float specPower;rn;rnrnstruct SpotLightrnrn float4 ambient;rn float4 diffuse;rn float4 spec;rn float3 posW;rn float3 dirW; rn float spotPower;rn;rnuniform extern float4x4 gViewProj;rnuniform extern texture gTex0;rnuniform extern texture gTex1;rnuniform extern texture gTex2; //三张文理图rnuniform extern texture gBlendMap; //extern 表示输入Shader的这个变量的值是统一的(也就是说这个变量的初始化值来源于Shader外界的输入).rnrnuniform extern float3 gEyePosW; //世界空间的摄像机的位置rnrn//加入灯光rnuniform extern Mtrl gMtrl;rnuniform extern SpotLight gLight;rnrnstatic float gTexScale = 16.0f;rnrnsampler Tex0S = sampler_staternrn Texture = ;rn MinFilter = LINEAR;rn MagFilter = LINEAR;rn MipFilter = POINT;rn AddressU = WRAP;rn AddressV = WRAP;rn;rnsampler Tex1S = sampler_staternrn Texture = ;rn MinFilter = LINEAR;rn MagFilter = LINEAR;rn MipFilter = POINT;rn AddressU = WRAP;rn AddressV = WRAP;rn;rnsampler Tex2S = sampler_staternrn Texture = ;rn MinFilter = LINEAR;rn MagFilter = LINEAR;rn MipFilter = POINT;rn AddressU = WRAP;rn AddressV = WRAP;rn;rnsampler BlendMapS = sampler_staternrn Texture = ;rn MinFilter = LINEAR;rn MagFilter = LINEAR;rn MipFilter = POINT;rn AddressU = WRAP;rn AddressV = WRAP;rn;rnrnstruct OutputVSrnrn float4 posH :POSITION0;rn float2 tiledTexC :TEXCOORD0;rn float2 nonTiledTexC :TEXCOORD1;rn float shade :TEXCOORD2;rn float3 normalW :TEXCOORD3;rn float3 posW :TEXCOORD4;//经过世界变换的顶点坐标rn float3 toEyeW :TEXCOORD5;//方向向量rn;rnOutputVS TerrainVS(float3 posL:POSITION0,rn float3 normalL:NORMAL0,rn float2 tex0:TEXCOORD0)rn rn OutputVS outVS = (OutputVS)0;rn float3 normalW = normalL;//这里的地形不需要经过世界变换rn rn outVS.shade = 1.0f;rn outVS.posH = mul(float4(posL,1.0f),gViewProj);rn outVS.tiledTexC = tex0*gTexScale;rn outVS.nonTiledTexC = tex0;rn outVS.normalW = normalW;rn outVS.posW = posL; rn outVS.toEyeW = gEyePosW - posL; //摄像机和顶点的位置rn return outVS;rnrnfloat4 TerrainPS(float2 tiledTexC:TEXCOORD0,rn float2 nonTiledTexC:TEXCOORD1,rn float shade:TEXCOORD2,rn float3 normalW : TEXCOORD3,rn float3 posW : TEXCOORD4,rn float3 toEyeW : TEXCOORD5rn ):COLORrnrn //计算光照rn normalW = normalize(normalW);rn toEyeW = normalize(toEyeW);rn float3 lightVecW = normalize(gLight.posW - posW);rn //计算反射向量rn float3 r = reflect(-lightVecW, normalW);rn //计算光照rn float t = pow(max(dot(r, toEyeW), 0.00001f), gMtrl.specPower);rn float s = max(dot(lightVecW, normalW), 0.00001f);rn float3 spec = t * (gMtrl.spec * gLight.spec).rgb;rn float3 diffuse = s * (gMtrl.diffuse * gLight.diffuse).rgb;//待看rn float3 ambient = gMtrl.ambient * gLight.ambient;rn float spot = pow(max( dot(-lightVecW, gLight.dirW), 0.00001f), gLight.spotPower);rn //计算纹理的颜色rn float3 c0 = tex2D(Tex0S,tiledTexC).rgb;rn float3 c1 = tex2D(Tex1S,tiledTexC).rgb;rn float3 c2 = tex2D(Tex2S,tiledTexC).rgb;rn float3 B = tex2D(BlendMapS,nonTiledTexC).rgb; rn float totalInverse = 1.0f/(B.r+B.g+B.b);rn c0 = c0 * B.r * totalInverse;rn c1 = c1 * B.g * totalInverse;rn c2 = c2 * B.b * totalInverse;rn float3 final = (c0 + c1 + c2) * shade; rn float4 texColor = float4(final,1.0f);rn //计算最后的颜色 rn rn float3 litColor = spot*ambient*texColor.rgb + spot*1.0f*(diffuse*texColor.rgb + spec); rn return float4(litColor,gMtrl.diffuse.a*texColor.a);rnrnrntechnique TerrainTechrnrn pass P0rn rn vertexShader = compile vs_3_0 TerrainVS();rn pixelShader = compile ps_3_0 TerrainPS();rn rn

地形纹理混合问题

04-22

我的问题如下rn 我有四张纹理图,分四次进行渲染,然后得到了四层纹理的混合的地形效果。现在我需要加上第五层渲染,这层渲染得功能 是让我拾取的地形三角形显示为红色。我先在第五层渲染得时候选择漫反射颜色为红色,但是实际效果上,并没有显示红色,而是 根据漫反射颜色的alpha通道的值进行了透明处理,如果这个值为ff就是纯黑色,不知道是什么问题,我把主要代码放在下面了rn[code=C/C++]rndevice->SetTexture(0, Tex_1);rn device->SetRenderState( D3DRS_ALPHABLENDENABLE,TRUE);rn device->SetRenderState( D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);rn device->SetRenderState( D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);rn device->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX,0);rn device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );rn device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); rn //device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTA_DIFFUSE ); rn device->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );rn device->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );rn device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0,rowidth*colheight,0,(rowidth-1)*(colheight-1)*2);rn device->SetTexture(0, Tex_2);rn device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );rn device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );rn device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0,rowidth*colheight,0,(rowidth-1)*(colheight-1)*2);rn device->SetTexture(0, Tex_3);rn device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );rn device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );rn device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0,rowidth*colheight,0,(rowidth-1)*(colheight-1)*2);rn device->SetTexture(0, Tex_4);rn rn device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);rn device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );rn rn device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );rn device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0,rowidth*colheight,0,(rowidth-1)*(colheight-1)*2);rn MyTr->Lock(0,0,(void **) &mymap,0);rn for (int i =0;i<64*64;i++)rn //mymap[i]._color=(diffusevt[i]<<24);rn rn rn mymap[i]._color=0xccff0000;rn rn rn rn MyTr->Unlock();rn rn device->SetTexture(0, Tex_4);rn device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );rn rn device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );rn device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0,rowidth*colheight,0,(rowidth-1)*(colheight-1)*2);rn[/code]

DX纹理混合的一个问题。

09-12

// 纹理创建rnD3DXCreateTextureFromFileEx(m_pD3DDevice, "Media\\纯阳子.png", 0, 0, 0, 0,rn D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0xffffffff, &info, NULL, &m_pTextures[ETEX_ACTOR]);rnrnrn// 启用融合运算rnm_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true); rnm_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); rnm_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); rnrn// 最后渲染。rn...rnrn这是我主要代码,然后我的“纯阳子.png”图片是一张带有半透明的图rn这样渲染出来,没有问题,该半透明的地方就是半透明。rnrn但是现在有个问题:rn如何修改这个图片的整体透明度?rn如果我用的不是一张半透明图片,那简单,直接这样:rn m_pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);rn m_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);rn m_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE); rn但现在问题是,我这张图片本身已经含有半透明信息,如果也这样做的话,那原本纹理中完全透明或半透明的地方将都会被黑色替代(即,若修改alpha为255,则黑色为255,若修改alpha为100,则黑色为100)rnrn整个问题,简单说就是,已知一张含有半透明的png图片,如何在程序中修改其整体的alpha值..rnrn有没朋友知道我这种情况有没办法修改这个纹理的整体alpha??

没有更多推荐了,返回首页