Direct3D 16边形 就是练习
程序员文章站
2022-07-13 09:14:54
...
我什么也不说,就是一张结果图
代码
// tWinMain.cpp : Defines the entry point for the application.
#define _USE_MATH_DEFINES
#include "windows.h"
#include "tchar.h"
#include "d3d9.h"
#include "d3dx9.h"
#include "math.h"
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "winmm.lib")
#define WNDWIDTH 800
#define WNDHEIGHT 600
typedef struct _CUSTOMVERTEX
{
FLOAT x, y, z, rhw;
DWORD color;
}CUSTOMVERTEX;
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
// Global Variables:
HWND g_hWnd = NULL;
LPDIRECT3D9 g_pDirect3D = NULL;
LPDIRECT3DDEVICE9 g_pDirect3DDevice = NULL;
LPD3DXFONT g_pD3DXFont = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL;
LPDIRECT3DINDEXBUFFER9 g_pIndexBuffer = NULL;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HRESULT Direct3DInit();
VOID Direct3DRender();
VOID Direct3DClean();
bool GameInit();
void GameRender();
void GameClean();
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg = {0};
// Initialize global strings
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Direct3DRender();
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = _T("WndClass");
wcex.hIconSm = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
g_hWnd = CreateWindow(_T("WndClass"), _T("LearnGame"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, WNDWIDTH, WNDHEIGHT, NULL, NULL, hInstance, NULL);
if (!g_hWnd)
{
return FALSE;
}
if (FAILED(Direct3DInit()))
{
return FALSE;
}
ShowWindow(g_hWnd, nCmdShow);
UpdateWindow(g_hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN:
if (wParam == VK_ESCAPE)
{
Direct3DClean();
DestroyWindow(hWnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
HRESULT Direct3DInit()
{
g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
if (g_pDirect3D == NULL)
{
return E_FAIL;
}
D3DCAPS9 d3dcaps;
ZeroMemory(&d3dcaps, sizeof(D3DCAPS9));
if (FAILED(g_pDirect3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dcaps)))
{
return E_FAIL;
}
int vp = 0;
if (d3dcaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else
{
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.BackBufferWidth = WNDWIDTH;
d3dpp.BackBufferHeight = WNDHEIGHT;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = g_hWnd;
d3dpp.Windowed = TRUE;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = 0;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
if (FAILED(g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, vp, &d3dpp, &g_pDirect3DDevice)))
{
return E_FAIL;
}
if (FAILED(D3DXCreateFont(g_pDirect3DDevice, 24, 0, 0, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, _T("Arial"), &g_pD3DXFont)))
{
return E_FAIL;
}
if (!GameInit())
{
return E_FAIL;
}
return S_OK;
}
VOID Direct3DRender()
{
g_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
if (SUCCEEDED(g_pDirect3DDevice->BeginScene()))
{
GameRender();
g_pDirect3DDevice->EndScene();
g_pDirect3DDevice->Present(NULL, NULL, NULL, NULL);
}
}
VOID Direct3DClean()
{
GameClean();
g_pD3DXFont->Release();
g_pD3DXFont = NULL;
g_pDirect3DDevice->Release();
g_pDirect3DDevice = NULL;
g_pDirect3D->Release();
g_pDirect3D = NULL;
}
bool GameInit()
{
if (FAILED(g_pDirect3DDevice->CreateVertexBuffer(17 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL)))
{
return false;
}
if (FAILED(g_pDirect3DDevice->CreateIndexBuffer(48 * sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIndexBuffer, NULL)))
{
return false;
}
CUSTOMVERTEX vertices[17];
vertices[0].x = 400.0f;
vertices[0].y = 300.0f;
vertices[0].z = 0.0f;
vertices[0].rhw = 1.0f;
vertices[0].color = D3DCOLOR_XRGB(rand()%256, rand()%256, rand()%256);
for (int idx = 0; idx < 16; idx++)
{
vertices[idx + 1].x = (float)(250.0f * sinf(idx * M_PI / 8.0f)) + 400.0f;
vertices[idx + 1].y = -(float)(250.0f * cosf(idx * M_PI / 8.0f)) + 300.0f;
vertices[idx + 1].z = 0.0f;
vertices[idx + 1].rhw = 1.0f;
vertices[idx + 1].color = D3DCOLOR_XRGB(rand()%256, rand()%256, rand()%256);
}
VOID* pVertices = NULL;
if (FAILED(g_pVertexBuffer->Lock(0, sizeof(vertices), static_cast<VOID**>(&pVertices), 0)))
{
return false;
}
memcpy(pVertices, vertices, sizeof(vertices));
g_pVertexBuffer->Unlock();
WORD indices[] =
{ 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5,
0, 5, 6, 0, 6, 7, 0, 7, 8, 0, 8, 9, 0, 9, 10,
0, 10, 11, 0, 11, 12, 0, 12, 13, 0, 13, 14,
0, 14, 15, 0, 15, 16, 0, 16, 1 };
WORD* pIndices = NULL;
if (FAILED(g_pIndexBuffer->Lock(0, 0, (VOID**)(&pIndices), 0)))
{
return false;
}
memcpy(pIndices, indices, sizeof(indices));
g_pIndexBuffer->Unlock();
return true;
}
void GameRender()
{
g_pDirect3DDevice->SetStreamSource(0, g_pVertexBuffer, 0, sizeof(CUSTOMVERTEX));
g_pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pDirect3DDevice->SetIndices(g_pIndexBuffer);
g_pDirect3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 17, 0, 16);
}
void GameClean()
{
g_pVertexBuffer->Release();
g_pVertexBuffer = NULL;
g_pIndexBuffer->Release();
g_pIndexBuffer = NULL;
}