听说DirectX9.0发布了吗?如果没有,你现在也应该听我说过了,那就去http://download.microsoft.com/download/b/6/a/b6ab32f3-39e8-4096-9445-d38e6675de85/dx90bsdk.exe下载一个最新的DirectX9.0的SDK,因为我将使用D3D9来进行全文的讲解。其实DirectX9.0里有非常详细的教程和参考,大多数人只需要看看这些帮助就可以自己学习D3D了,我的这篇文章适合那些很懒但想快速入门、不懂英文或编程知识很欠缺的人看。装好DirectX9.0后,打开VC.net,新建一个Win32工程,在StdAfx.h里添加下面的语句:
#include
// D3D标准头文件
#include // D3D数学库头文件
#include // 这个不用我说了吧?
#pragma comment( lib, "d3d9" ) // D3D的静态库
#pragma comment( lib, "d3dx9" ) // D3D数学库的静态库
然后把winmain所在文件的代码做如下的修改:
#include "stdafx.h"
#define MAX_LOADSTRING 100
HINSTANCE g_hInst;
HWND g_hWnd;
IDirect3D9 *g_pD3D;
IDirect3DDevice9 *g_pd3dDevice;
IDirect3DVertexBuffer9 *g_pVB;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void OnIdle( void );
void OnCreate( HWND hWnd );
HRESULT InitD3D( void );
HRESULT CreateObject( void );
void ReleaseD3D( void );
HRESULT SetModalMatrix( void );
HRESULT SetProjMatrix( WORD wWidth, WORD wHeight );
void BeforePaint( void );
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_D3DTEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_D3DTEST);
while ( true )
{
if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
continue;
}
if ( WM_QUIT == msg.message )
{
break;
}
OnIdle();
}
UnregisterClass( szWindowClass, g_hInst );
return (int)msg.wParam;
}
ATOM MyRegisterClass( 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 = LoadIcon(hInstance, (LPCTSTR)IDI_D3DTEST);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCTSTR)IDC_D3DTEST;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
{
g_hInst = hInstance;
CreateWindow( szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL );
if ( !g_hWnd )
{
return FALSE;
}
ShowWindow( g_hWnd, nCmdShow );
UpdateWindow( g_hWnd );
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
switch (message)
{
case WM_CREATE:
OnCreate( hWnd );
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_SIZE:
SetProjMatrix( LOWORD( lParam ), HIWORD( lParam ) );
break;
case WM_DESTROY:
ReleaseD3D();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void OnCreate( HWND hWnd )
{
g_hWnd = hWnd;
InitD3D();
CreateObject();
}
void ReleaseD3D( void )
{
}
HRESULT InitD3D( void )
{
return S_OK;
}
void BeforePaint( void )
{
}
HRESULT CreateObject( void )
{
return S_OK;
}
void OnIdle( void )
{
}
HRESULT SetModalMatrix( void )
{
return S_OK;
}
HRESULT SetProjMatrix( WORD wWidth, WORD wHeight )
{
return S_OK;
}
上面的代码仅是一个框架,仔细研究过上篇文章的朋友应该非常清楚这些代码的含意,不过我还是需要大至讲解一下。 在InitInstance函数中,初始化程序实例的开始,我们将该实例的句柄放到全局变量中去,以便以后使用:
g_hInst = hInstance;
在创建窗体时,我并没有直接将CreateWindow的返回值――创建成功的窗体句柄赋给全局变量g_hWnd,因为CreateWindow函数在执行中会引发几个消息,其中有WM_CREATE,在这个消息的处理函数中OnCreate中,我执行了下面的语句:
g_hWnd = hWnd;
这样,我们就可以早一点用到g_hWnd了。SetProjMatrix是设置投影矩阵的,投影矩形仅受窗口纵横比影响,所以在WM_SIZE事件时调用时就可以了。而SetModalMatrix是设置模型矩阵的,也就是眼睛点和视点之类的东东,所以它一定要在Rander的时候,准确的说是在Render之前执行。InitD3D在窗体创建时调用,用于初始化D3D设备。CreateObject和InitD3D一样是初始化函数,它用于创建三维对象。
下面我们来填写代码,首先是初始化,我们在InitD3D函数里填写下面的代码:
g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
if( NULL == g_pD3D )
{
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE,
&d3dpp, &g_pd3dDevice );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_AMBIENT,
D3DCOLOR_COLORVALUE( 0.3f, 0.3f, 0.3f, 1.0 ) );
g_pd3dDevice->LightEnable( 0, TRUE);
D3DMATERIAL9 mtrl;
ZeroMemory( &mtrl, sizeof(mtrl) );
mtrl.Diffuse.r = mtrl.Ambient.r = 140.0f / 255.0f;
mtrl.Diffuse.g = mtrl.Ambient.g = 200.0f / 255.0f;
mtrl.Diffuse.b = mtrl.Ambient.b = 255.0f / 255.0f;
mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
g_pd3dDevice->SetMaterial( &mtrl );
return S_OK;
Direct3DCreate9函数为我们创建了一个D3D接口指针,并将接口指针返回到全局变量中保存起来,参数必须为D3D_SDK_VERSION。D3DPRESENT_PARAMETERS是一个结构体,它就像OpenGL中的PixelFormat一样,是创建时指定设备的一些属性的。
d3dpp.Windowed = TRUE; // 设备是窗口设备而不是全屏
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // 翻转缓冲区时不改动后台缓冲
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; // ARGB颜色模式
在这之后,我们就可以调用CreateDevice来创建设备了,第一个参数指定使用主显示驱动,第二个参数指定该设备是否使用硬件来处理显示,第三个参数当然是你的窗体句柄,第四个参数如果指定D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_查看本文来源