|
类似于这?
- #include <d3d9.h>
- #include <d3dx9.h>
- // 全局变量
- LPDIRECT3D9 g_pD3D = NULL;
- LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
- LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;
- LPDIRECT3DTEXTURE9 g_pTexture = NULL;
- struct CUSTOMVERTEX {
- FLOAT x, y, z, rhw; // 位置
- FLOAT tu, tv; // 纹理坐标
- };
- #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_TEX1)
- // 初始化 Direct3D
- HRESULT InitD3D(HWND hWnd) {
- if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
- 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 InitVertexBuffer() {
- if (FAILED(g_pd3dDevice->CreateVertexBuffer(4 * sizeof(CUSTOMVERTEX),
- 0, D3DFVF_CUSTOMVERTEX,
- D3DPOOL_MANAGED, &g_pVB, NULL))) {
- return E_FAIL;
- }
- CUSTOMVERTEX* pVertices;
- if (FAILED(g_pVB->Lock(0, sizeof(CUSTOMVERTEX) * 4, (void**)&pVertices, 0)))
- return E_FAIL;
- pVertices[0].x = 0.0f; pVertices[0].y = 0.0f; pVertices[0].z = 0.5f; pVertices[0].rhw = 1.0f; pVertices[0].tu = 0.0f; pVertices[0].tv = 0.0f;
- pVertices[1].x = 800.0f; pVertices[1].y = 0.0f; pVertices[1].z = 0.5f; pVertices[1].rhw = 1.0f; pVertices[1].tu = 1.0f; pVertices[1].tv = 0.0f;
- pVertices[2].x = 0.0f; pVertices[2].y = 600.0f; pVertices[2].z = 0.5f; pVertices[2].rhw = 1.0f; pVertices[2].tu = 0.0f; pVertices[2].tv = 1.0f;
- pVertices[3].x = 800.0f; pVertices[3].y = 600.0f; pVertices[3].z = 0.5f; pVertices[3].rhw = 1.0f; pVertices[3].tu = 1.0f; pVertices[3].tv = 1.0f;
- g_pVB->Unlock();
- return S_OK;
- }
- // 加载纹理
- HRESULT LoadTexture() {
- if (FAILED(D3DXCreateTextureFromFileEx(g_pd3dDevice, "pic.png",
- D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0,
- D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
- D3DX_DEFAULT, D3DX_DEFAULT, 0,
- NULL, NULL, &g_pTexture))) {
- return E_FAIL;
- }
- return S_OK;
- }
- // 渲染场景
- void Render() {
- if (NULL == g_pd3dDevice)
- return;
- g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- if (SUCCEEDED(g_pd3dDevice->BeginScene())) {
- // 启用 alpha 混合
- g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
- g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
- g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
- // 设置顶点格式
- g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
- // 设置纹理
- g_pd3dDevice->SetTexture(0, g_pTexture);
- // 设置顶点缓冲区
- g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
- // 绘制矩形
- g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
- // 关闭 alpha 混合(如果不需要后续绘制继续使用)
- g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
- g_pd3dDevice->EndScene();
- }
- g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
- }
- // 清理 Direct3D 和 COM
- void Cleanup() {
- if (g_pTexture != NULL)
- g_pTexture->Release();
- if (g_pVB != NULL)
- g_pVB->Release();
- if (g_pd3dDevice != NULL)
- g_pd3dDevice->Release();
- if (g_pD3D != NULL)
- g_pD3D->Release();
- }
- // 窗口过程
- LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- switch (msg) {
- case WM_DESTROY:
- Cleanup();
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hWnd, msg, wParam, lParam);
- }
- // WinMain
- INT WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, INT) {
- WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, L"D3D Tutorial", NULL };
- RegisterClassEx(&wc);
- HWND hWnd = CreateWindow(L"D3D Tutorial", L"D3D Tutorial 01: CreateDevice",
- WS_OVERLAPPEDWINDOW, 100, 100, 800, 600,
- NULL, NULL, wc.hInstance, NULL);
- if (SUCCEEDED(InitD3D(hWnd))) {
- if (SUCCEEDED(InitVertexBuffer()) && SUCCEEDED(LoadTexture())) {
- ShowWindow(hWnd, SW_SHOWDEFAULT);
- UpdateWindow(hWnd);
- MSG msg;
- ZeroMemory(&msg, sizeof(msg));
- while (msg.message != WM_QUIT) {
- if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- } else {
- Render();
- }
- }
- }
- }
- UnregisterClass(L"D3D Tutorial", wc.hInstance);
- return 0;
- }
复制代码 |
|