1. 加载库
1
#pragma comment(lib,"d3d9.lib")
2
3
#pragma comment(lib,"d3dx9.lib")
4
5
#pragma comment(lib,"winmm.lib")
6
7
8
9
#include <d3d9.h>
10
11
#include <d3dx9math.h>
12
2. 定义一个派生于generic CWnd的类CD3DWnd
3. 在CD3DWnd类中定义
1
LPDIRECT3D9 m_pD3D; //Direct3D对象的接口指针
2
3
LPDIRECT3DDEVICE9 m_pD3DDevice; //设备对象的接口指针
4
4. 在OnCreate()出调用InitD3D()函数
1 void CD3DWnd::InitD3D()
2 {
3 // 创建D3D对象,并获得接口ID3D9的指针
4 // 我们将通过该指针操作D3D对象
5 m_pD3D = ::Direct3DCreate9(D3D_SDK_VERSION);
6
7 D3DPRESENT_PARAMETERS d3dpp;
8 ZeroMemory( &d3dpp, sizeof(d3dpp));
9 d3dpp.Windowed = TRUE; // 创建窗口模式的D3D程序
10 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
11 d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
12
13 // 调用方法IDirect3d9::CreateDevice创建设备对象,并获取
14 // 接口ID3DDevice9指针,我们将通过该指针操作设备对象
15 m_pD3D->CreateDevice(
16 D3DADAPTER_DEFAULT
17 , D3DDEVTYPE_HAL
18 , m_hWnd
19 , D3DCREATE_SOFTWARE_VERTEXPROCESSING
20 , &d3dpp
21 , &m_pD3DDevice );
22
23 m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
24 }
25
5. 在OnPaint()调用Render()
1 void CD3DWnd::Render()
2 {
3 if( NULL == m_pD3DDevice )
4 return;
5
6 // Clear the backbuffer to a blue color
7 m_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);
8
9 // Begin the scene
10 if( SUCCEEDED( m_pD3DDevice->BeginScene() ) )
11 {
12 // Rendering of scene objects can happen here
13 SetupMatrices(); // 设置变换矩阵
14 m_pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
15 m_pD3DDevice->SetStreamSource(0,m_pVB,0,sizeof(CUSTOMVERTEX));
16 m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
17
18 // End the scene
19 m_pD3DDevice->EndScene();
20 }
21
22
23 // Present the backbuffer contents to the display
24 m_pD3DDevice->Present( NULL, NULL, NULL, NULL );
25 }
26
6. 在OnDestroy()调用Cleanup()
1 void CD3DWnd::Cleanup()
2 {
3 m_pVB->Release();
4 m_pD3DDevice->Release();
5 m_pD3D->Release();
6 }
7