C++ Exercises(十四)

None.gif//=============================================================================
None.gif
//Desc:坐标变换
None.gif
//=============================================================================
None.gif

None.gif#include
<d3dx9.h>//D3DX实用库函数,该头文件中又包含了d3d9.h头文件
None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:全局变量
None.gif
//-----------------------------------------------------------------------------
None.gif
LPDIRECT3D9g_pD3D=NULL;//Direct3D对象
None.gif
LPDIRECT3DDEVICE9g_pd3dDevice=NULL;//Direct3D设备对象
None.gif
LPDIRECT3DVERTEXBUFFER9g_pVB=NULL;//顶点缓冲区对象
None.gif
HWNDg_Wnd=NULL;//窗口句柄
None.gif
D3DRECTg_ClentRect;
None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:顶点结构
None.gif
//-----------------------------------------------------------------------------
None.gif
structCUSTOMVERTEX
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifD3DXVECTOR3position;
InBlock.gifDWORDcolor;
ExpandedBlockEnd.gif}
;
None.gif
#defineD3DFVF_CUSTOMVERTEX(D3DFVF_XYZ|D3DFVF_DIFFUSE)
None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:设置世界矩阵
None.gif
//-----------------------------------------------------------------------------
None.gif
VOIDSetupWorldMatrice()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//建立一个绕X轴动态旋转的世界矩阵
InBlock.gif
D3DXMATRIXmatWorld;
InBlock.gifUINTiTime
=timeGetTime()%1000;
InBlock.gifFLOATfAngle
=iTime*(2.0f*D3DX_PI)/1000.0f;//旋转角度
InBlock.gif
D3DXMatrixIdentity(&matWorld);//单位矩阵
InBlock.gif
D3DXMatrixRotationX(&matWorld,fAngle);//饶x轴选择
InBlock.gif
InBlock.gif
//设置世界矩阵
InBlock.gif
g_pd3dDevice->SetTransform(D3DTS_WORLD,&matWorld);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:设置观察矩阵和投影矩阵
None.gif
//-----------------------------------------------------------------------------
None.gif
VOIDSetupViewandProjMatrices()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//建立并设置观察矩阵
InBlock.gif
D3DXVECTOR3vEyePt(0.0f,3.0f,-5.0f);
InBlock.gifD3DXVECTOR3vLookatPt(
0.0f,0.0f,0.0f);
InBlock.gifD3DXVECTOR3vUpVec(
0.0f,1.0f,0.0f);
InBlock.gifD3DXMATRIXmatView;
InBlock.gifD3DXMatrixLookAtLH(
&matView,&vEyePt,&vLookatPt,&vUpVec);
InBlock.gifg_pd3dDevice
->SetTransform(D3DTS_VIEW,&matView);
InBlock.gif
InBlock.gif
//建立并设置投影矩阵
InBlock.gif
D3DXMATRIXmatProj;
InBlock.gifD3DXMatrixPerspectiveFovLH(
&matProj,D3DX_PI/4,1.0f,1.0f,100.0f);
InBlock.gifg_pd3dDevice
->SetTransform(D3DTS_PROJECTION,&matProj);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:设置视区
None.gif
//-----------------------------------------------------------------------------
None.gif
VOIDSetupViewPort()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifRECTrect;
InBlock.gifGetClientRect(g_Wnd,
&rect);
InBlock.gifD3DVIEWPORT9vp;
InBlock.gifvp.X
=0;
InBlock.gifvp.Y
=0;
InBlock.gifvp.Width
=rect.right;
InBlock.gifvp.Height
=rect.bottom;
InBlock.gifvp.MinZ
=0.0f;
InBlock.gifvp.MaxZ
=1.0f;
InBlock.gifg_pd3dDevice
->SetViewport(&vp);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:初始化Direct3D
None.gif
//-----------------------------------------------------------------------------
None.gif
HRESULTInitD3D(HWNDhWnd)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//创建Direct3D对象,该对象用于创建Direct3D设备对象
InBlock.gif
if(NULL==(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
InBlock.gif
returnE_FAIL;
InBlock.gif
InBlock.gif
//设置D3DPRESENT_PARAMETERS结构,准备创建Direct3D设备对象
InBlock.gif
D3DPRESENT_PARAMETERSd3dpp;
InBlock.gifZeroMemory(
&d3dpp,sizeof(d3dpp));
InBlock.gifd3dpp.Windowed
=TRUE;
InBlock.gifd3dpp.SwapEffect
=D3DSWAPEFFECT_DISCARD;
InBlock.gifd3dpp.BackBufferFormat
=D3DFMT_UNKNOWN;
InBlock.gif
InBlock.gif
//创建Direct3D设备对象
InBlock.gif
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
InBlock.gifD3DCREATE_SOFTWARE_VERTEXPROCESSING,
InBlock.gif
&d3dpp,&g_pd3dDevice)))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
returnE_FAIL;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//设置剔出模式为不剔出任何面(正面和反面)
InBlock.gif
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
InBlock.gif
InBlock.gif
//关闭光照处理,默认情况下启用光照处理
InBlock.gif
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
InBlock.gif
InBlock.gif
//设置观察和投影矩阵
InBlock.gif
SetupViewandProjMatrices();
InBlock.gif
InBlock.gif
//设置视区
InBlock.gif
SetupViewPort();
InBlock.gif
InBlock.gif
returnS_OK;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:创建场景图形
None.gif
//-----------------------------------------------------------------------------
None.gif
HRESULTInitGeometry()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//创顶点缓冲区
InBlock.gif
if(FAILED(g_pd3dDevice->CreateVertexBuffer(50*2*sizeof(CUSTOMVERTEX),
InBlock.gif
0,D3DFVF_CUSTOMVERTEX,
InBlock.gifD3DPOOL_DEFAULT,
&g_pVB,NULL)))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
returnE_FAIL;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//填充顶点缓冲区
InBlock.gif
CUSTOMVERTEX*pVertices;
InBlock.gif
if(FAILED(g_pVB->Lock(0,0,(void**)&pVertices,0)))
InBlock.gif
returnE_FAIL;
InBlock.gif
for(DWORDi=0;i<50;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifFLOATtheta
=(2*D3DX_PI*i)/(50-1);
InBlock.gifpVertices[
2*i+0].position=D3DXVECTOR3(sinf(theta),-1.0f,cosf(theta));
InBlock.gifpVertices[
2*i+0].color=0xffffff00;
InBlock.gifpVertices[
2*i+1].position=D3DXVECTOR3(sinf(theta),1.0f,cosf(theta));
InBlock.gifpVertices[
2*i+1].color=0xffffff00;
ExpandedSubBlockEnd.gif}

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
VOIDCleanup()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//释放顶点缓冲区对象
InBlock.gif
if(g_pVB!=NULL)
InBlock.gifg_pVB
->Release();
InBlock.gif
InBlock.gif
//释放Direct3D设备对象
InBlock.gif
if(g_pd3dDevice!=NULL)
InBlock.gifg_pd3dDevice
->Release();
InBlock.gif
InBlock.gif
//释放Direct3D对象
InBlock.gif
if(g_pD3D!=NULL)
InBlock.gifg_pD3D
->Release();
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:渲染图形
None.gif
//-----------------------------------------------------------------------------
None.gif
VOIDRender()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//清空后台缓冲区
InBlock.gif
g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(45,50,170),1.0f,0);
InBlock.gif
InBlock.gif
//开始在后台缓冲区绘制图形
InBlock.gif
if(SUCCEEDED(g_pd3dDevice->BeginScene()))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//设置世界矩阵
InBlock.gif
SetupWorldMatrice();
InBlock.gif
InBlock.gif
//在后台缓冲区绘制图形
InBlock.gif
g_pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX));
InBlock.gifg_pd3dDevice
->SetFVF(D3DFVF_CUSTOMVERTEX);
InBlock.gifg_pd3dDevice
->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2*50-2);
InBlock.gif
InBlock.gif
//结束在后台缓冲区渲染图形
InBlock.gif
g_pd3dDevice->EndScene();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//将在后台缓冲区绘制的图形提交到前台缓冲区显示
InBlock.gif
g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:消息处理
None.gif
//-----------------------------------------------------------------------------
None.gif
LRESULTWINAPIMsgProc(HWNDhWnd,UINTmsg,WPARAMwParam,LPARAMlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
switch(msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseWM_DESTROY:
InBlock.gifCleanup();
InBlock.gifPostQuitMessage(
0);
InBlock.gif
return0;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
returnDefWindowProc(hWnd,msg,wParam,lParam);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
//Desc:入口函数
None.gif
//-----------------------------------------------------------------------------
None.gif
INTWINAPIWinMain(HINSTANCEhInst,HINSTANCE,LPSTR,INT)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//注册窗口类
ExpandedSubBlockStart.gifContractedSubBlock.gif
WNDCLASSEXwc=dot.gif{sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,
InBlock.gifGetModuleHandle(NULL),NULL,NULL,NULL,NULL,
ExpandedSubBlockEnd.gifL
"ClassName",NULL}
;
InBlock.gifRegisterClassEx(
&wc);
InBlock.gif
InBlock.gif
//创建窗口
InBlock.gif
HWNDhWnd=CreateWindow(L"ClassName",L"坐标变换",
InBlock.gifWS_OVERLAPPEDWINDOW,
200,100,500,500,
InBlock.gifGetDesktopWindow(),NULL,wc.hInstance,NULL);
InBlock.gifg_Wnd
=hWnd;
InBlock.gif
InBlock.gif
//初始化Direct3D
InBlock.gif
if(SUCCEEDED(InitD3D(hWnd)))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//创建场景图形
InBlock.gif
if(SUCCEEDED(InitGeometry()))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//显示窗口
InBlock.gif
ShowWindow(hWnd,SW_SHOWDEFAULT);
InBlock.gifUpdateWindow(hWnd);
InBlock.gif
InBlock.gif
//进入消息循环
InBlock.gif
MSGmsg;
InBlock.gifZeroMemory(
&msg,sizeof(msg));
InBlock.gif
while(msg.message!=WM_QUIT)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifTranslateMessage(
&msg);
InBlock.gifDispatchMessage(
&msg);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifRender();
//渲染图形
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifUnregisterClass(L
"ClassName",wc.hInstance);
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
None.gif
阅读更多
换一批

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