|
|
作者:HuiHui
本节主要的目的就是要学习如何在D3D中画一个三角形,主要用到渲染方面的知识,下面我就直接给出源代码:
#include <d3d8.h>
#include <D3dx8math.h>
LPDIRECT3D8 g_pD3D=NULL;
LPDIRECT3DDEVICE8 g_pD3DDevice=NULL;
LPDIRECT3DVERTEXBUFFER8 g_pVertextBuffer=NULL;
HWND hWnd;
HINSTANCE hInstance;
struct CUSTOMVERTEX
{
D3DXVECTOR3 position;
D3DCOLOR color;
};
#define SafeRelease(pObject) if(pObject!=NULL) {pObject->Release();pObject=NULL;}
#define D3DFVF_CUSTOMVER (D3DFVF_XYZ|D3DFVF_DIFFUSE)
//Function Declaration
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool CreatWindow();
HRESULT InitialD3D(HWND hWnd);
HRESULT InitialVB();
void Render();
void CleanUp();
void GameLoop();
//Application entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
if(!CreatWindow())
{
return false;
}
//Initialize Direct3D
if(SUCCEEDED(InitialD3D(hWnd)))
{
//Initialize Vertex Buffer
if(SUCCEEDED(InitialVB()))
{
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
//Start game running: Enter the game loop
GameLoop();
}
}
CleanUp();
UnregisterClass("HUIHUI WorkGroup2", hInstance);
return 0;
}
//The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_KEYUP:
if (wParam==VK_ESCAPE)
{
//User has pressed the escape key, so quit
DestroyWindow(hWnd);
}
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
bool CreatWindow()
{
//Register the window class
WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"HUIHUI WorkGroup2", NULL};
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,"Register Window Error!","ERROR!",MB_OK);
return false;
}
//Create the application's window
hWnd = CreateWindow("HUIHUI WorkGroup2", "HUIHUI WorkGroup: routine 2",
WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
GetDesktopWindow(), NULL, wc.hInstance, NULL);
return true;
}
//initialiseD3D
HRESULT InitialD3D(HWND hWnd)
{
g_pD3D=Direct3DCreate8(D3D_SDK_VERSION);
if(g_pD3D==NULL)
{
return E_FAIL;
}
D3DDISPLAYMODE d3ddm;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.Windowed=TRUE;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat=d3ddm.Format;
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,
&g_pD3DDevice)))
return E_FAIL;
g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// turn off light
g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
return S_OK;
}
HRESULT InitialVB()
{
CUSTOMVERTEX * pVertices;
//Create the vertex buffer from our device
if(FAILED(g_pD3DDevice->CreateVertexBuffer(3* sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVER ,
D3DPOOL_DEFAULT, &g_pVertextBuffer)))
{
return E_FAIL;
}
//Get a pointer to the vertex buffer vertices and lock the vertex buffer
if(FAILED(g_pVertextBuffer->Lock(0, 0, (BYTE **)&pVertices, 0)))
{
return E_FAIL;
}
//Copy our stored vertices values into the vertex buffer
pVertices[0].position=D3DXVECTOR3(-1.0f,-1.0f, 0.0f);
pVertices[0].color=D3DCOLOR_XRGB(255,0,0);
pVertices[1].position=D3DXVECTOR3(1.0f,-1.0f, 0.0f);
pVertices[1].color=D3DCOLOR_XRGB(0,255,0);
pVertices[2].position=D3DXVECTOR3( 0.0f, 1.0f, 0.0f);
pVertices[2].color=D3DCOLOR_XRGB(0,0,255);
//Unlock the vertex buffer
g_pVertextBuffer->Unlock();
return S_OK;
}
void Render()
{
if(g_pD3DDevice == NULL)
{
return;
}
//Clear the backbuffer to black
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0, 0), 1.0f, 0);
//Begin the scene
g_pD3DDevice->BeginScene();
//Rendering our triangle
g_pD3DDevice->SetStreamSource(0, g_pVertextBuffer, sizeof(CUSTOMVERTEX));
g_pD3DDevice->SetVertexShader( D3DFVF_CUSTOMVER );
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
//End the scene
g_pD3DDevice->EndScene();
//Filp the back and front buffers so that whatever has been rendered on the back buffer
//will now be visible on screen (front buffer).
g_pD3DDevice-> resent(NULL, NULL, NULL, NULL);
}
void CleanUp()
{
SafeRelease(g_pVertextBuffer);
SafeRelease(g_pD3DDevice);
SafeRelease(g_pD3D);
}
void GameLoop()
{
//Enter the game loop
MSG msg;
BOOL fMessage;
PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
while(msg.message != WM_QUIT)
{
fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
if(fMessage)
{
//Process message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//No message to process, so render the current scene
Render();
}
}
}
效果图是:
在本文本中,你可以注意到我已经没有在每行代码后面作注释了,因为那样的编程风格是不好的,至少我认为是那样的,呵呵!*注:本程序中还要在Project->Setting->Link的 L object/Module中加入:d3d8.lib、dxguid.lib文件,因为这样不会出现连接错误,不信!你试试看吧!呵呵!因为我写的文章很少,有什么不好的地方请给我留言,我一定会看的。下次见,拜拜!!!!(本文出自HuiHui WorkGruop)
|
-
|