各种基本图元的绘制
上面使用顶点缓冲区绘制的是三角形列表图元,前面介绍过Direct3D支持点列表,线段列表、线段条带、三角形列表、三角形条带、三角扇形6种基本图元。下面通过示例程序BasicPrimitive演示各种基本图元的绘制。该示例程序使用同一个顶点缓冲区绘制不同类型的图元,程序中通过一个全局变量标识当前绘制的图元类型,通过单击键盘上的"1"
~ "6"数字键可以在各图元类型之间进行切换,单击空格键可以在线框模式和实体模式之间切换。
三角形条带:
三角形列表:
线段条带:
线段列表:
点列表:
三角扇形:
实体模式:
完整源码:
#include <d3d9.h>
#define PRIMITIVE_TRIANGLE_STRIP 1
#define PRIMITIVE_TRIANGLE_LIST 2
#define PRIMITIVE_LINE_STRIP 3
#define PRIMITIVE_LINE_LIST 4
#define PRIMITIVE_POINT_LIST 5
#define PRIMITIVE_TRIANGLE_FAN 6
#define CLASS_NAME "GameApp"
#define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0)
IDirect3D9* g_d3d;
IDirect3DDevice9* g_device;
IDirect3DVertexBuffer9* g_vertex_buffer;
DWORD g_primitive_type = PRIMITIVE_TRIANGLE_STRIP;
DWORD g_fill_mode = D3DFILL_WIREFRAME;
struct sCustomVertex
{
float x, y, z, rhw;
DWORD color;
};
#define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
void init_vb()
{
sCustomVertex vertices[] =
{
{ 50.0f, 250.0f, 0.5f, 1.0f, 0xffff0000, },
{ 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, },
{ 250.0f, 250.0f, 0.5f, 1.0f, 0xffff0000, },
{ 350.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, },
{ 450.0f, 250.0f, 0.5f, 1.0f, 0xffff0000, },
{ 550.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }
};
g_device->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_DEFAULT, &g_vertex_buffer, NULL);
void* ptr;
g_vertex_buffer->Lock(0, sizeof(vertices), (void**)&ptr, 0);
memcpy(ptr, vertices, sizeof(vertices));
g_vertex_buffer->Unlock();
}
bool init_d3d(HWND hwnd)
{
g_d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(g_d3d == NULL)
return false;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_device)))
{
return false;
}
init_vb();
g_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // do not cull any face
g_device->SetRenderState(D3DRS_FILLMODE, g_fill_mode);
return true;
}
void cleanup()
{
release_com(g_vertex_buffer);
release_com(g_device);
release_com(g_d3d);
}
void render()
{
g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(5, 5, 5), 1.0f, 0);
g_device->BeginScene();
g_device->SetRenderState(D3DRS_FILLMODE, g_fill_mode);
g_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex));
g_device->SetFVF(D3DFVF_CUSTOM_VERTEX);
switch(g_primitive_type)
{
case PRIMITIVE_TRIANGLE_STRIP:
g_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 4);
break;
case PRIMITIVE_TRIANGLE_LIST:
g_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
break;
case PRIMITIVE_LINE_STRIP:
g_device->DrawPrimitive(D3DPT_LINESTRIP, 0, 5);
break;
case PRIMITIVE_LINE_LIST:
g_device->DrawPrimitive(D3DPT_LINELIST, 0, 3);
break;
case PRIMITIVE_POINT_LIST:
g_device->DrawPrimitive(D3DPT_POINTLIST, 0, 6);
break;
case PRIMITIVE_TRIANGLE_FAN:
g_device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 4);
break;
}
g_device->EndScene();
g_device->Present(NULL, NULL, NULL, NULL);
}
LRESULT WINAPI WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_KEYDOWN:
switch(wParam)
{
case 49: // key "1"
case 50: // key "2"
case 51: // key "3"
case 52: // key "4"
case 53: // key "5"
case 54: // key "6"
g_primitive_type = (DWORD)wParam - 48;
break;
case VK_SPACE:
g_fill_mode = (g_fill_mode == D3DFILL_WIREFRAME) ? D3DFILL_SOLID : D3DFILL_WIREFRAME;
break;
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR, INT)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_CLASSDC;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = inst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = CLASS_NAME;
wc.hIconSm = NULL;
if(! RegisterClassEx(&wc))
return -1;
HWND hwnd = CreateWindow(CLASS_NAME, "Direct3D App", WS_OVERLAPPEDWINDOW, 200, 100, 600, 300,
NULL, NULL, wc.hInstance, NULL);
if(hwnd == NULL)
return -1;
if(init_d3d(hwnd))
{
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
render();
}
}
cleanup();
UnregisterClass(CLASS_NAME, wc.hInstance);
return 0;
}