原文链接:MFC D3D Application: Direct3D Tutorial Part I
作者这个MFC程序中第一个有趣的地方是让用于Direct3D绘制的窗口类从CWnd类和CXD3D类继承下来:
class CD3DWnd : public CXD3D, public CWnd
CD3DWnd类是用于窗口中控件(比如一个PictureBox)的基类,这个控件将提供普通Cwnd类的功能,但同时也拥有CXD3D的3D绘制的能力。
先来看CXD3D类:
复制代码
//-----------------------------------------------------------------------------
// CXD3D class: the class a view class will derive from to provide a window
// handle to render into, and that will override the 3D scene rendering.
//-----------------------------------------------------------------------------
class CXD3D
{
protected:
// internal state variables
bool m_bActive; // toggled on Pause, can be queried upon
// initializing to issue a Create [false]
bool m_bStartFullscreen; // queried on ChooseInitialSettings [false]
bool m_bShowCursor; // in fullscreen mode [true]
bool m_bClipCursor; // in fullscreen mode [true]
bool m_bWindowed; // queried on BuildPresentParamsFromSettings
// [true]
bool m_bIgnoreSizeChange; // queried on HandlePossibleSizeChange [false]
bool m_bDeviceLost; // true when the device's Present fails
bool m_bDeviceObjectsInited; // true if InitDeviceObjects succeeds
bool m_bDeviceObjectsRestored; // true if RestoreDeviceObjects succeeds
// internal timing variables
FLOAT m_fTime; // absolute time handled by DXUtil_Timer
FLOAT m_fElapsedTime; // elapsed time handled by DXUtil_Timer
FLOAT m_fFPS; // the frame rate, or frames per second
// statistics
TCHAR m_strDeviceStats[256]; // device description
TCHAR m_strFrameStats[16]; // frame statistics
// main objects used for creating and rendering the 3D scene
HWND m_hWndRender; // device window
HWND m_hWndFocus; // focus window
LPDIRECT3D9 m_pd3d; // main D3D object
LPDIRECT3DDEVICE9 m_pd3dDevice; // D3D rendering device
D3DPRESENT_PARAMETERS m_d3dpp; // presentation parameters
DWORD m_dwCreateFlags; // sw/hw VP + pure device
DWORD m_dwWindowStyle; // saved for mode switches
RECT m_rcWindow; // window and client rects,
RECT m_rcClient; // saved for mode switches
// setup objects
CXD3DEnum Enumeration; // hierarchy of adapters, modes, devices, etc.
CXD3DSettings Settings; // current display settings
protected:
// internal error handling function
HRESULT DisplayErrorMsg(HRESULT hr, DWORD dwType);
// internal management functions
void BuildPresentParamsFromSettings();
bool FindBestWindowedMode(bool bHAL, bool bREF);
bool FindBestFullscreenMode(bool bHAL, bool bREF);
HRESULT ChooseInitialSettings();
HRESULT InitializeEnvironment();
HRESULT ResetEnvironment();
void CleanupEnvironment();
public:
HRESULT RenderEnvironment();
HRESULT HandlePossibleSizeChange();
protected:
void UpdateStats();
// Overridable functions for the 3D scene created by the app
virtual HRESULT OneTimeSceneInit() { return S_OK; }
virtual HRESULT InitDeviceObjects() { return S_OK; }
virtual HRESULT RestoreDeviceObjects() { return S_OK; }
virtual HRESULT FrameMove() { return S_OK; }
virtual HRESULT InvalidateDeviceObjects() { return S_OK; }
virtual HRESULT DeleteDeviceObjects() { return S_OK; }
virtual HRESULT FinalCleanup() { return S_OK; }
public:
virtual HRESULT Render() { return S_OK; }
// construct/destruct, create and pause/play
CXD3D();
virtual ~CXD3D() { }
virtual HRESULT CreateD3D();
virtual void Pause(bool bPause);
// active state wrapper
bool IsActive() { return m_bActive; };
// device and frame statistics wrappers
LPCTSTR GetDeviceStats() { return m_strDeviceStats; }
LPCTSTR GetFrameStats() { return m_strFrameStats; }
};
复制代码
CXD3D实现文件
在CXD3D::CreateD3D函数中,首先初始化了一个D3D对象,然后构建了一个列表,这个列表包含了机器上的所有显卡,显卡模式和设备。我们需要知道机器上有多少显卡(一般只有一个),而且每个显卡也可以支持多个设备。对于每个设备会有一种支持的格式,设置和能力,这对于应用程序来说不一定是合适的,因此我们需要一个列表来跟踪这些信息,以便挑选出合适的。
再来看枚举类CXD3DEnum,它用来为应用程序中使用的分辨率,颜色,A通道,显示格式,后备缓冲格式,深度/模板缓冲格式,多重采样类型,提交显示时间间隔等参数建立约束。
文中枚举类的代码很多,但我们只需要记住一点就可以了,要使Direct3D建立起来,我们首先得不断地枚举,枚举,再枚举,不断检查它的各种属性,各种能力是否满足,基本上检查的顺序可以用下图来表示:
复制代码
Enumeration
|
+-- AdapterInfos[0]
| |
| +-- DisplayModes[0]
| +-- DisplayModes[1]
|
| |
| +-- DeviceInfos[0]
| | |
| | +-- DeviceCombos[0]
| | | |
| | | +-- VPTypes
| | | +-- DSFormats
| | | +-- MSTypes
| | | +-- MSQualityLevels
| | | +-- DSMSConflicts
| | | +-- PresentIntervals
| | +-- DeviceCombos[1]
| |
| +-- DeviceInfos[1]
|
+-- AdapterInfos[1]
复制代码
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/07/08/1238479.html,如需转载请自行联系原作者