|
[em7]
诸多教程,使用的是DirectX9.0c SDK,然而现在最新的DX SDK貌似有很多地方与其不一致
比如下面的入门级的代码,渲染出一个三角形,
1. 原版: SetStreamSource(0, g_pVB, sizeof(CUSTOMVERTEX)
现在: SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX)
2。 原版: SetVertexShader(D3DFVF_CUSTOMVERTEX)
现在: SetVertexShader(NULL) (貌似分了两步)
SetFVF(D3DFVF_CUSTOMVERTEX)
3。 现在: CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL)))
最后的参数是新增的
搞不懂啊,大多数教程都这样,
原来照着一行行敲,编译就出错,狂baidu,狂google,搞成现在 这样子,窗口里还是毛都没得 ……
郁闷了,入个门容易么 ……
难道教材还没更新?还是我没找到最新的版本?为什么很少有人提及?都用的原来的老的SDK??
唉 ……
#include <d3d9.h>
#pragma comment(lib, "d3d9.lib")
HWND g_hWnd = NULL;
LPDIRECT3D9 g_pd3d = NULL; // create d3d device
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // d3d device
//------------------------------------------------------------------------------
// chapter 2:
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // vertex buffer
struct CUSTOMVERTEX
{
FLOAT x, y, z; // vertex coordinates
DWORD color; // vertex color
};
CUSTOMVERTEX g_Vertices[] = { { -1.0f, -1.0f, 0.0f, 0xffff0000 }, // color:red
{ 1.0f, -1.0f, 0.0f, 0xff0000ff }, // blue
{ 0.0f, 1.0f, 0.0f, 0xffffffff }}; // white
// FVF(flexible vertex format) : self defined here
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
// end chapter 2.
//------------------------------------------------------------------------------
// initialize d3d
HRESULT InitD3D(HWND hWnd);
// release d3d
VOID Cleanup();
// render
VOID Render();
// window msg proc
LRESULT WINAPI MsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
/////////////////////////////////////////////////////////////////////////////////////////////////////
// WINMAIN
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
WNDCLASSEX wcex = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClassEx(&wcex);
HWND hWnd = CreateWindowEx(NULL, "D3D Tutorial", "D3D Tutorial 01: CreateDevice",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), NULL, wcex.hInstance, NULL);
// initialize d3d
if (SUCCEEDED(InitD3D(hWnd)))
{
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
g_hWnd = hWnd;
// message loop
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();
}
}
}
// over processing
Cleanup();
UnregisterClass("D3D Tutorial", wcex.hInstance);
return 0;
}
LRESULT WINAPI MsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
VOID Render()
{
if (NULL == g_pd3dDevice)
{
return;
}
// clear background color to blue
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0, 0, 255), 1.0F, 0);
// begin to draw scene
g_pd3dDevice->BeginScene();
// TODO: put what you want to do here
//--------------------------------------------------------------------------------------
// chapter 2:
// specify the render source
// g_pd3dDevice->SetStreamSource(0, g_pVB, sizeof(CUSTOMVERTEX));
if (g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX)) != D3D_OK)
{
MessageBox(g_hWnd, "setstreamsource failed", "notice", MB_OK);
}
// specify the self definition FVF
// error C2664: “IDirect3DDevice9::SetVertexShader”: 不能将参数 1 从“int”转换为“IDirect3DVertexShader9 *”
// g_pd3dDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
// g_pd3dDevice->SetVertexShader(NULL);
if (g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX) != D3D_OK)
{
MessageBox(g_hWnd, "setfvf failed", "notice", MB_OK);
}
// render
if (g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1) != D3D_OK)
{
MessageBox(g_hWnd, "draw failed", "notice", MB_OK);
}
// end
//--------------------------------------------------------------------------------------
// end draw scene
g_pd3dDevice->EndScene();
// show to screen
g_pd3dDevice-> resent(NULL, NULL, NULL, NULL);
}
VOID Cleanup()
{
if (g_pd3dDevice != NULL)
{
g_pd3dDevice->Release();
}
if (g_pd3d != NULL)
{
g_pd3d->Release();
}
}
HRESULT InitD3D(HWND hWnd)
{
// create d3d object
if (NULL == (g_pd3d = Direct3DCreate9(D3D_SDK_VERSION)))
{
return E_FAIL;
}
// get current show mode
D3DDISPLAYMODE d3ddm;
if (FAILED(g_pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp)); // #define ZeroMemory RtlZeroMemory
d3dpp.Windowed = TRUE; // window mode
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // set up swap mode
d3dpp.BackBufferFormat = d3ddm.Format; // set up back buffer format as current left format
// create d3d device
if (FAILED(g_pd3d->CreateDevice(D3DADAPTER_DEFAULT, // use default display card
D3DDEVTYPE_HAL, // request use HAL(硬件抽象层)
hWnd, // window handle
D3DCREATE_SOFTWARE_VERTEXPROCESSING,// use software processing vertex
&d3dpp, // parameter for created
&g_pd3dDevice))) // d3d device pointer
{
return E_FAIL;
}
//--------------------------------------------------------------------------------
// chapter 2:
// create vertex buffer area
// buffer area size (here we have 3 vertexs
if (FAILED(g_pd3dDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
0,// //
D3DFVF_CUSTOMVERTEX,// self definition FVF
D3DPOOL_DEFAULT, // it shows how to create, use DEFAULT just OK
&g_pVB, // return the pointer who pointed to the buffer
NULL)))
{
return E_FAIL;
}
// VOID *pVertices;
CUSTOMVERTEX *pVertex;
// error C2664: “IDirect3DVertexBuffer9: ock”: 不能将参数 3 从“BYTE **”转换为“void **”
// g_pVB->Lock(0, sizeof(pVertices), (BYTE **)&pVertices, 0);
// g_pVB->Lock(0, sizeof(pVertices), (VOID **)&pVertices, 0);
// g_pVB->Lock(0, sizeof(pVertex), (VOID **)&pVertex, 0);
if (FAILED(g_pVB->Lock(0, sizeof(g_Vertices), (VOID**)&pVertex, 0)))
{
return E_FAIL;
}
memcpy(pVertex, g_Vertices, sizeof(g_Vertices));
g_pVB->Unlock();
// end
//--------------------------------------------------------------------------------
return S_OK;
}
|
|