新手问题,怎样才能让DrawPrimitive成功执行?
代码如下:
#include <d3d9.h>
#pragma comment (lib, "d3d9.lib")
struct s_CustomVertex
{
float x, y, z, rhw;
DWORD color;
};
HINSTANCE hInst;
HWND hwndHandle;
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
LPDIRECT3DVERTEXBUFFER9 g_pVB;
LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
bool initWindow (HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof (WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC) WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor (0, IDC_ARROW);
wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = "DirectXExample";
wcex.hIconSm = 0;
RegisterClassEx (&wcex);
hwndHandle = CreateWindow (
"DirectXExample",
"DirectXExample",
WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
0,
0,
hInstance,
0);
if (!hwndHandle)
return false;
ShowWindow (hwndHandle, SW_SHOW);
UpdateWindow (hwndHandle);
return true;
}
HRESULT SetupVB ()
{
HRESULT hr;
s_CustomVertex g_Vertices[] = {
{320, 50, 0.5, 1, D3DCOLOR_ARGB (0,255,0,0)},
{250, 400, 0.5, 1, D3DCOLOR_ARGB (0,0,255,0)},
{50, 400, 0.5, 1, D3DCOLOR_ARGB (0,0,0,255)}
};
hr = pd3dDevice->CreateVertexBuffer (
3 * sizeof (s_CustomVertex),
0,
D3DFVF_XYZRHW | D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT,
&g_pVB,
0);
if (FAILED (hr))
return hr;
void* pVertices;
hr = g_pVB->Lock (0, 3 * sizeof (s_CustomVertex), (void**) &pVertices, 0);
if (FAILED (hr))
return hr;
memcpy (pVertices, g_Vertices, 3 * sizeof (s_CustomVertex));
g_pVB->Unlock ();
return S_OK;
}
bool initDirect3D ()
{
// create pD3D object
pD3D = 0;
pd3dDevice = 0;
if (0 == (pD3D = Direct3DCreate9 (D3D_SDK_VERSION)))
return false;
// create pd3dDevice object
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory (&d3dpp, sizeof (d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = 800;
d3dpp.BackBufferWidth = 600;
d3dpp.hDeviceWindow = hwndHandle;
if (FAILED (pD3D->CreateDevice (
D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
hwndHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pd3dDevice)))
return false;
if (FAILED (SetupVB ()))
return false;
return true;
}
void render ()
{
pd3dDevice->Clear (0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB (0, 0, 255), 1.0f, 0);
IDirect3DSurface9* backbuffer = 0;
pd3dDevice->GetBackBuffer (
0,
0,
D3DBACKBUFFER_TYPE_MONO,
&backbuffer);
if (FAILED (pd3dDevice->SetStreamSource (0, g_pVB, 0, sizeof (s_CustomVertex))))
throw "SetStreamSource";
if (FAILED (pd3dDevice->SetFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)))
throw "SetFVF fail";
///////////////////////////////////////////////////////////////////////////////////
/*下面这条语句不能成功执行*/
if (FAILED (pd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 1)))
throw "DrawPrimitive fail";
///////////////////////////////////////////////////////////////////////////////////
pd3dDevice->Present (0, 0, 0, 0);
}
void cleanUp ()
{
pd3dDevice->Release ();
pD3D->Release ();
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int nCmdShow)
{
if (!initWindow (hInstance))
return 0;
if (!initDirect3D())
return 0;
MSG msg;
ZeroMemory (&msg, sizeof (msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage (&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
else
{
render ();
}
}
cleanUp ();
return (int) msg.wParam;
}