D3D9利用顶点缓冲区绘制三角形
程序员文章站
2024-02-21 14:36:34
...
#include <d3d9.h>
//附加依赖项 d3d9.lib
//利用顶点缓冲区绘制三角形
const TCHAR szClassName[MAX_PATH] = TEXT("冒险岛class");
const TCHAR szTilteName[MAX_PATH] = TEXT("冒险岛");
LPDIRECT3D9 g_pD3D = NULL; //Direct3D对象
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; //Direct3D设备对象
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; //顶点缓冲区对象
struct CUSTOMVERTEX
{
FLOAT x, y,z, rhw;
//
DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW| D3DFVF_DIFFUSE)
//顶点格式
HRESULT Init3D(HWND hwnd)
{
if(NULL ==(g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE; //是否窗口化
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice)))
return E_FAIL;
return S_OK;
}
HRESULT InitVB()
{
CUSTOMVERTEX vertices[] =
{
{100.0f, 400.0f, 0.5f, 1.0f, 0xffff0000},
{300.0f, 50.f, 0.5f, 1.0f, 0xff00ff00},
{500.0f, 400.0f, 0.5f, 1.0f, 0xff0000ff},
};
HRESULT hr = g_pd3dDevice->CreateVertexBuffer(sizeof(CUSTOMVERTEX) * 3,
0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL);
if (FAILED(hr))
return E_FAIL;
VOID* pVertices;
if (FAILED(g_pVB->Lock(0, sizeof(vertices), (void**)&pVertices, 0)))
return E_FAIL;
memcpy(pVertices, vertices, sizeof(vertices));
g_pVB->Unlock();
return S_OK;
}
VOID Cleanup()
{
//释放内存
if (g_pd3dDevice != NULL)
g_pd3dDevice->Release();
if (g_pD3D != NULL)
g_pD3D->Release();
if (g_pVB != NULL)
g_pVB->Release();
}
VOID Render()
{
//清空后台缓冲区
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(45, 50, 170), 1.0f, 0);
//开始在后台缓存区绘制图形
if (SUCCEEDED(g_pd3dDevice->BeginScene()))
{
//在后台缓冲区绘制图形
g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_pd3dDevice->EndScene();
//在后台缓冲区结束绘制图形
}
//将在后台缓冲区绘制的图形提交到前台缓冲区显示
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
Cleanup();
PostQuitMessage(0);
return 0;
case WM_PAINT:
Render();
ValidateRect(hwnd, NULL);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_CLASSDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(NULL);
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = szClassName;
wc.hIconSm = NULL;
//注册窗口类
if (!RegisterClassEx(&wc))
MessageBox(NULL, TEXT("注册窗口类失败"), NULL, NULL);
//创建窗口
HWND hwnd = CreateWindow(szClassName, szTilteName,
WS_OVERLAPPEDWINDOW,
200, 100, 600, 500,
NULL, NULL, hInstance, 0);
if (SUCCEEDED(Init3D(hwnd)))
{
if (SUCCEEDED(InitVB()))
{
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);
}
else
{
Render();
}
}
}
}
UnregisterClass(szClassName, hInstance);
return 0;
}
抄的代码
效果图
记得在此处添加附加依赖项
下一篇: 两数之和(力扣)