1,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
#include<d3d9.h>
#pragmawarning(disable:4996)//disabledeprecatedwarning
#include<strsafe.h>
#pragmawarning(default:4996)
//-----------------------------------------------------------------------------
//Globalvariables
//-----------------------------------------------------------------------------
LPDIRECT3D9g_pD3D=NULL;//UsedtocreatetheD3DDevice
LPDIRECT3DDEVICE9g_pd3dDevice=NULL;//Ourrenderingdevice
//-----------------------------------------------------------------------------
//Name:InitD3D()
//Desc:InitializesDirect3D
//-----------------------------------------------------------------------------
HRESULTInitD3D(HWNDhWnd)

{
//CreatetheD3Dobject,whichisneededtocreatetheD3DDevice.
if(NULL==(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
returnE_FAIL;
//SetupthestructureusedtocreatetheD3DDevice.Mostparametersare
//zeroedout.WesetWindowedtoTRUE,sincewewanttodoD3Dina
//window,andthensettheSwapEffectto"discard",whichisthemost
//efficientmethodofpresentingthebackbuffertothedisplay.And
//werequestabackbufferformatthatmatchesthecurrentdesktopdisplay
//format.
D3DPRESENT_PARAMETERSd3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.Windowed=TRUE;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
//CreatetheDirect3Ddevice.Hereweareusingthedefaultadapter(most
//systemsonlyhaveone,unlesstheyhavemultiplegraphicshardwarecards
//installed)andrequestingtheHAL(whichissayingwewantthehardware
//deviceratherthanasoftwareone).Softwarevertexprocessingis
//specifiedsinceweknowitwillworkonallcards.Oncardsthatsupport
//hardwarevertexprocessing,though,wewouldseeabigperformancegain
//byspecifyinghardwarevertexprocessing.
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,&g_pd3dDevice)))

{
returnE_FAIL;
}
//Devicestatewouldnormallybesethere
returnS_OK;
}
//-----------------------------------------------------------------------------
//Name:Cleanup()
//Desc:Releasesallpreviouslyinitializedobjects
//-----------------------------------------------------------------------------
VOIDCleanup()

{
//释放D3D设备对象
if(g_pd3dDevice!=NULL)
g_pd3dDevice->Release();
//释放D3D对象
if(g_pD3D!=NULL)
g_pD3D->Release();
}

//-----------------------------------------------------------------------------
//Name:Render()
//Desc:Drawsthescene
//-----------------------------------------------------------------------------
VOIDRender()

{
if(NULL==g_pd3dDevice)
return;
//Clearthebackbuffertoabluecolor
g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);
//Beginthescene
if(SUCCEEDED(g_pd3dDevice->BeginScene()))

{
//Renderingofsceneobjectscanhappenhere
//Endthescene
g_pd3dDevice->EndScene();
}
//Presentthebackbuffercontentstothedisplay
g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
}
//-----------------------------------------------------------------------------
//Name:MsgProc()
//Desc:Thewindow'smessagehandler
//-----------------------------------------------------------------------------
LRESULTWINAPIMsgProc(HWNDhWnd,UINTmsg,WPARAMwParam,LPARAMlParam)

{
switch(msg)

{
caseWM_DESTROY:
Cleanup();
PostQuitMessage(0);
return0;
caseWM_PAINT:
Render();
ValidateRect(hWnd,NULL);
return0;
}
returnDefWindowProc(hWnd,msg,wParam,lParam);
}

//-----------------------------------------------------------------------------
//Name:WinMain()
//Desc:Theapplication'sentrypoint
//-----------------------------------------------------------------------------
INTWINAPIWinMain(HINSTANCEhInst,HINSTANCE,LPSTR,INT)

{
//Registerthewindowclass
WNDCLASSEXwc=
{sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,
GetModuleHandle(NULL),NULL,NULL,NULL,NULL,
"D3DTutorial",NULL};
RegisterClassEx(&wc);
//Createtheapplication'swindow
HWNDhWnd=CreateWindow("D3DTutorial","D3DTutorial01:CreateDevice",
WS_OVERLAPPEDWINDOW,100,100,300,300,
NULL,NULL,wc.hInstance,NULL);
//InitializeDirect3D
if(SUCCEEDED(InitD3D(hWnd)))

{//初始化成功
//Showthewindow
ShowWindow(hWnd,SW_SHOWDEFAULT);
UpdateWindow(hWnd);
//Enterthemessageloop
MSGmsg;
while(true)

{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))

{//消息处理
if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else

{//空闲时绘制
Render();
}
}
}
UnregisterClass("D3DTutorial",wc.hInstance);
return0;
}



2,

#include<d3d9.h>
#pragmawarning(disable:4996)//disabledeprecatedwarning
#include<strsafe.h>
#pragmawarning(default:4996)
LPDIRECT3D9g_pD3D=NULL;//UsedtocreatetheD3DDevice
LPDIRECT3DDEVICE9g_pd3dDevice=NULL;//Ourrenderingdevice
LPDIRECT3DVERTEXBUFFER9g_pVB=NULL;//Buffertoholdvertices顶点缓冲区
//Astructureforourcustomvertextype
structCUSTOMVERTEX

{
FLOATx,y,z,rhw;//Thetransformedpositionforthevertex
DWORDcolor;//Thevertexcolor
};
//OurcustomFVF,whichdescribesourcustomvertexstructure
#defineD3DFVF_CUSTOMVERTEX(D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
HRESULTInitD3D(HWNDhWnd)

{
//CreatetheD3Dobject.
if(NULL==(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
returnE_FAIL;
//SetupthestructureusedtocreatetheD3DDevice
D3DPRESENT_PARAMETERSd3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.Windowed=TRUE;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
//CreatetheD3DDevice
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,&g_pd3dDevice)))

{
returnE_FAIL;
}
//Devicestatewouldnormallybesethere
returnS_OK;
}
HRESULTInitVB()

{
//Initializethreeverticesforrenderingatriangle
CUSTOMVERTEXvertices[]=

{

{150.0f,50.0f,0.5f,1.0f,0xffff0000,},//x,y,z,rhw,color

{250.0f,250.0f,0.5f,1.0f,0xff00ff00,},

{50.0f,250.0f,0.5f,1.0f,0xff00ffff,},
};
//Createthevertexbuffer.Hereweareallocatingenoughmemory
//(fromthedefaultpool)toholdallour3customvertices.Wealso
//specifytheFVF,sothevertexbufferknowswhatdataitcontains.
if(FAILED(g_pd3dDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
0,D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,&g_pVB,NULL)))

{
returnE_FAIL;
}
//Nowwefillthevertexbuffer.Todothis,weneedtoLock()theVBto
//gainaccesstothevertices.Thismechanismisrequiredbecuasevertex
//buffersmaybeindevicememory.
VOID*pVertices;
if(FAILED(g_pVB->Lock(0,sizeof(vertices),(void**)&pVertices,0)))
returnE_FAIL;
memcpy(pVertices,vertices,sizeof(vertices));
g_pVB->Unlock();
returnS_OK;
}
VOIDCleanup()

{
if(g_pVB!=NULL)
g_pVB->Release();
if(g_pd3dDevice!=NULL)
g_pd3dDevice->Release();
if(g_pD3D!=NULL)
g_pD3D->Release();
}
VOIDRender()

{
//Clearthebackbuffertoabluecolor
g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);
//Beginthescene
if(SUCCEEDED(g_pd3dDevice->BeginScene()))

{
//Drawthetrianglesinthevertexbuffer.
g_pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
//Endthescene
g_pd3dDevice->EndScene();
}
//Presentthebackbuffercontentstothedisplay
g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
}
LRESULTWINAPIMsgProc(HWNDhWnd,UINTmsg,WPARAMwParam,LPARAMlParam)

{
switch(msg)

{
caseWM_DESTROY:
Cleanup();
PostQuitMessage(0);
return0;
}
returnDefWindowProc(hWnd,msg,wParam,lParam);
}

INTWINAPIWinMain(HINSTANCEhInst,HINSTANCE,LPSTR,INT)

{
//Registerthewindowclass
WNDCLASSEXwc=
{sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,
GetModuleHandle(NULL),NULL,NULL,NULL,NULL,
"D3DTutorial",NULL};
RegisterClassEx(&wc);
//Createtheapplication'swindow
HWNDhWnd=CreateWindow("D3DTutorial","D3DTutorial01:CreateDevice",
WS_OVERLAPPEDWINDOW,100,100,300,300,
NULL,NULL,wc.hInstance,NULL);
//InitializeDirect3D
if(SUCCEEDED(InitD3D(hWnd)))

{//初始化成功
//Createthevertexbuffer
if(SUCCEEDED(InitVB()))

{
//Showthewindow
ShowWindow(hWnd,SW_SHOWDEFAULT);
UpdateWindow(hWnd);
//Enterthemessageloop
MSGmsg;
while(true)

{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))

{//消息处理
if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else

{//空闲时绘制
Render();
}
}
}
}
UnregisterClass("D3DTutorial",wc.hInstance);
return0;
}
本文介绍如何使用Direct3D创建窗口并渲染三角形。包括初始化Direct3D设备、设置顶点缓冲区、绘制基本图形等内容。
680

被折叠的 条评论
为什么被折叠?



